Make it use the gas stack instead of implementing its own schema reading
This commit is contained in:
@@ -41,7 +41,7 @@ var Checks = map[string]Check{
|
||||
column_name
|
||||
from columns
|
||||
where columns."notnull" = 0
|
||||
and fk_target_column is null
|
||||
and is_foreign_key = 0
|
||||
and is_primary_key = 0 -- primary keys are automatically not-null, but aren't listed as such in pragma_table_info
|
||||
`,
|
||||
Explanation: "All columns should be marked as `not null` unless they are foreign keys. (Primary keys are\n" +
|
||||
@@ -55,7 +55,7 @@ var Checks = map[string]Check{
|
||||
// column_name
|
||||
// from columns
|
||||
// where dflt_value is null
|
||||
// and fk_target_column is null
|
||||
// and is_foreign_key = 0
|
||||
// and is_primary_key = 0;
|
||||
// `,
|
||||
// Explanation: "All columns should have a default value specified, unless they are foreign keys or primary keys.",
|
||||
@@ -113,7 +113,7 @@ var Checks = map[string]Check{
|
||||
where column_name = 'rowid'
|
||||
and is_primary_key != 0 -- 'pk' is either 0, or the 1-based index of the column within the primary key
|
||||
), foreign_keys as (
|
||||
select * from columns where fk_target_column is not null
|
||||
select * from columns where is_foreign_key = 1
|
||||
)
|
||||
select 'Foreign keys should point to indexed columns' as error_msg,
|
||||
foreign_keys.table_name as table_name,
|
||||
|
||||
@@ -6,47 +6,17 @@ import (
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
|
||||
"git.offline-twitter.com/offline-labs/gas-stack/pkg/schema"
|
||||
)
|
||||
|
||||
// OpenSchema opens a SQLite database in memory, executes the schema against it, and adds some views
|
||||
func OpenSchema(filepath string) (*sqlx.DB, error) {
|
||||
// Open a SQLite database in memory
|
||||
db, err := sqlx.Open("sqlite3", ":memory:")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open in-memory database: %w", err)
|
||||
}
|
||||
|
||||
// Read the SQL file
|
||||
sqlBytes, err := os.ReadFile(filepath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read SQL file: %w", err)
|
||||
}
|
||||
|
||||
// Execute the SQL statements
|
||||
db.MustExec(string(sqlBytes))
|
||||
|
||||
// Execute the SQL statements for creating views
|
||||
db.MustExec(`
|
||||
create view tables as
|
||||
select l.*
|
||||
from sqlite_schema s
|
||||
left join pragma_table_list l on s.name = l.name
|
||||
where s.type = 'table';
|
||||
|
||||
|
||||
create view columns as
|
||||
select tables.name as table_name,
|
||||
table_info.name as column_name,
|
||||
table_info.type as column_type,
|
||||
"notnull",
|
||||
dflt_value,
|
||||
pk as is_primary_key,
|
||||
fk."table" as fk_target_table,
|
||||
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, nil
|
||||
return schema.InitDB(string(sqlBytes)), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user