Compare commits
2 Commits
6358d12d10
...
89db9e14e2
| Author | SHA1 | Date | |
|---|---|---|---|
| 89db9e14e2 | |||
| f74da53c37 |
@ -10,6 +10,7 @@ import (
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "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"
|
||||
)
|
||||
|
||||
@ -37,32 +38,20 @@ func SchemaFromDB(db *sqlx.DB) 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)
|
||||
}
|
||||
PanicIf(db.Select(&tables, `select name, is_strict, is_without_rowid from tables`))
|
||||
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)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
PanicIf(db.Select(&tbl.Columns, `select * from columns where table_name = ?`, tbl.TableName))
|
||||
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)
|
||||
}
|
||||
PanicIf(db.Select(&indexes, `select index_name, table_name, is_unique from indexes`))
|
||||
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)
|
||||
}
|
||||
PanicIf(db.Select(&idx.Columns, `select column_name from index_columns where index_name = ? order by rank`, idx.Name))
|
||||
ret.Indexes[idx.Name] = idx
|
||||
}
|
||||
return ret
|
||||
|
||||
@ -39,6 +39,7 @@ type Index struct {
|
||||
Columns []string
|
||||
IsUnique bool `db:"is_unique"`
|
||||
// TODO: `where ...` for partial indexes
|
||||
// TODO: identify columns that are expressions
|
||||
}
|
||||
|
||||
type Schema struct {
|
||||
|
||||
@ -33,7 +33,8 @@ create temporary view indexes as
|
||||
|
||||
create temporary view index_columns as
|
||||
select indexes.index_name,
|
||||
idx_cols.name as column_name,
|
||||
idx_cols.cid as column_idx,
|
||||
ifnull(idx_cols.name, '') as column_name,
|
||||
idx_cols.seqno as rank
|
||||
from indexes
|
||||
join pragma_index_info(indexes.index_name) as idx_cols;
|
||||
|
||||
@ -43,6 +43,7 @@ create table foods (rowid integer primary key,
|
||||
cook_ratio real not null default 1
|
||||
) strict;
|
||||
create index foods_protein on foods(protein);
|
||||
create index foods_fake_index on foods(protein + 1);
|
||||
|
||||
|
||||
create table units (rowid integer primary key,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user