use*_*304 14
如果您将 Ktor 与 Gradle 一起使用,我建议您在应用程序的入口点内以编程方式使用 Flyway。这样,它就可以轻松成为持续交付管道的一部分。您可以在此处查看使用 API 的 Flyway 文档:https ://flywaydb.org/documentation/api/
我本质上做的是添加依赖项(使用 Kotlin DSL):
implementation("org.flywaydb:flyway-core:6.5.2")
Run Code Online (Sandbox Code Playgroud)
然后您需要做的就是创建一个 Flyway 实例并在加载模块时调用 migrate:
fun Application.module() {
Flyway.configure().dataSource(/*config to your DB*/).load().migrate()
//the rest of your application
routing {
}
}
Run Code Online (Sandbox Code Playgroud)
You could of course extract the creation of Flyway to your DI tool (e.g. Koin) and add some logging to show progress.
This way your DB will be migrated (if necessary) every time just before your app is started.
As for writing up the actual migration the official docs are very helpful. What you essentially need to do is:
src/main/resources/db/migration by default ).V,然后是一个数字,您将在每次新迁移时增加该数字,双下划线- 这在开始时欺骗了我;) - 以及蛇形描述,例如V1__Create_person_table.sql)数据库表在部署时通过 Flyway 进行迁移,并且可以在 db/migrations 文件夹中添加脚本来添加新表或执行查询,例如在服务器启动时插入数据等。(https://github.com/arun0009/kotlin-ktor-exposed-sample-api)
Flyway 的工作原理如下: https: //flywaydb.org/getstarted/how
V<version number>__<migration description>并运行flyway migrateR__<migration description>并运行flyway migrateV<version number>__<migration description>并运行flyway migrateflyway info提交您的文件(如果您满意)| 归档时间: |
|
| 查看次数: |
3851 次 |
| 最近记录: |