fix: sqlite schema sync logic and autoindex drop error

This commit is contained in:
AI Engineer 2026-05-14 21:12:10 +08:00
parent b2136e170e
commit ae534db085

View File

@ -424,34 +424,40 @@ func (db *DB) CheckTable(table *TableStruct) error {
tmpFields := []struct { tmpFields := []struct {
Name string Name string
Type string Type string
Notnull bool Notnull int
Dflt_value any Dflt_value any
Pk bool Pk int
}{} }{}
db.Query("PRAGMA table_info(" + db.Quote(table.Name) + ")").To(&tmpFields) if err := db.Query("PRAGMA table_info(" + db.Quote(table.Name) + ")").To(&tmpFields); err != nil {
return err
}
for _, f := range tmpFields { for _, f := range tmpFields {
oldFieldList = append(oldFieldList, &tableFieldDesc{ oldFieldList = append(oldFieldList, &tableFieldDesc{
Field: f.Name, Field: f.Name,
Type: f.Type, Type: f.Type,
Null: cast.If(f.Notnull, "NO", "YES"), Null: cast.If(f.Notnull != 0, "NO", "YES"),
Key: cast.If(f.Pk, "PRI", ""), Key: cast.If(f.Pk != 0, "PRI", ""),
Default: cast.String(f.Dflt_value), Default: cast.String(f.Dflt_value),
}) })
} }
tmpIndexes := []struct { tmpIndexes := []struct {
Name string Name string
Unique bool Unique int
Origin string Origin string
Partial int Partial int
}{} }{}
db.Query("PRAGMA index_list(" + db.Quote(table.Name) + ")").To(&tmpIndexes) if err := db.Query("PRAGMA index_list(" + db.Quote(table.Name) + ")").To(&tmpIndexes); err != nil {
return err
}
for _, i := range tmpIndexes { for _, i := range tmpIndexes {
tmpIndexInfo := []struct { tmpIndexInfo := []struct {
Name string Name string
Seqno int Seqno int
Cid int Cid int
}{} }{}
db.Query("PRAGMA index_info(" + db.Quote(i.Name) + ")").To(&tmpIndexInfo) if err := db.Query("PRAGMA index_info(" + db.Quote(i.Name) + ")").To(&tmpIndexInfo); err != nil {
return err
}
if len(tmpIndexInfo) > 0 { if len(tmpIndexInfo) > 0 {
oldIndexInfos = append(oldIndexInfos, &tableKeyDesc{ oldIndexInfos = append(oldIndexInfos, &tableKeyDesc{
Key_name: i.Name, Key_name: i.Name,
@ -515,6 +521,9 @@ func (db *DB) CheckTable(table *TableStruct) error {
for keyId := range oldIndexes { for keyId := range oldIndexes {
if keyId != "PRIMARY" && !isPostgres && strings.ToLower(keySetFields[keyId]) != strings.ToLower(oldIndexes[keyId]) { if keyId != "PRIMARY" && !isPostgres && strings.ToLower(keySetFields[keyId]) != strings.ToLower(oldIndexes[keyId]) {
if strings.HasPrefix(db.Config.Type, "sqlite") { if strings.HasPrefix(db.Config.Type, "sqlite") {
if strings.HasPrefix(keyId, "sqlite_autoindex_") {
continue
}
actions = append(actions, "DROP INDEX "+db.Quote(keyId)) actions = append(actions, "DROP INDEX "+db.Quote(keyId))
} else { } else {
actions = append(actions, "DROP KEY "+db.Quote(keyId)) actions = append(actions, "DROP KEY "+db.Quote(keyId))