fix(dbml): honor composite [pk] in Indexes blocks, preserve column order
A composite [pk] entry inside an Indexes block (e.g. (a, b) [pk]) was silently dropped: models.Index has no way to represent a primary key, so the attribute was parsed and ignored, producing neither a PK nor a meaningful index. It's now converted into a PrimaryKeyConstraint. Also, Column.Sequence was never set by the DBML reader, so composite PKs assembled from column-level [pk] attributes fell back to alphabetical Name sorting instead of declaration order. Columns now get a per-table sequence counter reflecting the order they were declared.
This commit is contained in:
@@ -932,3 +932,97 @@ func TestHasCommentedRefs(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestReader_CompositePKIndex verifies that a composite `[pk]` entry inside
|
||||
// an Indexes block is turned into a primary key constraint, in declaration
|
||||
// order, rather than being silently dropped.
|
||||
func TestReader_CompositePKIndex(t *testing.T) {
|
||||
dbmlContent := `Table artifact_blob {
|
||||
artifact_id integer [not null]
|
||||
sha256 text [not null]
|
||||
size integer
|
||||
|
||||
Indexes {
|
||||
(artifact_id, sha256) [pk]
|
||||
}
|
||||
}
|
||||
`
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "composite_pk.dbml")
|
||||
if err := os.WriteFile(path, []byte(dbmlContent), 0644); err != nil {
|
||||
t.Fatalf("failed to write fixture: %v", err)
|
||||
}
|
||||
|
||||
reader := NewReader(&readers.ReaderOptions{FilePath: path})
|
||||
db, err := reader.ReadDatabase()
|
||||
if err != nil {
|
||||
t.Fatalf("ReadDatabase() error = %v", err)
|
||||
}
|
||||
|
||||
table := db.Schemas[0].Tables[0]
|
||||
|
||||
var pk *models.Constraint
|
||||
for _, c := range table.Constraints {
|
||||
if c.Type == models.PrimaryKeyConstraint {
|
||||
pk = c
|
||||
break
|
||||
}
|
||||
}
|
||||
if pk == nil {
|
||||
t.Fatal("expected a primary key constraint, got none")
|
||||
}
|
||||
want := []string{"artifact_id", "sha256"}
|
||||
if len(pk.Columns) != len(want) {
|
||||
t.Fatalf("expected PK columns %v, got %v", want, pk.Columns)
|
||||
}
|
||||
for i, col := range want {
|
||||
if pk.Columns[i] != col {
|
||||
t.Errorf("PK column[%d] = %q, want %q (order must match declaration)", i, pk.Columns[i], col)
|
||||
}
|
||||
}
|
||||
|
||||
// No plain index should be emitted for the pk-only entry.
|
||||
if len(table.Indexes) != 0 {
|
||||
t.Errorf("expected no plain indexes from a [pk] Indexes entry, got %v", table.Indexes)
|
||||
}
|
||||
}
|
||||
|
||||
// TestReader_ColumnPKOrderPreserved verifies that composite primary keys
|
||||
// declared via column-level [pk] attributes keep declaration order (via
|
||||
// Column.Sequence) instead of falling back to alphabetical sorting.
|
||||
func TestReader_ColumnPKOrderPreserved(t *testing.T) {
|
||||
dbmlContent := `Table snapshot_artifact {
|
||||
snapshot_id integer [pk, not null]
|
||||
artifact_id integer [pk, not null]
|
||||
}
|
||||
`
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "column_pk_order.dbml")
|
||||
if err := os.WriteFile(path, []byte(dbmlContent), 0644); err != nil {
|
||||
t.Fatalf("failed to write fixture: %v", err)
|
||||
}
|
||||
|
||||
reader := NewReader(&readers.ReaderOptions{FilePath: path})
|
||||
db, err := reader.ReadDatabase()
|
||||
if err != nil {
|
||||
t.Fatalf("ReadDatabase() error = %v", err)
|
||||
}
|
||||
|
||||
table := db.Schemas[0].Tables[0]
|
||||
|
||||
snapshotCol, ok := table.Columns["snapshot_id"]
|
||||
if !ok {
|
||||
t.Fatal("column 'snapshot_id' not found")
|
||||
}
|
||||
artifactCol, ok := table.Columns["artifact_id"]
|
||||
if !ok {
|
||||
t.Fatal("column 'artifact_id' not found")
|
||||
}
|
||||
|
||||
if snapshotCol.Sequence == 0 || artifactCol.Sequence == 0 {
|
||||
t.Fatalf("expected non-zero Sequence values, got snapshot_id=%d artifact_id=%d", snapshotCol.Sequence, artifactCol.Sequence)
|
||||
}
|
||||
if snapshotCol.Sequence >= artifactCol.Sequence {
|
||||
t.Errorf("expected snapshot_id (declared first) to have a lower Sequence than artifact_id, got %d >= %d", snapshotCol.Sequence, artifactCol.Sequence)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user