Skip to content

Commit

Permalink
Use pointers for schema and Table struct fields (#574)
Browse files Browse the repository at this point in the history
Update the `schema` and `Tables` structs in the `schema` package to use
pointer types in fields.

As part of #239, the in-memory schema representation will need to be
updated more often and more accurately by individual operations in a
migration. Using pointers in these `schema` package structs makes this
possible.

Part of #239
  • Loading branch information
andrew-farries authored Jan 9, 2025
1 parent 40b2991 commit daba767
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 127 deletions.
10 changes: 5 additions & 5 deletions pkg/migrations/duplicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

var table = &schema.Table{
Name: "test_table",
Columns: map[string]schema.Column{
Columns: map[string]*schema.Column{
"id": {Name: "id", Type: "serial"},
"name": {Name: "name", Type: "text"},
"nick": {Name: "nick", Type: "text"},
Expand All @@ -22,21 +22,21 @@ var table = &schema.Table{
"city": {Name: "city", Type: "text"},
"description": {Name: "description", Type: "text"},
},
UniqueConstraints: map[string]schema.UniqueConstraint{
UniqueConstraints: map[string]*schema.UniqueConstraint{
"unique_email": {Name: "unique_email", Columns: []string{"email"}},
"unique_name_nick": {Name: "unique_name_nick", Columns: []string{"name", "nick"}},
},
CheckConstraints: map[string]schema.CheckConstraint{
CheckConstraints: map[string]*schema.CheckConstraint{
"email_at": {Name: "email_at", Columns: []string{"email"}, Definition: `"email" ~ '@'`},
"adults": {Name: "adults", Columns: []string{"age"}, Definition: `"age" > 18`},
"new_york_adults": {Name: "new_york_adults", Columns: []string{"city", "age"}, Definition: `"city" = 'New York' AND "age" > 21`},
"different_nick": {Name: "different_nick", Columns: []string{"name", "nick"}, Definition: `"name" != "nick"`},
},
ForeignKeys: map[string]schema.ForeignKey{
ForeignKeys: map[string]*schema.ForeignKey{
"fk_city": {Name: "fk_city", Columns: []string{"city"}, ReferencedTable: "cities", ReferencedColumns: []string{"id"}, OnDelete: "NO ACTION"},
"fk_name_nick": {Name: "fk_name_nick", Columns: []string{"name", "nick"}, ReferencedTable: "users", ReferencedColumns: []string{"name", "nick"}, OnDelete: "CASCADE"},
},
Indexes: map[string]schema.Index{
Indexes: map[string]*schema.Index{
"idx_no_kate": {
Name: "idx_no_kate",
Columns: []string{"name"},
Expand Down
4 changes: 2 additions & 2 deletions pkg/migrations/op_add_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (o *OpAddColumn) Start(ctx context.Context, conn db.DB, latestSchema string
tableToBackfill = table
}

table.AddColumn(o.Column.Name, schema.Column{
table.AddColumn(o.Column.Name, &schema.Column{
Name: TemporaryName(o.Column.Name),
})

Expand Down Expand Up @@ -186,7 +186,7 @@ func (o *OpAddColumn) Validate(ctx context.Context, s *schema.Schema) error {

// Update the schema to ensure that the new column is visible to validation of
// subsequent operations.
table.AddColumn(o.Column.Name, schema.Column{
table.AddColumn(o.Column.Name, &schema.Column{
Name: TemporaryName(o.Column.Name),
})

Expand Down
2 changes: 1 addition & 1 deletion pkg/migrations/op_alter_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (o *OpAlterColumn) Start(ctx context.Context, conn db.DB, latestSchema stri
// Add the new column to the internal schema representation. This is done
// here, before creation of the down trigger, so that the trigger can declare
// a variable for the new column.
table.AddColumn(o.Column, schema.Column{
table.AddColumn(o.Column, &schema.Column{
Name: TemporaryName(o.Column),
})

Expand Down
2 changes: 1 addition & 1 deletion pkg/migrations/op_create_constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (o *OpCreateConstraint) Start(ctx context.Context, conn db.DB, latestSchema
return nil, fmt.Errorf("failed to create up trigger: %w", err)
}

table.AddColumn(colName, schema.Column{
table.AddColumn(colName, &schema.Column{
Name: physicalColumnName,
})

Expand Down
6 changes: 3 additions & 3 deletions pkg/migrations/op_create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ func (o *OpCreateTable) Validate(ctx context.Context, s *schema.Schema) error {
// updateSchema updates the in-memory schema representation with the details of
// the new table.
func (o *OpCreateTable) updateSchema(s *schema.Schema) *schema.Schema {
columns := make(map[string]schema.Column, len(o.Columns))
columns := make(map[string]*schema.Column, len(o.Columns))
for _, col := range o.Columns {
columns[col.Name] = schema.Column{
columns[col.Name] = &schema.Column{
Name: col.Name,
}
}
s.AddTable(o.Name, schema.Table{
s.AddTable(o.Name, &schema.Table{
Name: o.Name,
Columns: columns,
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/migrations/op_drop_constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (o *OpDropConstraint) Start(ctx context.Context, conn db.DB, latestSchema s
// Add the new column to the internal schema representation. This is done
// here, before creation of the down trigger, so that the trigger can declare
// a variable for the new column.
table.AddColumn(column.Name, schema.Column{
table.AddColumn(column.Name, &schema.Column{
Name: TemporaryName(column.Name),
})

Expand Down
2 changes: 1 addition & 1 deletion pkg/migrations/op_drop_multicolumn_constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (o *OpDropMultiColumnConstraint) Start(ctx context.Context, conn db.DB, lat
// Add the new column to the internal schema representation. This is done
// here, before creation of the down trigger, so that the trigger can declare
// a variable for the new column.
table.AddColumn(columnName, schema.Column{
table.AddColumn(columnName, &schema.Column{
Name: TemporaryName(columnName),
})

Expand Down
2 changes: 1 addition & 1 deletion pkg/migrations/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
type triggerConfig struct {
Name string
Direction TriggerDirection
Columns map[string]schema.Column
Columns map[string]*schema.Column
SchemaName string
TableName string
PhysicalColumn string
Expand Down
8 changes: 4 additions & 4 deletions pkg/migrations/trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestBuildFunction(t *testing.T) {
config: triggerConfig{
Name: "triggerName",
Direction: TriggerDirectionUp,
Columns: map[string]schema.Column{
Columns: map[string]*schema.Column{
"id": {Name: "id", Type: "int"},
"username": {Name: "username", Type: "text"},
"product": {Name: "product", Type: "text"},
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestBuildFunction(t *testing.T) {
config: triggerConfig{
Name: "triggerName",
Direction: TriggerDirectionUp,
Columns: map[string]schema.Column{
Columns: map[string]*schema.Column{
"id": {Name: "id", Type: "int"},
"username": {Name: "username", Type: "text"},
"product": {Name: "product", Type: "text"},
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestBuildFunction(t *testing.T) {
config: triggerConfig{
Name: "triggerName",
Direction: TriggerDirectionDown,
Columns: map[string]schema.Column{
Columns: map[string]*schema.Column{
"id": {Name: "id", Type: "int"},
"username": {Name: "username", Type: "text"},
"product": {Name: "product", Type: "text"},
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestBuildFunction(t *testing.T) {
config: triggerConfig{
Name: "triggerName",
Direction: TriggerDirectionDown,
Columns: map[string]schema.Column{
Columns: map[string]*schema.Column{
"id": {Name: "id", Type: "int"},
"username": {Name: "username", Type: "text"},
"product": {Name: "product", Type: "text"},
Expand Down
2 changes: 1 addition & 1 deletion pkg/roll/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (m *Roll) Rollback(ctx context.Context) error {
}

// create view creates a view for the new version of the schema
func (m *Roll) ensureView(ctx context.Context, version, name string, table schema.Table) error {
func (m *Roll) ensureView(ctx context.Context, version, name string, table *schema.Table) error {
columns := make([]string, 0, len(table.Columns))
for k, v := range table.Columns {
columns = append(columns, fmt.Sprintf("%s AS %s", pq.QuoteIdentifier(v.Name), pq.QuoteIdentifier(k)))
Expand Down
26 changes: 13 additions & 13 deletions pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

func New() *Schema {
return &Schema{
Tables: make(map[string]Table),
Tables: make(map[string]*Table),
}
}

Expand All @@ -27,7 +27,7 @@ type Schema struct {
// Name is the name of the schema
Name string `json:"name"`
// Tables is a map of virtual table name -> table mapping
Tables map[string]Table `json:"tables"`
Tables map[string]*Table `json:"tables"`
}

// Table represents a table in the schema
Expand All @@ -42,22 +42,22 @@ type Table struct {
Comment string `json:"comment"`

// Columns is a map of virtual column name -> column mapping
Columns map[string]Column `json:"columns"`
Columns map[string]*Column `json:"columns"`

// Indexes is a map of the indexes defined on the table
Indexes map[string]Index `json:"indexes"`
Indexes map[string]*Index `json:"indexes"`

// The columns that make up the primary key
PrimaryKey []string `json:"primaryKey"`

// ForeignKeys is a map of all foreign keys defined on the table
ForeignKeys map[string]ForeignKey `json:"foreignKeys"`
ForeignKeys map[string]*ForeignKey `json:"foreignKeys"`

// CheckConstraints is a map of all check constraints defined on the table
CheckConstraints map[string]CheckConstraint `json:"checkConstraints"`
CheckConstraints map[string]*CheckConstraint `json:"checkConstraints"`

// UniqueConstraints is a map of all unique constraints defined on the table
UniqueConstraints map[string]UniqueConstraint `json:"uniqueConstraints"`
UniqueConstraints map[string]*UniqueConstraint `json:"uniqueConstraints"`
}

// Column represents a column in a table
Expand Down Expand Up @@ -148,13 +148,13 @@ func (s *Schema) GetTable(name string) *Table {
if !ok {
return nil
}
return &t
return t
}

// AddTable adds a table to the schema
func (s *Schema) AddTable(name string, t Table) {
func (s *Schema) AddTable(name string, t *Table) {
if s.Tables == nil {
s.Tables = make(map[string]Table)
s.Tables = make(map[string]*Table)
}

s.Tables[name] = t
Expand Down Expand Up @@ -189,7 +189,7 @@ func (t *Table) GetColumn(name string) *Column {
if !ok {
return nil
}
return &c
return c
}

// ConstraintExists returns true if a constraint with the given name exists
Expand Down Expand Up @@ -234,9 +234,9 @@ func (t *Table) GetPrimaryKey() (columns []*Column) {
}

// AddColumn adds a column to the table
func (t *Table) AddColumn(name string, c Column) {
func (t *Table) AddColumn(name string, c *Column) {
if t.Columns == nil {
t.Columns = make(map[string]Column)
t.Columns = make(map[string]*Column)
}

t.Columns[name] = c
Expand Down
Loading

0 comments on commit daba767

Please sign in to comment.