Compare commits
2 Commits
1b04b71d9d
...
14d395f0ea
Author | SHA1 | Date | |
---|---|---|---|
14d395f0ea | |||
754c21ab29 |
@ -1,6 +1,7 @@
|
|||||||
package schema
|
package schema
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
_ "embed"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/jinzhu/inflection"
|
"github.com/jinzhu/inflection"
|
||||||
@ -10,32 +11,14 @@ import (
|
|||||||
"git.offline-twitter.com/offline-labs/gas-stack/pkg/textutils"
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/textutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed views.sql
|
||||||
|
var create_views string
|
||||||
|
|
||||||
// InitDB creates an in-memory DB from a given schema string.
|
// InitDB creates an in-memory DB from a given schema string.
|
||||||
func InitDB(sql_schema string) *sqlx.DB {
|
func InitDB(sql_schema string) *sqlx.DB {
|
||||||
db := sqlx.MustOpen("sqlite3", ":memory:")
|
db := sqlx.MustOpen("sqlite3", ":memory:")
|
||||||
db.MustExec(sql_schema)
|
db.MustExec(sql_schema)
|
||||||
db.MustExec(`
|
db.MustExec(create_views)
|
||||||
create temporary view tables as
|
|
||||||
select l.schema, l.name, l.type, l.ncol, l.wr, l.strict
|
|
||||||
from sqlite_schema s
|
|
||||||
left join pragma_table_list l on s.name = l.name
|
|
||||||
where s.type = 'table';
|
|
||||||
|
|
||||||
create temporary view columns as
|
|
||||||
select tables.name as table_name,
|
|
||||||
table_info.name as column_name,
|
|
||||||
lower(table_info.type) as column_type,
|
|
||||||
"notnull",
|
|
||||||
dflt_value is not null as has_default_value,
|
|
||||||
ifnull(dflt_value, 0) dflt_value,
|
|
||||||
pk as is_primary_key,
|
|
||||||
fk."table" is not null as is_foreign_key,
|
|
||||||
ifnull(fk."table", '') as fk_target_table,
|
|
||||||
ifnull(fk."to", '') as fk_target_column
|
|
||||||
from tables
|
|
||||||
join pragma_table_info(tables.name) as table_info
|
|
||||||
left join pragma_foreign_key_list(tables.name) as fk on fk."from" = column_name;
|
|
||||||
`)
|
|
||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,7 +27,7 @@ func SchemaFromDB(db *sqlx.DB) Schema {
|
|||||||
ret := Schema{}
|
ret := Schema{}
|
||||||
|
|
||||||
var tables []Table
|
var tables []Table
|
||||||
err := db.Select(&tables, `select name, strict from tables`)
|
err := db.Select(&tables, `select name, is_strict, is_without_rowid from tables`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -21,9 +21,11 @@ func (c Column) IsNullableForeignKey() bool {
|
|||||||
|
|
||||||
// Table is a single SQLite table.
|
// Table is a single SQLite table.
|
||||||
type Table struct {
|
type Table struct {
|
||||||
TableName string `db:"name"`
|
TableName string `db:"name"`
|
||||||
IsStrict bool `db:"strict"`
|
IsStrict bool `db:"is_strict"`
|
||||||
Columns []Column
|
IsWithoutRowid bool `db:"is_without_rowid"`
|
||||||
|
|
||||||
|
Columns []Column
|
||||||
|
|
||||||
TypeIDName string
|
TypeIDName string
|
||||||
VarName string
|
VarName string
|
||||||
|
24
pkg/schema/views.sql
Normal file
24
pkg/schema/views.sql
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
create temporary view tables as
|
||||||
|
select l.schema,
|
||||||
|
l.name,
|
||||||
|
l.type,
|
||||||
|
l.wr as is_without_rowid,
|
||||||
|
l.strict as is_strict
|
||||||
|
from sqlite_schema s
|
||||||
|
left join pragma_table_list l on s.name = l.name
|
||||||
|
where s.type = 'table';
|
||||||
|
|
||||||
|
create temporary view columns as
|
||||||
|
select tables.name as table_name,
|
||||||
|
table_info.name as column_name,
|
||||||
|
lower(table_info.type) as column_type,
|
||||||
|
"notnull",
|
||||||
|
dflt_value is not null as has_default_value,
|
||||||
|
ifnull(dflt_value, 0) dflt_value,
|
||||||
|
pk as is_primary_key,
|
||||||
|
fk."table" is not null as is_foreign_key,
|
||||||
|
ifnull(fk."table", '') as fk_target_table,
|
||||||
|
ifnull(fk."to", '') as fk_target_column
|
||||||
|
from tables
|
||||||
|
join pragma_table_info(tables.name) as table_info
|
||||||
|
left join pragma_foreign_key_list(tables.name) as fk on fk."from" = column_name;
|
Loading…
x
Reference in New Issue
Block a user