feat(writers): stamp RelSpec version in generated headers; preserve DBML column order
- Add pkg/buildinfo with Version/BuildDate (set via ldflags, VCS fallback); cmd/relspec now sources version info from it - Emit "RelSpec <version> (built: <date>)" in generated file headers for bun, gorm, drizzle, pgsql (incl. migration), mssql, sqlite writers - pgsql writer: getSortedColumns now sorts by Sequence then Name so the streaming WriteSchema/WriteDatabase path preserves source column order - dbml reader: mergeTable re-bases merged-in column Sequence values past the existing max, fixing colliding sequences (and alphabetical fallback) when a table is split across multiple DBML files - Tests for bun/gorm header + column order, and dbml multi-file merge ordering
This commit is contained in:
@@ -305,9 +305,39 @@ func sortDBMLFiles(files []string) []string {
|
||||
// Merges: Columns (map), Constraints (map), Indexes (map), Relationships (map)
|
||||
// Uses first non-empty Description
|
||||
func mergeTable(baseTable, fileTable *models.Table) {
|
||||
// Merge columns (map naturally merges - later keys overwrite)
|
||||
for key, col := range fileTable.Columns {
|
||||
baseTable.Columns[key] = col
|
||||
// Merge columns. Each file numbers its own columns from 1, so a table split
|
||||
// across files would otherwise end up with colliding Column.Sequence values
|
||||
// and writers would fall back to alphabetical order. Re-base the incoming
|
||||
// file's new columns after the highest sequence already present, preserving
|
||||
// their in-file order. Columns that overwrite an existing key keep the
|
||||
// original position.
|
||||
var maxSeq uint
|
||||
for _, col := range baseTable.Columns {
|
||||
if col.Sequence > maxSeq {
|
||||
maxSeq = col.Sequence
|
||||
}
|
||||
}
|
||||
|
||||
incoming := make([]*models.Column, 0, len(fileTable.Columns))
|
||||
for _, col := range fileTable.Columns {
|
||||
incoming = append(incoming, col)
|
||||
}
|
||||
sort.Slice(incoming, func(i, j int) bool {
|
||||
if incoming[i].Sequence != incoming[j].Sequence {
|
||||
return incoming[i].Sequence < incoming[j].Sequence
|
||||
}
|
||||
return incoming[i].Name < incoming[j].Name
|
||||
})
|
||||
|
||||
var added uint
|
||||
for _, col := range incoming {
|
||||
if existing, ok := baseTable.Columns[col.Name]; ok {
|
||||
col.Sequence = existing.Sequence
|
||||
} else {
|
||||
added++
|
||||
col.Sequence = maxSeq + added
|
||||
}
|
||||
baseTable.Columns[col.Name] = col
|
||||
}
|
||||
|
||||
// Merge constraints
|
||||
|
||||
@@ -652,6 +652,22 @@ func TestReadDirectory_TableMerging(t *testing.T) {
|
||||
if emailCol.Type != "varchar(255)" {
|
||||
t.Errorf("Expected email type 'varchar(255)', got '%s'", emailCol.Type)
|
||||
}
|
||||
|
||||
// Merged columns must keep declaration order (file 1: id, email; file 3:
|
||||
// name, created_at) via strictly increasing, non-colliding Sequence values
|
||||
// so downstream writers do not fall back to alphabetical order.
|
||||
order := []string{"id", "email", "name", "created_at"}
|
||||
var prev uint
|
||||
for i, name := range order {
|
||||
col := usersTable.Columns[name]
|
||||
if col.Sequence == 0 {
|
||||
t.Fatalf("column %q has zero Sequence after merge", name)
|
||||
}
|
||||
if i > 0 && col.Sequence <= prev {
|
||||
t.Errorf("column %q Sequence %d not greater than previous %d (order not preserved across files)", name, col.Sequence, prev)
|
||||
}
|
||||
prev = col.Sequence
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadDirectory_CommentedRefsLast(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user