refactor: shorten model and table prefixes
This commit is contained in:
@@ -9,16 +9,16 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// SenlinAgentSchemaMigration 记录已完成的数据修复及审计摘要,确保升级可重入且可追溯。
|
||||
type SenlinAgentSchemaMigration struct {
|
||||
// SaSchemaMigration 记录已完成的数据修复及审计摘要,确保升级可重入且可追溯。
|
||||
type SaSchemaMigration struct {
|
||||
Version int `gorm:"primaryKey;autoIncrement:false"`
|
||||
Name string `gorm:"not null"`
|
||||
Details string `gorm:"type:text;not null"`
|
||||
AppliedAt time.Time `gorm:"not null"`
|
||||
}
|
||||
|
||||
func (SenlinAgentSchemaMigration) TableName() string {
|
||||
return "senlin_agent_schema_migrations"
|
||||
func (SaSchemaMigration) TableName() string {
|
||||
return "sa_schema_migrations"
|
||||
}
|
||||
|
||||
type versionedMigration struct {
|
||||
@@ -38,7 +38,7 @@ func runVersionedMigrations(database *gorm.DB) error {
|
||||
// version table are not inherited by the data-repair transaction.
|
||||
migrationDatabase := database.Session(&gorm.Session{NewDB: true})
|
||||
var applied int64
|
||||
if err := migrationDatabase.Model(&SenlinAgentSchemaMigration{}).Where("version = ?", migration.version).Count(&applied).Error; err != nil {
|
||||
if err := migrationDatabase.Model(&SaSchemaMigration{}).Where("version = ?", migration.version).Count(&applied).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if applied > 0 {
|
||||
@@ -53,7 +53,7 @@ func runVersionedMigrations(database *gorm.DB) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Create(&SenlinAgentSchemaMigration{
|
||||
return tx.Create(&SaSchemaMigration{
|
||||
Version: migration.version, Name: migration.name, Details: string(encoded), AppliedAt: time.Now().UTC(),
|
||||
}).Error
|
||||
}); err != nil {
|
||||
@@ -64,8 +64,8 @@ func runVersionedMigrations(database *gorm.DB) error {
|
||||
}
|
||||
|
||||
func normalizeLegacyProjectIdentifiers(tx *gorm.DB) (map[string]int, error) {
|
||||
var projects []SenlinAgentProject
|
||||
if err := tx.Model(&SenlinAgentProject{}).Select("id", "owner_id", "identifier").Order("owner_id asc, id asc").Find(&projects).Error; err != nil {
|
||||
var projects []SaProject
|
||||
if err := tx.Model(&SaProject{}).Select("id", "owner_id", "identifier").Order("owner_id asc, id asc").Find(&projects).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -102,12 +102,12 @@ func normalizeLegacyProjectIdentifiers(tx *gorm.DB) (map[string]int, error) {
|
||||
if candidate == project.Identifier {
|
||||
continue
|
||||
}
|
||||
if err := tx.Model(&SenlinAgentProject{}).Where("id = ?", project.ID).Update("identifier", candidate).Error; err != nil {
|
||||
if err := tx.Model(&SaProject{}).Where("id = ?", project.ID).Update("identifier", candidate).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
updated++
|
||||
}
|
||||
if err := tx.Exec(`CREATE UNIQUE INDEX IF NOT EXISTS uidx_senlin_agent_projects_owner_identifier ON senlin_agent_projects (owner_id, identifier)`).Error; err != nil {
|
||||
if err := tx.Exec(`CREATE UNIQUE INDEX IF NOT EXISTS uidx_sa_projects_owner_identifier ON sa_projects (owner_id, identifier)`).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]int{"audited": len(projects), "updated": updated}, nil
|
||||
@@ -124,11 +124,11 @@ func uniqueIdentifier(base string, reserved map[string]struct{}) string {
|
||||
}
|
||||
|
||||
func deduplicateLegacyProjectTags(tx *gorm.DB) (map[string]int, error) {
|
||||
var tags []SenlinAgentTag
|
||||
if err := tx.Model(&SenlinAgentTag{}).Select("id", "identity", "project_id", "name").Order("project_id asc, name asc, id asc").Find(&tags).Error; err != nil {
|
||||
var tags []SaTag
|
||||
if err := tx.Model(&SaTag{}).Select("id", "identity", "project_id", "name").Order("project_id asc, name asc, id asc").Find(&tags).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
keepers := make(map[string]SenlinAgentTag)
|
||||
keepers := make(map[string]SaTag)
|
||||
deduplicated := 0
|
||||
for _, tag := range tags {
|
||||
key := fmt.Sprintf("%d\x00%s", tag.ProjectID, tag.Name)
|
||||
@@ -137,17 +137,17 @@ func deduplicateLegacyProjectTags(tx *gorm.DB) (map[string]int, error) {
|
||||
keepers[key] = tag
|
||||
continue
|
||||
}
|
||||
if err := tx.Model(&SenlinAgentTask{}).Where("tag_id = ?", tag.ID).Updates(map[string]any{
|
||||
if err := tx.Model(&SaTask{}).Where("tag_id = ?", tag.ID).Updates(map[string]any{
|
||||
"tag_id": keeper.ID, "tag_identity": keeper.Identity,
|
||||
}).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := tx.Where("id = ?", tag.ID).Delete(&SenlinAgentTag{}).Error; err != nil {
|
||||
if err := tx.Where("id = ?", tag.ID).Delete(&SaTag{}).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
deduplicated++
|
||||
}
|
||||
if err := tx.Exec(`CREATE UNIQUE INDEX IF NOT EXISTS uidx_senlin_agent_tags_project_name ON senlin_agent_tags (project_id, name)`).Error; err != nil {
|
||||
if err := tx.Exec(`CREATE UNIQUE INDEX IF NOT EXISTS uidx_sa_tags_project_name ON sa_tags (project_id, name)`).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]int{"audited": len(tags), "deduplicated": deduplicated}, nil
|
||||
|
||||
Reference in New Issue
Block a user