fix(db): 支持SQLite业务字段同步删除(by AI)
This commit is contained in:
parent
8031e5bf23
commit
7c7043ac0b
@ -1,5 +1,8 @@
|
||||
# 变更记录 - @go/db
|
||||
|
||||
## [1.5.11] - 2026-08-22
|
||||
- SQLite schema 同步支持删除定义中已经移除的字段,并同步处理影子删除表。
|
||||
|
||||
## [1.5.10] - 2026-08-13
|
||||
- 修复 schema 同步中的字段转义解析。
|
||||
|
||||
|
||||
10
Schema.go
10
Schema.go
@ -581,6 +581,9 @@ func (db *DB) CheckTable(table *TableStruct) error {
|
||||
for keyId := range oldIndexes {
|
||||
if keyId != "PRIMARY" && !isPostgres && strings.ToLower(keySetFields[keyId]) != strings.ToLower(oldIndexes[keyId]) {
|
||||
if strings.HasPrefix(db.Config.Type, "sqlite") {
|
||||
if table.Name == "_Table" || table.Name == "_Field" {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(keyId, "sqlite_autoindex_") {
|
||||
continue
|
||||
}
|
||||
@ -646,7 +649,12 @@ func (db *DB) CheckTable(table *TableStruct) error {
|
||||
|
||||
for oldFieldName := range oldFields {
|
||||
if !newFieldExists[oldFieldName] {
|
||||
if !strings.HasPrefix(db.Config.Type, "sqlite") && !isPostgres {
|
||||
if strings.HasPrefix(db.Config.Type, "sqlite") {
|
||||
if table.Name == "_Table" || table.Name == "_Field" {
|
||||
continue
|
||||
}
|
||||
actions = append(actions, "ALTER TABLE "+db.Quote(table.Name)+" DROP COLUMN "+db.Quote(oldFieldName))
|
||||
} else if !isPostgres {
|
||||
actions = append(actions, "DROP COLUMN "+db.Quote(oldFieldName))
|
||||
}
|
||||
}
|
||||
|
||||
@ -42,6 +42,24 @@ test_table SD // Test table with shadow delete
|
||||
if res.IntOnR1C1() != 1 {
|
||||
t.Fatal("Shadow delete failed")
|
||||
}
|
||||
|
||||
withoutStatus := `== Default ==
|
||||
test_table SD // Test table with shadow delete
|
||||
id AI // ID
|
||||
name v50 U
|
||||
autoVersion ubi // Version
|
||||
`
|
||||
if err := dbInst.Sync(withoutStatus); err != nil {
|
||||
t.Fatal("Sync removed field:", err)
|
||||
}
|
||||
for _, tableName := range []string{"test_table", "test_table_deleted"} {
|
||||
fields := dbInst.Query("PRAGMA table_info(" + dbInst.Quote(tableName) + ")").MapResults()
|
||||
for _, field := range fields {
|
||||
if field["name"] == "status" {
|
||||
t.Fatalf("removed field remains in physical table %s", tableName)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoDetectShadow(t *testing.T) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user