- bun: scanonly+generated for GENERATED columns; scanonly+identity for ALWAYS identity (PK gets identity only) - gorm: <-:false+generated / identity, same primary key rule - dbml: write GENERATED/IDENTITY column notes and read them back into the model - pgsql: emit generation/identity clauses in migration create-table and add-column, no DEFAULT - readmes updated
78 lines
2.4 KiB
Go
78 lines
2.4 KiB
Go
package dbml
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"git.warky.dev/wdevs/relspecgo/pkg/models"
|
|
"git.warky.dev/wdevs/relspecgo/pkg/readers"
|
|
dbmlreader "git.warky.dev/wdevs/relspecgo/pkg/readers/dbml"
|
|
"git.warky.dev/wdevs/relspecgo/pkg/writers"
|
|
)
|
|
|
|
func TestGeneratedAndIdentityColumnsRoundTrip(t *testing.T) {
|
|
table := models.InitTable("people", "public")
|
|
|
|
id := models.InitColumn("id", "people", "public")
|
|
id.Type = "bigint"
|
|
id.IsPrimaryKey = true
|
|
id.AutoIncrement = true
|
|
id.Identity = true
|
|
id.IdentityGeneration = "ALWAYS"
|
|
table.Columns["id"] = id
|
|
|
|
seq := models.InitColumn("seq", "people", "public")
|
|
seq.Type = "integer"
|
|
seq.Identity = true
|
|
seq.IdentityGeneration = "BY DEFAULT"
|
|
table.Columns["seq"] = seq
|
|
|
|
full := models.InitColumn("full_name", "people", "public")
|
|
full.Type = "text"
|
|
full.Generated = true
|
|
full.GenerationExpression = "first || ' ' || last"
|
|
table.Columns["full_name"] = full
|
|
|
|
plain := models.InitColumn("first", "people", "public")
|
|
plain.Type = "text"
|
|
plain.Comment = "given name"
|
|
table.Columns["first"] = plain
|
|
|
|
schema := models.InitSchema("public")
|
|
schema.Tables = append(schema.Tables, table)
|
|
db := models.InitDatabase("testdb")
|
|
db.Schemas = append(db.Schemas, schema)
|
|
|
|
out := filepath.Join(t.TempDir(), "people.dbml")
|
|
require.NoError(t, NewWriter(&writers.WriterOptions{OutputPath: out}).WriteDatabase(db))
|
|
|
|
content, err := os.ReadFile(out)
|
|
require.NoError(t, err)
|
|
assert.Contains(t, string(content), "GENERATED ALWAYS AS (first || \\' \\' || last) STORED")
|
|
assert.Contains(t, string(content), "GENERATED ALWAYS AS IDENTITY")
|
|
assert.Contains(t, string(content), "GENERATED BY DEFAULT AS IDENTITY")
|
|
|
|
got, err := dbmlreader.NewReader(&readers.ReaderOptions{FilePath: out}).ReadDatabase()
|
|
require.NoError(t, err)
|
|
cols := got.Schemas[0].Tables[0].Columns
|
|
|
|
assert.True(t, cols["id"].Identity)
|
|
assert.Equal(t, "ALWAYS", cols["id"].IdentityGeneration)
|
|
assert.Empty(t, cols["id"].Comment, "marker note must not become a comment")
|
|
|
|
assert.True(t, cols["seq"].Identity)
|
|
assert.Equal(t, "BY DEFAULT", cols["seq"].IdentityGeneration)
|
|
|
|
assert.True(t, cols["full_name"].Generated)
|
|
assert.Equal(t, "first || ' ' || last", cols["full_name"].GenerationExpression)
|
|
assert.Empty(t, cols["full_name"].Comment)
|
|
|
|
assert.False(t, cols["first"].Generated)
|
|
assert.False(t, cols["first"].Identity)
|
|
assert.Equal(t, "given name", cols["first"].Comment)
|
|
}
|