refactor: use flowutils helpers instead of 'if err != nil'

This commit is contained in:
wispem-wantex 2025-12-12 21:52:59 -08:00
parent 6358d12d10
commit f74da53c37

View File

@ -10,6 +10,7 @@ import (
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
. "git.offline-twitter.com/offline-labs/gas-stack/pkg/flowutils"
"git.offline-twitter.com/offline-labs/gas-stack/pkg/textutils" "git.offline-twitter.com/offline-labs/gas-stack/pkg/textutils"
) )
@ -37,32 +38,20 @@ func SchemaFromDB(db *sqlx.DB) Schema {
ret := Schema{Tables: map[string]Table{}, Indexes: map[string]Index{}} ret := Schema{Tables: map[string]Table{}, Indexes: map[string]Index{}}
var tables []Table var tables []Table
err := db.Select(&tables, `select name, is_strict, is_without_rowid from tables`) PanicIf(db.Select(&tables, `select name, is_strict, is_without_rowid from tables`))
if err != nil {
panic(err)
}
for _, tbl := range tables { for _, tbl := range tables {
tbl.TypeName = textutils.SnakeToCamel(inflection.Singular(tbl.TableName)) tbl.TypeName = textutils.SnakeToCamel(inflection.Singular(tbl.TableName))
tbl.TypeIDName = tbl.TypeName + "ID" tbl.TypeIDName = tbl.TypeName + "ID"
tbl.VarName = strings.ToLower(string(tbl.TableName[0])) tbl.VarName = strings.ToLower(string(tbl.TableName[0]))
err = db.Select(&tbl.Columns, `select * from columns where table_name = ?`, tbl.TableName) PanicIf(db.Select(&tbl.Columns, `select * from columns where table_name = ?`, tbl.TableName))
if err != nil {
panic(err)
}
ret.Tables[tbl.TableName] = tbl ret.Tables[tbl.TableName] = tbl
} }
var indexes []Index var indexes []Index
err = db.Select(&indexes, `select index_name, table_name, is_unique from indexes`) PanicIf(db.Select(&indexes, `select index_name, table_name, is_unique from indexes`))
if err != nil {
panic(err)
}
for _, idx := range indexes { for _, idx := range indexes {
err = db.Select(&idx.Columns, `select column_name from index_columns where index_name = ? order by rank`, idx.Name) PanicIf(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 ret.Indexes[idx.Name] = idx
} }
return ret return ret