This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user