Skip to content

Commit

Permalink
sql/internal/specuti: connect columns to their indexes on eval (#3223)
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m authored Nov 17, 2024
1 parent 147520c commit 194c365
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
24 changes: 19 additions & 5 deletions sql/internal/specutil/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,16 +477,21 @@ func Index(spec *sqlspec.Index, parent *schema.Table, partFns ...func(*sqlspec.I
parts = append(parts, part)
}
}
i := &schema.Index{
idx := &schema.Index{
Name: spec.Name,
Unique: spec.Unique,
Table: parent,
Parts: parts,
}
if err := convertCommentFromSpec(spec, &i.Attrs); err != nil {
if err := convertCommentFromSpec(spec, &idx.Attrs); err != nil {
return nil, err
}
return i, nil
for _, p := range idx.Parts {
if p.C != nil {
p.C.AddIndexes(idx)
}
}
return idx, nil
}

// Check converts a sqlspec.Check to a schema.Check.
Expand All @@ -510,10 +515,19 @@ func PrimaryKey(spec *sqlspec.PrimaryKey, parent *schema.Table) (*schema.Index,
C: c,
})
}
return &schema.Index{
pk := &schema.Index{
Table: parent,
Parts: parts,
}, nil
}
if err := convertCommentFromSpec(spec, &pk.Attrs); err != nil {
return nil, err
}
for _, p := range pk.Parts {
if p.C != nil {
p.C.AddIndexes(pk)
}
}
return pk, nil
}

// linkForeignKeys creates the foreign keys defined in the Table's spec by creating references
Expand Down
8 changes: 5 additions & 3 deletions sql/mysql/sqlspec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,11 @@ schema "schema" {
{SeqNo: 0, C: exp.Tables[1].Columns[0]},
},
}
exp.Tables[0].Columns[0].AddIndexes(exp.Tables[0].PrimaryKey)
exp.Tables[0].Columns[0].AddIndexes(exp.Tables[0].Indexes[0])
exp.Tables[0].Columns[1].AddIndexes(exp.Tables[0].Indexes[0])
exp.Tables[1].Columns[0].AddIndexes(exp.Tables[1].PrimaryKey)
require.EqualValues(t, exp, &s)

_, err = MarshalHCL(&s)
require.NoError(t, err)
}
Expand Down Expand Up @@ -708,7 +711,6 @@ table "users" {
),
),
)
exp.Tables[0].Columns[0].Indexes = nil
schema.NewRealm(exp)
require.EqualValues(t, exp, &s)
}
Expand Down Expand Up @@ -801,7 +803,6 @@ schema "test" {
AddAttrs(&IndexType{T: IndexTypeFullText}, &IndexParser{P: "custom"}),
),
)
c.Indexes = nil
schema.NewRealm(exp)
require.EqualValues(t, exp, &s)
}
Expand Down Expand Up @@ -870,6 +871,7 @@ schema "test" {
Parts: []*schema.IndexPart{{C: exp.Tables[0].Columns[0]}},
Attrs: []schema.Attr{&IndexType{T: IndexTypeHash}},
})
exp.Tables[0].Columns[0].AddIndexes(exp.Tables[0].PrimaryKey)
schema.NewRealm(exp)
require.EqualValues(t, exp, &s)
}
Expand Down
4 changes: 4 additions & 0 deletions sql/postgres/sqlspec_oss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ enum "account_type" {
{SeqNo: 0, C: exp.Tables[1].Columns[0]},
},
}
exp.Tables[0].Columns[0].AddIndexes(exp.Tables[0].PrimaryKey)
exp.Tables[0].Columns[0].AddIndexes(exp.Tables[0].Indexes...)
exp.Tables[0].Columns[1].AddIndexes(exp.Tables[0].Indexes...)
exp.Tables[1].Columns[0].AddIndexes(exp.Tables[1].PrimaryKey)
exp.Realm = schema.NewRealm(exp)
require.EqualValues(t, exp, &s)
}
Expand Down
10 changes: 10 additions & 0 deletions sql/schema/dsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,16 @@ func (c *Column) SetPos(p *Pos) *Column {
return c
}

// AddIndexes appends the references to the indexes this column is part of.
func (c *Column) AddIndexes(indexes ...*Index) *Column {
for _, idx := range indexes {
if !slices.Contains(c.Indexes, idx) {
c.Indexes = append(c.Indexes, idx)
}
}
return c
}

// NewCheck creates a new check.
func NewCheck() *Check {
return &Check{}
Expand Down
4 changes: 4 additions & 0 deletions sql/sqlite/sqlspec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ table "accounts" {
{SeqNo: 0, C: exp.Tables[1].Columns[0]},
},
}
exp.Tables[0].Columns[0].AddIndexes(exp.Tables[0].PrimaryKey)
exp.Tables[0].Columns[0].AddIndexes(exp.Tables[0].Indexes[0])
exp.Tables[0].Columns[1].AddIndexes(exp.Tables[0].Indexes[0])
exp.Tables[1].Columns[0].AddIndexes(exp.Tables[1].PrimaryKey)
var s schema.Schema
err := EvalHCLBytes([]byte(f), &s, nil)
require.NoError(t, err)
Expand Down

0 comments on commit 194c365

Please sign in to comment.