From 100b53d79960abab76357a5387ff5e068245090c Mon Sep 17 00:00:00 2001 From: wispem-wantex Date: Sat, 23 Aug 2025 18:55:13 -0700 Subject: [PATCH] Add Schema definition changes to support the linter functions --- pkg/schema/parse.go | 10 ++++++++++ pkg/schema/table.go | 1 + pkg/schema/views.sql | 3 ++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/schema/parse.go b/pkg/schema/parse.go index c797f98..a44602c 100644 --- a/pkg/schema/parse.go +++ b/pkg/schema/parse.go @@ -2,6 +2,8 @@ package schema import ( _ "embed" + "fmt" + "os" "strings" "github.com/jinzhu/inflection" @@ -22,6 +24,14 @@ func InitDB(sql_schema string) *sqlx.DB { return db } +func SchemaFromSQLFile(filepath string) (Schema, error) { + sqlBytes, err := os.ReadFile(filepath) + if err != nil { + return Schema{}, fmt.Errorf("failed to read SQL file: %w", err) + } + return SchemaFromDB(InitDB(string(sqlBytes))), nil +} + // SchemaFromDB takes a DB connection, checks its schema metadata tables, and returns a Schema. func SchemaFromDB(db *sqlx.DB) Schema { ret := Schema{Tables: map[string]Table{}, Indexes: map[string]Index{}} diff --git a/pkg/schema/table.go b/pkg/schema/table.go index cc69032..b97bb8c 100644 --- a/pkg/schema/table.go +++ b/pkg/schema/table.go @@ -9,6 +9,7 @@ type Column struct { HasDefaultValue bool `db:"has_default_value"` DefaultValue string `db:"dflt_value"` IsPrimaryKey bool `db:"is_primary_key"` + PrimaryKeyRank uint `db:"primary_key_rank"` IsForeignKey bool `db:"is_foreign_key"` ForeignKeyTargetTable string `db:"fk_target_table"` ForeignKeyTargetColumn string `db:"fk_target_column"` diff --git a/pkg/schema/views.sql b/pkg/schema/views.sql index ca6eca0..915f1c7 100644 --- a/pkg/schema/views.sql +++ b/pkg/schema/views.sql @@ -15,7 +15,8 @@ create temporary view columns as "notnull", dflt_value is not null as has_default_value, ifnull(dflt_value, 0) dflt_value, - pk as is_primary_key, + pk != 0 as is_primary_key, + pk as primary_key_rank, fk."table" is not null as is_foreign_key, ifnull(fk."table", '') as fk_target_table, ifnull(fk."to", '') as fk_target_column