1
This commit is contained in:
parent
4f31d1517f
commit
530f682f17
@ -11,6 +11,7 @@ import java.sql.Timestamp
|
||||
class Account : BaseEntity() {
|
||||
|
||||
@TableId(type = IdType.ASSIGN_ID)
|
||||
@TableFieldComment("用户ID")
|
||||
var userId: Long = 0L
|
||||
|
||||
@TableField("user_name")
|
||||
|
||||
@ -5,6 +5,7 @@ import org.aikrai.vertx.db.annotation.TableId
|
||||
import org.aikrai.vertx.db.annotation.TableName
|
||||
import org.aikrai.vertx.db.annotation.TableIndex
|
||||
import org.aikrai.vertx.db.annotation.EnumValue
|
||||
import org.aikrai.vertx.db.annotation.TableFieldComment
|
||||
import org.aikrai.vertx.db.migration.AnnotationMapping
|
||||
import org.aikrai.vertx.db.migration.ColumnMapping
|
||||
import org.aikrai.vertx.db.migration.DbMigration
|
||||
@ -38,6 +39,7 @@ object GenerateMigration {
|
||||
|
||||
// 设置迁移生成器
|
||||
val dbMigration = DbMigration.create()
|
||||
dbMigration.setPathToResources("vertx-demo/src/main/resources")
|
||||
dbMigration.setEntityPackage("app.data.domain") // 指定实体类包路径
|
||||
dbMigration.setSqlAnnotationMapper(mapper)
|
||||
dbMigration.setGenerateDropStatements(false) // 不生成删除语句
|
||||
@ -61,7 +63,6 @@ object GenerateMigration {
|
||||
// 设置实体类注解映射
|
||||
mapper.entityMapping = AnnotationMapping(
|
||||
annotationClass = TableName::class,
|
||||
propertyName = "value"
|
||||
)
|
||||
|
||||
// 设置表名映射
|
||||
@ -96,6 +97,10 @@ object GenerateMigration {
|
||||
uniqueMapping = AnnotationMapping(
|
||||
annotationClass = TableField::class,
|
||||
propertyName = "unique"
|
||||
),
|
||||
commentMapping = AnnotationMapping(
|
||||
annotationClass = TableFieldComment::class,
|
||||
propertyName = "value"
|
||||
)
|
||||
)
|
||||
)
|
||||
@ -103,7 +108,6 @@ object GenerateMigration {
|
||||
// 设置主键映射
|
||||
mapper.primaryKeyMapping = AnnotationMapping(
|
||||
annotationClass = TableId::class,
|
||||
propertyName = "value"
|
||||
)
|
||||
|
||||
// 设置索引映射
|
||||
|
||||
@ -52,6 +52,13 @@ annotation class TableField(
|
||||
val default: String = ""
|
||||
)
|
||||
|
||||
@MustBeDocumented
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
@Target(AnnotationTarget.FIELD, AnnotationTarget.ANNOTATION_CLASS)
|
||||
annotation class TableFieldComment(
|
||||
val value: String = "",
|
||||
)
|
||||
|
||||
@MustBeDocumented
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
@Target(AnnotationTarget.FIELD, AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget.FUNCTION)
|
||||
|
||||
@ -159,6 +159,10 @@ class DefaultDbMigration : DbMigration {
|
||||
if (sqlAnnotationMapper == null) {
|
||||
throw IllegalStateException("SQL注解映射器未设置,请调用setSqlAnnotationMapper()")
|
||||
}
|
||||
|
||||
if (sqlAnnotationMapper?.entityMapping == null) {
|
||||
throw IllegalStateException("实体注解映射未设置,请配置entityMapping")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -66,7 +66,7 @@ data class AnnotationMapping(
|
||||
/** 注解类 */
|
||||
val annotationClass: KClass<out Annotation>,
|
||||
/** 注解属性名 */
|
||||
val propertyName: String = "value"
|
||||
val propertyName: String = ""
|
||||
)
|
||||
|
||||
/**
|
||||
@ -84,5 +84,7 @@ data class ColumnMapping(
|
||||
/** 字段长度映射,可选 */
|
||||
val lengthMapping: AnnotationMapping? = null,
|
||||
/** 是否唯一映射,可选 */
|
||||
val uniqueMapping: AnnotationMapping? = null
|
||||
val uniqueMapping: AnnotationMapping? = null,
|
||||
/** 字段注释映射,可选 */
|
||||
val commentMapping: AnnotationMapping? = null
|
||||
)
|
||||
File diff suppressed because it is too large
Load Diff
@ -29,27 +29,39 @@ class SqlGenerator {
|
||||
val columnDefinitions = columns.map { column ->
|
||||
// 特殊处理一些常见约定字段
|
||||
val specialFieldDefaults = getSpecialFieldDefaults(column.name, column.type)
|
||||
val defaultValue = if (column.defaultValue.isNotEmpty()) {
|
||||
var defaultValue = ""
|
||||
|
||||
// 处理默认值
|
||||
if (column.defaultValue.isNotEmpty()) {
|
||||
// 对于时间戳类型字段,特殊处理NOW()函数作为默认值
|
||||
if (column.type.contains("TIMESTAMP") && column.defaultValue.equals("now()", ignoreCase = true)) {
|
||||
" DEFAULT 'now()'"
|
||||
defaultValue = " DEFAULT 'now()'"
|
||||
} else if (column.enumValues != null && column.enumValues!!.isNotEmpty()) {
|
||||
// 对于枚举类型字段,直接使用默认值
|
||||
// 如果是数字,就不带引号,否则加上引号
|
||||
val isNumeric = column.defaultValue.matches(Regex("^[0-9]+$"))
|
||||
if (isNumeric) {
|
||||
defaultValue = " DEFAULT ${column.defaultValue}"
|
||||
} else {
|
||||
defaultValue = " DEFAULT '${column.defaultValue}'"
|
||||
}
|
||||
} else {
|
||||
" DEFAULT ${column.defaultValue}"
|
||||
defaultValue = " DEFAULT ${column.defaultValue}"
|
||||
}
|
||||
} else if (specialFieldDefaults.isNotEmpty()) {
|
||||
specialFieldDefaults
|
||||
defaultValue = specialFieldDefaults
|
||||
} else {
|
||||
// 为一些常见类型提供合理的默认值
|
||||
when {
|
||||
column.type.contains("VARCHAR") -> " DEFAULT ''"
|
||||
column.type == "INTEGER" || column.type == "BIGINT" -> " DEFAULT 0"
|
||||
column.type == "BOOLEAN" -> " DEFAULT false"
|
||||
column.type.contains("JSON") -> " DEFAULT '{}'"
|
||||
else -> ""
|
||||
column.type.contains("VARCHAR") -> defaultValue = " DEFAULT ''"
|
||||
column.type == "INTEGER" || column.type == "BIGINT" -> defaultValue = " DEFAULT 0"
|
||||
column.type == "BOOLEAN" -> defaultValue = " DEFAULT false"
|
||||
column.type.contains("JSON") -> defaultValue = " DEFAULT '{}'"
|
||||
}
|
||||
}
|
||||
|
||||
val nullable = if (column.nullable) "" else " NOT NULL"
|
||||
// 移除内联注释,改用COMMENT ON语句
|
||||
" ${column.name} ${column.type}$defaultValue$nullable"
|
||||
}
|
||||
|
||||
@ -65,8 +77,17 @@ class SqlGenerator {
|
||||
val enumColumns = columns.filter { it.type.contains("VARCHAR") && it.enumValues != null && it.enumValues!!.isNotEmpty() }
|
||||
for (column in enumColumns) {
|
||||
if (column.enumValues != null && column.enumValues!!.isNotEmpty()) {
|
||||
// 检查枚举值是否都是数字
|
||||
val allNumeric = column.enumValues!!.all { it.matches(Regex("^[0-9]+$")) }
|
||||
|
||||
sb.append(",\n CONSTRAINT ck_${tableName}_${column.name} CHECK ( ${column.name} in (")
|
||||
sb.append(column.enumValues!!.joinToString(",") { "'$it'" })
|
||||
if (allNumeric) {
|
||||
// 如果全是数字,不需要加引号
|
||||
sb.append(column.enumValues!!.joinToString(","))
|
||||
} else {
|
||||
// 否则加上引号
|
||||
sb.append(column.enumValues!!.joinToString(",") { "'$it'" })
|
||||
}
|
||||
sb.append("))")
|
||||
}
|
||||
}
|
||||
@ -78,6 +99,16 @@ class SqlGenerator {
|
||||
|
||||
sb.append("\n);")
|
||||
|
||||
// 添加字段注释 - 使用PostgreSQL的COMMENT ON语句
|
||||
val columnsWithComment = columns.filter { it.comment.isNotEmpty() }
|
||||
if (columnsWithComment.isNotEmpty()) {
|
||||
sb.append("\n\n-- 添加字段注释\n")
|
||||
for (column in columnsWithComment) {
|
||||
sb.append("COMMENT ON COLUMN ${tableName}.${column.name} IS '${column.comment.replace("'", "''")}';")
|
||||
sb.append("\n")
|
||||
}
|
||||
}
|
||||
|
||||
// 添加索引创建语句
|
||||
val indexSql = generateCreateIndexSql(sqlInfo)
|
||||
if (indexSql.isNotEmpty()) {
|
||||
@ -151,7 +182,13 @@ class SqlGenerator {
|
||||
}
|
||||
// 状态字段
|
||||
fieldName == "status" || fieldName == "state" -> {
|
||||
if (fieldType.contains("VARCHAR")) " DEFAULT 'NORMAL'" else ""
|
||||
if (fieldType.contains("VARCHAR")) {
|
||||
// 默认为空值,实际值将通过字段的默认值处理
|
||||
""
|
||||
} else {
|
||||
// 对于数字类型状态默认为0
|
||||
" DEFAULT 0"
|
||||
}
|
||||
}
|
||||
else -> ""
|
||||
}
|
||||
|
||||
@ -22,7 +22,8 @@ data class ColumnInfo(
|
||||
var isPrimaryKey: Boolean = false,
|
||||
var enumValues: List<String>? = null,
|
||||
var unique: Boolean = false,
|
||||
var length: Int = 255
|
||||
var length: Int = 255,
|
||||
var comment: String = ""
|
||||
)
|
||||
|
||||
/**
|
||||
|
||||
@ -96,7 +96,7 @@ class SqlMigrationGenerator {
|
||||
createDirectories()
|
||||
|
||||
log("开始扫描实体类...")
|
||||
val entityClasses = scanEntityClasses(entityPackage)
|
||||
val entityClasses = scanEntityClasses(mapper, entityPackage)
|
||||
|
||||
if (entityClasses.isEmpty()) {
|
||||
throw IllegalArgumentException("未找到任何实体类,请检查包路径: $entityPackage")
|
||||
@ -156,9 +156,11 @@ class SqlMigrationGenerator {
|
||||
/**
|
||||
* 扫描包路径下标记了@TableName注解的实体类
|
||||
*/
|
||||
private fun scanEntityClasses(packagePath: String): List<KClass<*>> {
|
||||
private fun scanEntityClasses(mapper: SqlAnnotationMapper, packagePath: String): List<KClass<*>> {
|
||||
val entityAnnotationClass = mapper.entityMapping?.annotationClass?.java
|
||||
?: throw IllegalStateException("实体类注解映射未设置,请配置SqlAnnotationMapper.entityMapping")
|
||||
val reflections = Reflections(packagePath)
|
||||
val entityClasses = reflections.getTypesAnnotatedWith(TableName::class.java)
|
||||
val entityClasses = reflections.getTypesAnnotatedWith(entityAnnotationClass)
|
||||
return entityClasses.map { it.kotlin }
|
||||
}
|
||||
|
||||
@ -477,17 +479,23 @@ class SqlMigrationGenerator {
|
||||
doc: Document,
|
||||
changeSetElement: Element
|
||||
) {
|
||||
tablesToDrop.forEach { tableName ->
|
||||
// 生成SQL
|
||||
sqlBuilder.append("drop table if exists $tableName cascade;\n")
|
||||
// 检查是否需要生成删除语句
|
||||
val generateDropStatements = System.getProperty("ddl.migration.generateDropStatements", "false").toBoolean()
|
||||
|
||||
// 添加到XML
|
||||
tablesToDrop.forEach { tableName ->
|
||||
// 只有当配置为生成删除语句时才添加到SQL中
|
||||
if (generateDropStatements) {
|
||||
// 生成SQL
|
||||
sqlBuilder.append("drop table if exists $tableName cascade;\n")
|
||||
}
|
||||
|
||||
// 无论如何都添加到XML中,以记录历史变更
|
||||
val dropTableElement = doc.createElement("dropTable")
|
||||
dropTableElement.setAttribute("name", tableName)
|
||||
changeSetElement.appendChild(dropTableElement)
|
||||
}
|
||||
|
||||
if (tablesToDrop.isNotEmpty()) {
|
||||
if (generateDropStatements && tablesToDrop.isNotEmpty()) {
|
||||
sqlBuilder.append("\n")
|
||||
}
|
||||
}
|
||||
@ -543,22 +551,33 @@ class SqlMigrationGenerator {
|
||||
doc: Document,
|
||||
changeSetElement: Element
|
||||
) {
|
||||
// 检查是否需要生成删除语句
|
||||
val generateDropStatements = System.getProperty("ddl.migration.generateDropStatements", "false").toBoolean()
|
||||
|
||||
var addedSql = false
|
||||
|
||||
columnsToDrop.forEach { (tableName, columns) ->
|
||||
// 为每个表创建一个dropColumn元素
|
||||
val dropColumnElement = doc.createElement("dropColumn")
|
||||
dropColumnElement.setAttribute("tableName", tableName)
|
||||
changeSetElement.appendChild(dropColumnElement)
|
||||
if (columns.isNotEmpty()) {
|
||||
val alterTableElement = doc.createElement("alterTable")
|
||||
alterTableElement.setAttribute("name", tableName)
|
||||
changeSetElement.appendChild(alterTableElement)
|
||||
|
||||
columns.forEach { columnName ->
|
||||
// 生成SQL
|
||||
sqlBuilder.append("alter table $tableName drop column if exists $columnName;\n")
|
||||
columns.forEach { columnName ->
|
||||
// 只有当配置为生成删除语句时才添加到SQL中
|
||||
if (generateDropStatements) {
|
||||
sqlBuilder.append("alter table $tableName drop column if exists $columnName;\n")
|
||||
addedSql = true
|
||||
}
|
||||
|
||||
// 添加到XML
|
||||
val columnElement = doc.createElement("column")
|
||||
columnElement.setAttribute("name", columnName)
|
||||
dropColumnElement.appendChild(columnElement)
|
||||
// 无论如何都添加到XML中,以记录历史变更
|
||||
val dropColumnElement = doc.createElement("dropColumn")
|
||||
dropColumnElement.setAttribute("columnName", columnName)
|
||||
alterTableElement.appendChild(dropColumnElement)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (addedSql) {
|
||||
sqlBuilder.append("\n")
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user