Skip to content

Commit c43b84f

Browse files
authored
Update README.md
1 parent cae50c6 commit c43b84f

File tree

1 file changed

+83
-3
lines changed

1 file changed

+83
-3
lines changed

README.md

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,90 @@
11
# kotlin-db-migration
2-
数据库迁移
2+
3+
1. simple & light
4+
1. name based version
5+
1. support dry run
6+
1. support code migration and sql migration
7+
8+
```kotlin
9+
@Component
10+
class Migration: InitializingBean, ApplicationListener<ApplicationReadyEvent>{
11+
@Value("\${spring.datasource.url}")
12+
val dbUrl: String = ""
13+
14+
@Value("\${spring.datasource.username}")
15+
val dbUsername: String = ""
16+
17+
@Value("\${spring.datasource.password}")
18+
val dbPassword: String = ""
19+
20+
@Autowired
21+
lateinit var migrationConfig: MigrationConfigProperties
22+
23+
@Autowired
24+
protected var context: ApplicationContext? = null
25+
26+
override fun afterPropertiesSet() {
27+
if (migrationConfig.isEnable) {
28+
Migrate(dbUrl, dbUsername, dbPassword).run(migrationConfig.isDryRun)
29+
}
30+
}
31+
32+
override fun onApplicationEvent(event: ApplicationReadyEvent) {
33+
Migrate(dbUrl, dbUsername, dbPassword).runCode(context, migrationConfig.isDryRun)
34+
}
35+
}
36+
```
337

438
```kotlin
39+
class M2018_03_15_105104_test_migration: CodeMigration<ApplicationContext> {
40+
override fun run(context: ApplicationContext) {
41+
// context.getBean("xxx")
42+
// do what every you want
43+
}
44+
}
45+
```
46+
47+
```gradle
48+
// ./gradlew :common:createMigration -Ptp=code -Pmn=create_role_table
49+
createMigration.doLast {
50+
if (project.hasProperty('mn')) {
51+
def content = "\n"
52+
def fileName = ""
53+
def path = ""
54+
55+
if (project.hasProperty('tp') && project.tp == 'code') {
56+
def className = "M" + (new Date()).format('yyyy_MM_dd_HHmmss_') + "${project.mn}"
57+
fileName = "${className}.kt"
58+
path = "$projectDir/src/main/kotlin/db/migrations"
59+
content = """
60+
package db.migrations
61+
62+
import info.purocean.dbmigration.CodeMigration
63+
import org.springframework.context.ApplicationContext
64+
65+
class ${className}: CodeMigration<ApplicationContext> {
66+
override fun run(context: ApplicationContext) {
67+
// TODO
68+
}
69+
}
70+
"""
71+
} else {
72+
fileName = (new Date()).format('yyyy_MM_dd_HHmmss_') + "${project.mn}.sql"
73+
path = "$projectDir/src/main/resources/db/migrations"
74+
}
75+
76+
new File(path).mkdirs()
77+
78+
def file = new File("$path/$fileName")
79+
80+
file.createNewFile()
581
6-
val migrate = Migrate("jdbc:mysql://localhost/db_migration?useUnicode=true&characterEncoding=utf8&serverTimezone=PRC&useSSL=true", "root", "yang")
82+
file.text = content
783
8-
migrate.run()
84+
println("created:$path/$fileName")
85+
} else {
86+
println("no name!")
87+
}
88+
}
989
1090
```

0 commit comments

Comments
 (0)