Add parsing of schema indexes
All checks were successful
CI / release-test (push) Successful in 12s

This commit is contained in:
2025-08-23 16:55:02 -07:00
parent e7f9934f03
commit 8245f89305
5 changed files with 71 additions and 9 deletions

View File

@@ -24,24 +24,36 @@ func InitDB(sql_schema string) *sqlx.DB {
// SchemaFromDB takes a DB connection, checks its schema metadata tables, and returns a Schema.
func SchemaFromDB(db *sqlx.DB) Schema {
ret := Schema{}
ret := Schema{Tables: map[string]Table{}, Indexes: map[string]Index{}}
var tables []Table
err := db.Select(&tables, `select name, is_strict, is_without_rowid from tables`)
if err != nil {
panic(err)
}
for _, tbl := range tables {
tbl.TypeName = textutils.SnakeToCamel(inflection.Singular(tbl.TableName))
tbl.TypeIDName = tbl.TypeName + "ID"
tbl.VarName = strings.ToLower(string(tbl.TableName[0]))
err := db.Select(&tbl.Columns, `select * from columns where table_name = ?`, tbl.TableName)
err = db.Select(&tbl.Columns, `select * from columns where table_name = ?`, tbl.TableName)
if err != nil {
panic(err)
}
ret[tbl.TableName] = tbl
ret.Tables[tbl.TableName] = tbl
}
var indexes []Index
err = db.Select(&indexes, `select index_name, table_name, is_unique from indexes`)
if err != nil {
panic(err)
}
for _, idx := range indexes {
err = db.Select(&idx.Columns, `select column_name from index_columns where index_name = ? order by rank`, idx.Name)
if err != nil {
panic(err)
}
ret.Indexes[idx.Name] = idx
}
return ret
}