From f8664ed514e01b3050b95679c35f2ab7f80f44bb Mon Sep 17 00:00:00 2001 From: wispem-wantex Date: Sun, 15 Mar 2026 15:45:16 -0700 Subject: [PATCH] schema: split 'column', 'index' and 'schema' into separate files from 'table' --- pkg/schema/column.go | 61 +++++++++++++++++++++++++++++++++++++ pkg/schema/index.go | 10 +++++++ pkg/schema/schema.go | 6 ++++ pkg/schema/table.go | 71 -------------------------------------------- 4 files changed, 77 insertions(+), 71 deletions(-) create mode 100644 pkg/schema/column.go create mode 100644 pkg/schema/index.go create mode 100644 pkg/schema/schema.go diff --git a/pkg/schema/column.go b/pkg/schema/column.go new file mode 100644 index 0000000..e614465 --- /dev/null +++ b/pkg/schema/column.go @@ -0,0 +1,61 @@ +package schema + +import ( + "strings" + + "git.offline-twitter.com/offline-labs/gas-stack/pkg/textutils" +) + +// Column represents a single column in a table. +type Column struct { + TableName string `db:"table_name"` + Name string `db:"column_name"` + Type string `db:"column_type"` + IsNotNull bool `db:"notnull"` + 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"` +} + +// IsNullableForeignKey is a helper function. +func (c Column) IsNullableForeignKey() bool { + return !c.IsNotNull && !c.IsPrimaryKey && c.IsForeignKey +} + +func (c Column) IsNonCodeTableForeignKey() bool { + return c.IsForeignKey && strings.HasSuffix(c.Name, "_id") +} + +func (c Column) GoFieldName() string { + if c.Name == "rowid" { + return "ID" + } + if c.IsNonCodeTableForeignKey() { + return textutils.SnakeToCamel(strings.TrimSuffix(c.Name, "_id")) + "ID" + } + return textutils.SnakeToCamel(c.Name) +} + +// GoVarName returns the name of a local variable for this column, e.g., when used as a function parameter. +func (c Column) GoVarName() string { + if c.Name == "rowid" { + return strings.ToLower(c.TableName)[0:1] + "ID" + // TODO: Or should it just be "id"?? + } + // For foreign keys, use first letter of the target type and "ID". "UserID" => "uID" + if c.IsNonCodeTableForeignKey() { + return strings.ToLower(c.ForeignKeyTargetTable)[0:1] + "ID" + } + + // Otherwise, just use the whole name + return c.LongGoVarName() +} + +// LongGoVarName returns a lowercased version of the field name (Pascal => Camel). +func (c Column) LongGoVarName() string { + return textutils.CamelToPascal(c.GoFieldName()) +} diff --git a/pkg/schema/index.go b/pkg/schema/index.go new file mode 100644 index 0000000..2d4863f --- /dev/null +++ b/pkg/schema/index.go @@ -0,0 +1,10 @@ +package schema + +type Index struct { + Name string `db:"index_name"` + TableName string `db:"table_name"` + Columns []string + IsUnique bool `db:"is_unique"` + // TODO: `where ...` for partial indexes + // TODO: identify columns that are expressions +} diff --git a/pkg/schema/schema.go b/pkg/schema/schema.go new file mode 100644 index 0000000..f07c557 --- /dev/null +++ b/pkg/schema/schema.go @@ -0,0 +1,6 @@ +package schema + +type Schema struct { + Tables map[string]Table + Indexes map[string]Index +} diff --git a/pkg/schema/table.go b/pkg/schema/table.go index 5e742c9..88c9722 100644 --- a/pkg/schema/table.go +++ b/pkg/schema/table.go @@ -2,65 +2,8 @@ package schema import ( "sort" - "strings" - - "git.offline-twitter.com/offline-labs/gas-stack/pkg/textutils" ) -// Column represents a single column in a table. -type Column struct { - TableName string `db:"table_name"` - Name string `db:"column_name"` - Type string `db:"column_type"` - IsNotNull bool `db:"notnull"` - 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"` -} - -// IsNullableForeignKey is a helper function. -func (c Column) IsNullableForeignKey() bool { - return !c.IsNotNull && !c.IsPrimaryKey && c.IsForeignKey -} - -func (c Column) IsNonCodeTableForeignKey() bool { - return c.IsForeignKey && strings.HasSuffix(c.Name, "_id") -} - -func (c Column) GoFieldName() string { - if c.Name == "rowid" { - return "ID" - } - if c.IsNonCodeTableForeignKey() { - return textutils.SnakeToCamel(strings.TrimSuffix(c.Name, "_id")) + "ID" - } - return textutils.SnakeToCamel(c.Name) -} - -// GoVarName returns the name of a local variable for this column, e.g., when used as a function parameter. -func (c Column) GoVarName() string { - if c.Name == "rowid" { - return strings.ToLower(c.TableName)[0:1] + "ID" - // TODO: Or should it just be "id"?? - } - // For foreign keys, use first letter of the target type and "ID". "UserID" => "uID" - if c.IsNonCodeTableForeignKey() { - return strings.ToLower(c.ForeignKeyTargetTable)[0:1] + "ID" - } - - // Otherwise, just use the whole name - return c.LongGoVarName() -} - -// LongGoVarName returns a lowercased version of the field name (Pascal => Camel). -func (c Column) LongGoVarName() string { - return textutils.CamelToPascal(c.GoFieldName()) -} - // Table is a single SQLite table. type Table struct { TableName string `db:"name"` @@ -118,17 +61,3 @@ func (t Table) HasAutoTimestamps() (hasCreatedAt bool, hasUpdatedAt bool) { } return } - -type Index struct { - Name string `db:"index_name"` - TableName string `db:"table_name"` - Columns []string - IsUnique bool `db:"is_unique"` - // TODO: `where ...` for partial indexes - // TODO: identify columns that are expressions -} - -type Schema struct { - Tables map[string]Table - Indexes map[string]Index -}