refactor(documents): rename document owner fields
This commit is contained in:
@@ -19,8 +19,8 @@ type SaDocumentTree struct {
|
||||
ParentID *uint `gorm:"index"`
|
||||
ParentIdentity *string `gorm:"type:char(36);index"`
|
||||
ParentScope string `gorm:"type:char(36);not null;default:'';uniqueIndex:uidx_sa_document_sibling,priority:2"`
|
||||
CreatedBy uint `gorm:"index;not null"`
|
||||
CreatedByIdentity string `gorm:"type:char(36);index"`
|
||||
OwnerID uint `gorm:"index;not null"`
|
||||
OwnerIdentity string `gorm:"type:char(36);index"`
|
||||
SourceInboxItemID *uint `gorm:"index"`
|
||||
SourceInboxItemIdentity *string `gorm:"type:char(36);index"`
|
||||
Kind string `gorm:"size:16;not null"`
|
||||
|
||||
@@ -82,7 +82,7 @@ func (m *SaDocumentTree) BeforeCreate(tx *gorm.DB) error {
|
||||
if err := resolveIdentity(tx, &SaProject{}, m.ProjectID, &m.ProjectIdentity); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := resolveIdentity(tx, &SaUser{}, m.CreatedBy, &m.CreatedByIdentity); err != nil {
|
||||
if err := resolveIdentity(tx, &SaUser{}, m.OwnerID, &m.OwnerIdentity); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := resolveOptionalIdentity(tx, &SaDocumentTree{}, m.ParentID, &m.ParentIdentity); err != nil {
|
||||
|
||||
@@ -44,5 +44,21 @@ func renameLegacyTables(database *gorm.DB) error {
|
||||
return fmt.Errorf("rename legacy table %s to %s: %w", table.legacy, table.current, err)
|
||||
}
|
||||
}
|
||||
if migrator.HasTable("sa_documents") {
|
||||
for _, column := range []struct {
|
||||
legacy string
|
||||
current string
|
||||
}{
|
||||
{legacy: "created_by", current: "owner_id"},
|
||||
{legacy: "created_by_identity", current: "owner_identity"},
|
||||
} {
|
||||
if !migrator.HasColumn("sa_documents", column.legacy) || migrator.HasColumn("sa_documents", column.current) {
|
||||
continue
|
||||
}
|
||||
if err := migrator.RenameColumn("sa_documents", column.legacy, column.current); err != nil {
|
||||
return fmt.Errorf("rename sa_documents column %s to %s: %w", column.legacy, column.current, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -87,6 +87,36 @@ func TestAutoMigrateRejectsAmbiguousLegacyAndCurrentTables(t *testing.T) {
|
||||
require.EqualError(t, err, "cannot rename legacy table senlin_agent_users: target table sa_users already exists")
|
||||
}
|
||||
|
||||
func TestRenameLegacyTablesRenamesDocumentOwnerColumns(t *testing.T) {
|
||||
database, err := gorm.Open(sqlite.Open(fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name())), &gorm.Config{})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, database.Exec(`
|
||||
CREATE TABLE sa_documents (
|
||||
id integer primary key,
|
||||
created_by integer not null,
|
||||
created_by_identity text
|
||||
)
|
||||
`).Error)
|
||||
require.NoError(t, database.Exec(`
|
||||
INSERT INTO sa_documents (id, created_by, created_by_identity)
|
||||
VALUES (1, 7, 'owner-identity')
|
||||
`).Error)
|
||||
|
||||
require.NoError(t, renameLegacyTables(database))
|
||||
require.False(t, database.Migrator().HasColumn("sa_documents", "created_by"))
|
||||
require.False(t, database.Migrator().HasColumn("sa_documents", "created_by_identity"))
|
||||
require.True(t, database.Migrator().HasColumn("sa_documents", "owner_id"))
|
||||
require.True(t, database.Migrator().HasColumn("sa_documents", "owner_identity"))
|
||||
|
||||
var stored struct {
|
||||
OwnerID uint
|
||||
OwnerIdentity string
|
||||
}
|
||||
require.NoError(t, database.Table("sa_documents").Where("id = ?", 1).Take(&stored).Error)
|
||||
require.Equal(t, uint(7), stored.OwnerID)
|
||||
require.Equal(t, "owner-identity", stored.OwnerIdentity)
|
||||
}
|
||||
|
||||
func projectIdentifiers(projects []SaProject) []string {
|
||||
result := make([]string, 0, len(projects))
|
||||
for _, project := range projects {
|
||||
|
||||
Reference in New Issue
Block a user