87 lines
2.2 KiB
Kotlin
87 lines
2.2 KiB
Kotlin
package app
|
|
|
|
import org.aikrai.vertx.db.annotation.TableField
|
|
import org.aikrai.vertx.db.annotation.TableId
|
|
import org.aikrai.vertx.db.annotation.TableName
|
|
import org.aikrai.vertx.gen.AnnotationMapping
|
|
import org.aikrai.vertx.gen.ColumnMapping
|
|
import org.aikrai.vertx.gen.SqlAnnotationMapper
|
|
import org.aikrai.vertx.gen.SqlMigrationGenerator
|
|
import org.reflections.Reflections
|
|
import kotlin.reflect.KClass
|
|
|
|
object GenerateMigration {
|
|
@JvmStatic
|
|
fun main(args: Array<String>) {
|
|
testMigrationGeneration()
|
|
}
|
|
|
|
/**
|
|
* 创建注解映射器
|
|
*/
|
|
private fun createMapper(): SqlAnnotationMapper {
|
|
val mapper = SqlAnnotationMapper()
|
|
|
|
// 设置表名映射
|
|
mapper.tableName = AnnotationMapping(
|
|
annotationClass = TableName::class,
|
|
propertyName = "value"
|
|
)
|
|
|
|
// 设置列名映射
|
|
mapper.addColumnMapping(
|
|
ColumnMapping(
|
|
nameMapping = AnnotationMapping(
|
|
annotationClass = TableField::class,
|
|
propertyName = "value"
|
|
),
|
|
typeMapping = AnnotationMapping(
|
|
annotationClass = TableField::class,
|
|
propertyName = "type"
|
|
),
|
|
nullableMapping = AnnotationMapping(
|
|
annotationClass = TableField::class,
|
|
propertyName = "nullable"
|
|
),
|
|
defaultValueMapping = AnnotationMapping(
|
|
annotationClass = TableField::class,
|
|
propertyName = "default"
|
|
)
|
|
)
|
|
)
|
|
|
|
// 设置主键映射
|
|
mapper.primaryKeyMapping = AnnotationMapping(
|
|
annotationClass = TableId::class,
|
|
propertyName = "value"
|
|
)
|
|
|
|
return mapper
|
|
}
|
|
|
|
/**
|
|
* 测试数据库迁移生成
|
|
*/
|
|
private fun testMigrationGeneration() {
|
|
// 创建注解映射器
|
|
val mapper = createMapper()
|
|
|
|
// 扫描实体类
|
|
val entityClasses = scanEntityClasses("app.example")
|
|
|
|
// 生成迁移文件
|
|
SqlMigrationGenerator.generateMigrations(entityClasses, mapper)
|
|
|
|
println("数据库迁移文件生成完成")
|
|
}
|
|
|
|
/**
|
|
* 扫描包路径下标记了@TableName注解的实体类
|
|
*/
|
|
private fun scanEntityClasses(packagePath: String): List<KClass<*>> {
|
|
val reflections = Reflections(packagePath)
|
|
val entityClasses = reflections.getTypesAnnotatedWith(TableName::class.java)
|
|
|
|
return entityClasses.map { it.kotlin }
|
|
}
|
|
} |