This commit is contained in:
@@ -449,7 +449,7 @@ CREATE TABLE IF NOT EXISTS `user_device`
|
||||
`subscribe_id` bigint DEFAULT NULL COMMENT 'Subscribe ID',
|
||||
`ip` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT 'Device Ip.',
|
||||
`Identifier` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT 'Device Identifier.',
|
||||
`user_agent` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT 'Device User Agent.',
|
||||
`user_agent` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT 'Device User Agent.',
|
||||
`online` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'Online',
|
||||
`enabled` tinyint(1) NOT NULL DEFAULT '1' COMMENT 'EnableDeviceNumber',
|
||||
`created_at` datetime(3) DEFAULT NULL COMMENT 'Creation Time',
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
ALTER TABLE `log_message`
|
||||
MODIFY COLUMN `app_version` VARCHAR(32) NULL,
|
||||
MODIFY COLUMN `os_name` VARCHAR(32) NULL,
|
||||
MODIFY COLUMN `os_version` VARCHAR(32) NULL,
|
||||
MODIFY COLUMN `device_id` VARCHAR(64) NULL,
|
||||
MODIFY COLUMN `session_id` VARCHAR(64) NULL,
|
||||
MODIFY COLUMN `error_code` VARCHAR(64) NULL;
|
||||
@@ -0,0 +1,7 @@
|
||||
ALTER TABLE `log_message`
|
||||
MODIFY COLUMN `app_version` VARCHAR(64) NULL,
|
||||
MODIFY COLUMN `os_name` VARCHAR(64) NULL,
|
||||
MODIFY COLUMN `os_version` VARCHAR(64) NULL,
|
||||
MODIFY COLUMN `device_id` VARCHAR(255) NULL,
|
||||
MODIFY COLUMN `session_id` VARCHAR(255) NULL,
|
||||
MODIFY COLUMN `error_code` VARCHAR(128) NULL;
|
||||
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE `user_device`
|
||||
MODIFY COLUMN `user_agent` VARCHAR(64) NULL COMMENT 'Device User Agent.';
|
||||
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE `user_device`
|
||||
MODIFY COLUMN `user_agent` VARCHAR(255) NULL COMMENT 'Device User Agent.';
|
||||
@@ -20,6 +20,14 @@ type schemaColumnPatch struct {
|
||||
ddl string
|
||||
}
|
||||
|
||||
type schemaColumnDefinitionPatch struct {
|
||||
table string
|
||||
column string
|
||||
dataType string
|
||||
characterMaxLen *int64
|
||||
ddl string
|
||||
}
|
||||
|
||||
func EnsureSchemaCompatibility(ctx *svc.ServiceContext) error {
|
||||
tablePatches := []schemaTablePatch{
|
||||
{
|
||||
@@ -142,6 +150,17 @@ func EnsureSchemaCompatibility(ctx *svc.ServiceContext) error {
|
||||
},
|
||||
}
|
||||
|
||||
varchar255 := int64(255)
|
||||
columnDefinitionPatches := []schemaColumnDefinitionPatch{
|
||||
{
|
||||
table: "user_device",
|
||||
column: "user_agent",
|
||||
dataType: "varchar",
|
||||
characterMaxLen: &varchar255,
|
||||
ddl: "ALTER TABLE `user_device` MODIFY COLUMN `user_agent` VARCHAR(255) NULL COMMENT 'Device User Agent.';",
|
||||
},
|
||||
}
|
||||
|
||||
for _, patch := range tablePatches {
|
||||
exists, err := tableExists(ctx.DB, patch.table)
|
||||
if err != nil {
|
||||
@@ -199,6 +218,27 @@ func EnsureSchemaCompatibility(ctx *svc.ServiceContext) error {
|
||||
logger.Infof("[SchemaCompat] created missing index: %s.%s", patch.table, patch.index)
|
||||
}
|
||||
|
||||
for _, patch := range columnDefinitionPatches {
|
||||
tblExists, err := tableExists(ctx.DB, patch.table)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "check table %s failed", patch.table)
|
||||
}
|
||||
if !tblExists {
|
||||
continue
|
||||
}
|
||||
matches, err := columnDefinitionMatches(ctx.DB, patch.table, patch.column, patch.dataType, patch.characterMaxLen)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "check column definition %s.%s failed", patch.table, patch.column)
|
||||
}
|
||||
if matches {
|
||||
continue
|
||||
}
|
||||
if err = ctx.DB.Exec(patch.ddl).Error; err != nil {
|
||||
return errors.Wrapf(err, "modify column %s.%s failed", patch.table, patch.column)
|
||||
}
|
||||
logger.Infof("[SchemaCompat] repaired column definition: %s.%s", patch.table, patch.column)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -237,6 +277,38 @@ func indexExists(db *gorm.DB, table, index string) (bool, error) {
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
func columnDefinitionMatches(db *gorm.DB, table, column, dataType string, characterMaxLen *int64) (bool, error) {
|
||||
type columnMeta struct {
|
||||
DataType string
|
||||
CharacterMaximumLen *int64
|
||||
}
|
||||
|
||||
var meta columnMeta
|
||||
err := db.Raw(
|
||||
`SELECT DATA_TYPE AS data_type, CHARACTER_MAXIMUM_LENGTH AS character_maximum_len
|
||||
FROM information_schema.COLUMNS
|
||||
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND COLUMN_NAME = ?`,
|
||||
table,
|
||||
column,
|
||||
).Scan(&meta).Error
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if meta.DataType == "" {
|
||||
return false, nil
|
||||
}
|
||||
if meta.DataType != dataType {
|
||||
return false, nil
|
||||
}
|
||||
if characterMaxLen == nil {
|
||||
return true, nil
|
||||
}
|
||||
if meta.CharacterMaximumLen == nil {
|
||||
return false, nil
|
||||
}
|
||||
return *meta.CharacterMaximumLen == *characterMaxLen, nil
|
||||
}
|
||||
|
||||
func _schemaCompatDebug(table, column string) string {
|
||||
if column == "" {
|
||||
return table
|
||||
|
||||
Reference in New Issue
Block a user