Compare commits
No commits in common. "main" and "v1.5.9" have entirely different histories.
@ -1,11 +1,5 @@
|
||||
# 变更记录 - @go/db
|
||||
|
||||
## [1.5.11] - 2026-08-22
|
||||
- SQLite schema 同步支持删除定义中已经移除的字段,并同步处理影子删除表。
|
||||
|
||||
## [1.5.10] - 2026-08-13
|
||||
- 修复 schema 同步中的字段转义解析。
|
||||
|
||||
## [1.5.9] - 2026-07-18
|
||||
- **依赖校验修复**: 升级 `redis` 至 `v1.5.11`,并将 `redigo` 固定为稳定版 `v1.9.3`,移除撤回版本。
|
||||
- **依赖对齐**: 升级 `cast` 至 `v1.5.4`、`crypto` 至 `v1.5.4`、`encoding` 至 `v1.5.5`、`id` 至 `v1.5.6`。
|
||||
|
||||
48
Schema.go
48
Schema.go
@ -5,7 +5,6 @@ import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"apigo.cc/go/cast"
|
||||
@ -20,39 +19,6 @@ type SchemaGroup struct {
|
||||
var fieldSpliter = regexp.MustCompile(`\s+`)
|
||||
var wnMatcher = regexp.MustCompile(`^([a-zA-Z]+)([0-9]+)$`)
|
||||
|
||||
func splitSchemaField(line string) []string {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
return nil
|
||||
}
|
||||
if line[0] != '"' {
|
||||
return fieldSpliter.Split(line, 10)
|
||||
}
|
||||
for i, escaped := 1, false; i < len(line); i++ {
|
||||
if escaped {
|
||||
escaped = false
|
||||
continue
|
||||
}
|
||||
if line[i] == '\\' {
|
||||
escaped = true
|
||||
continue
|
||||
}
|
||||
if line[i] == '"' {
|
||||
name, err := strconv.Unquote(line[:i+1])
|
||||
if err != nil {
|
||||
return fieldSpliter.Split(line, 10)
|
||||
}
|
||||
rest := strings.TrimSpace(line[i+1:])
|
||||
parts := []string{name}
|
||||
if rest != "" {
|
||||
parts = append(parts, fieldSpliter.Split(rest, 9)...)
|
||||
}
|
||||
return parts
|
||||
}
|
||||
}
|
||||
return fieldSpliter.Split(line, 10)
|
||||
}
|
||||
|
||||
// ParseField 解析单行字段描述
|
||||
func ParseField(line string) TableField {
|
||||
lc := strings.SplitN(line, "//", 2)
|
||||
@ -62,7 +28,7 @@ func ParseField(line string) TableField {
|
||||
comment = strings.TrimSpace(lc[1])
|
||||
}
|
||||
|
||||
a := splitSchemaField(line)
|
||||
a := fieldSpliter.Split(line, 10)
|
||||
field := TableField{
|
||||
Name: a[0],
|
||||
Type: "",
|
||||
@ -343,6 +309,8 @@ func (db *DB) Sync(desc string) error {
|
||||
return outErr
|
||||
}
|
||||
|
||||
|
||||
|
||||
// CheckTable 检查并同步单个表结构
|
||||
func (db *DB) CheckTable(table *TableStruct) error {
|
||||
fieldSets := make([]string, 0)
|
||||
@ -581,9 +549,6 @@ 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
|
||||
}
|
||||
@ -649,12 +614,7 @@ func (db *DB) CheckTable(table *TableStruct) error {
|
||||
|
||||
for oldFieldName := range oldFields {
|
||||
if !newFieldExists[oldFieldName] {
|
||||
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 {
|
||||
if !strings.HasPrefix(db.Config.Type, "sqlite") && !isPostgres {
|
||||
actions = append(actions, "DROP COLUMN "+db.Quote(oldFieldName))
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,13 +8,6 @@ import (
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
func TestParseFieldQuotedName(t *testing.T) {
|
||||
field := db.ParseField(`"Display Name 中文" v255 I // label`)
|
||||
if field.Name != "Display Name 中文" || field.Type != "varchar(255)" || field.Index != "index" || field.Comment != "label" {
|
||||
t.Fatalf("unexpected quoted field: %#v", field)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchemaSync(t *testing.T) {
|
||||
dbPath := "test_schema.db"
|
||||
dbInst := db.GetDB("sqlite://"+dbPath, nil)
|
||||
@ -42,24 +35,6 @@ 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) {
|
||||
@ -82,3 +57,4 @@ func TestAutoDetectShadow(t *testing.T) {
|
||||
t.Fatal("Auto-detect shadow delete failed")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user