fix(db): 支持SQLite业务字段同步删除(by AI)

This commit is contained in:
Star 2026-08-22 18:23:16 +08:00
parent 8031e5bf23
commit 7c7043ac0b
4 changed files with 34 additions and 1 deletions

View File

@ -1,5 +1,8 @@
# 变更记录 - @go/db
## [1.5.11] - 2026-08-22
- SQLite schema 同步支持删除定义中已经移除的字段,并同步处理影子删除表。
## [1.5.10] - 2026-08-13
- 修复 schema 同步中的字段转义解析。

View File

@ -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))
}
}

View File

@ -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) {

View File

@ -1,5 +1,9 @@
# @go/db 测试报告
## v1.5.11 验证
- `TestSchemaSync` 覆盖 SQLite 主表与影子删除表的字段移除。
- `go test -v ./...``go test -bench=. ./...` 通过。
## v1.5.9 验证
- `go list -m all`:模块图使用 `redis v1.5.11` 与稳定版 `redigo v1.9.3`
- `go test -v ./...`:通过完整测试。