Skip to content

Commit

Permalink
Move final two function from dbx -> ir and retire dbx completely
Browse files Browse the repository at this point in the history
  • Loading branch information
williammoran committed May 1, 2024
1 parent bdab373 commit 00932e1
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 64 deletions.
42 changes: 0 additions & 42 deletions lib/dbx.go

This file was deleted.

3 changes: 2 additions & 1 deletion lib/format/pgsql8/column.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pgsql8

import (
"log/slog"
"strings"

"github.com/dbsteward/dbsteward/lib"
Expand Down Expand Up @@ -125,7 +126,7 @@ func getColumnType(doc *ir.Definition, schema *ir.Schema, table *ir.Table, colum
// if it is a foreign keyed column, solve for the foreign key type
if column.ForeignTable != "" {
// TODO(feat) what about compound FKs?
foreign, err := lib.GlobalDBX.GetTerminalForeignColumn(doc, schema, table, column)
foreign, err := doc.GetTerminalForeignColumn(slog.Default(), schema, table, column)
lib.GlobalDBSteward.FatalIfError(err, "")
return getReferenceType(foreign.Type)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/format/pgsql8/constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func getTableConstraints(doc *ir.Definition, schema *ir.Schema, table *ir.Table,
if ct.Includes(sql99.ConstraintTypeConstraint) {
for _, column := range table.Columns {
if ct.Includes(sql99.ConstraintTypeForeign) && column.HasForeignKey() {
ref, err := lib.GlobalDBX.ResolveForeignKeyColumn(doc, schema, table, column)
ref, err := doc.ResolveForeignKeyColumn(schema, table, column)
lib.GlobalDBSteward.FatalIfError(err, "")
constraints = append(constraints, &sql99.TableConstraint{
Name: util.CoalesceStr(column.ForeignKeyName, buildForeignKeyName(table.Name, column.Name)),
Expand Down
3 changes: 2 additions & 1 deletion lib/format/pgsql8/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"log"
"log/slog"
"os"
"slices"
"strings"
Expand Down Expand Up @@ -788,7 +789,7 @@ func (ops *Operations) CompareDbData(doc *ir.Definition, host string, port uint,
if len(colType) > 0 {
dbsteward.Fatal("type of %s was found for column %s but it is foreign keyed", colType, column.Name)
}
foreign, err := lib.GlobalDBX.GetTerminalForeignColumn(doc, schema, table, column)
foreign, err := doc.GetTerminalForeignColumn(slog.Default(), schema, table, column)
dbsteward.FatalIfError(err, "")
colType = foreign.Type
}
Expand Down
32 changes: 30 additions & 2 deletions lib/ir/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ func (doc *Definition) ResolveForeignKey(localKey Key, foreignKey KeyNames) (Key
}

if len(localKey.Columns) != len(foreignKey.Columns) {
return Key{}, fmt.Errorf("Local %s has column count mismatch with foreign %s", localKey.String(), foreignKey.String())
return Key{}, fmt.Errorf("local %s has column count mismatch with foreign %s", localKey.String(), foreignKey.String())
}

out := Key{
Expand All @@ -544,14 +544,42 @@ func (doc *Definition) ResolveForeignKey(localKey Key, foreignKey KeyNames) (Key
return Key{}, fmt.Errorf("running TryInheritanceGetColumn: %w", err)
}
if fCol == nil {
return Key{}, fmt.Errorf("Failed to find foreign column %s in %s referenced by %s", col, foreignKey.String(), localKey.String())
return Key{}, fmt.Errorf("failed to find foreign column %s in %s referenced by %s", col, foreignKey.String(), localKey.String())
}
out.Columns[i] = fCol
}

return out, nil
}

func (doc *Definition) GetTerminalForeignColumn(l *slog.Logger, schema *Schema, table *Table, column *Column) (*Column, error) {
fkey, err := doc.ResolveForeignKeyColumn(schema, table, column)
if err != nil {
return nil, err
}
fcol := fkey.Columns[0]

if fcol.Type == "" && fcol.ForeignTable != "" {
l.Debug(fmt.Sprintf("Seeking nested foreign key for %s", fkey.String()))
return doc.GetTerminalForeignColumn(l, fkey.Schema, fkey.Table, fcol)
}
return fcol, err
}

func (doc *Definition) ResolveForeignKeyColumn(schema *Schema, table *Table, column *Column) (Key, error) {
// this used to be called format_constraint::foreign_key_lookup() in v1
// most of the functionality got split to the more general ResolveForeignKey
foreign := column.TryGetReferencedKey()
util.Assert(foreign != nil, "ResolveForeignKeyColumn called with column that does not reference a foreign column")

local := Key{
Schema: schema,
Table: table,
Columns: []*Column{column},
}
return doc.ResolveForeignKey(local, *foreign)
}

func (s *Sql) IdentityMatches(other *Sql) bool {
if other == nil {
return false
Expand Down
31 changes: 14 additions & 17 deletions lib/dbx_fk_test.go → lib/ir/operations_test.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,43 @@
package lib_test
package ir

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/dbsteward/dbsteward/lib"
"github.com/dbsteward/dbsteward/lib/ir"
)

func TestDBX_ResolveForeignKey_InheritedColumn(t *testing.T) {
// NOTE: In v1 this was tests/pgsql8/FKeyToInheritedTableTest.php
doc := &ir.Definition{
Schemas: []*ir.Schema{
&ir.Schema{
doc := &Definition{
Schemas: []*Schema{
{
Name: "test",
Tables: []*ir.Table{
&ir.Table{
Tables: []*Table{
{
Name: "parent",
PrimaryKey: []string{"foo"},
Columns: []*ir.Column{
Columns: []*Column{
{Name: "foo", Type: "varchar(255)"},
},
},
&ir.Table{
{
Name: "child",
PrimaryKey: []string{"foo"},
InheritsSchema: "test",
InheritsTable: "parent",
Columns: []*ir.Column{
Columns: []*Column{
{Name: "bar", Type: "varchar(255)"},
},
},
},
},
&ir.Schema{
{
Name: "other",
Tables: []*ir.Table{
&ir.Table{
Tables: []*Table{
{
Name: "baz",
PrimaryKey: []string{"footoo"},
Columns: []*ir.Column{
Columns: []*Column{
{Name: "footoo", ForeignSchema: "test", ForeignTable: "child", ForeignColumn: "foo"},
},
},
Expand All @@ -52,7 +49,7 @@ func TestDBX_ResolveForeignKey_InheritedColumn(t *testing.T) {
table := schema.Tables[0]
column := table.Columns[0]

fkey, err := lib.NewDBX().ResolveForeignKeyColumn(doc, schema, table, column)
fkey, err := doc.ResolveForeignKeyColumn(schema, table, column)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 00932e1

Please sign in to comment.