- mssql: write computed columns (AS (expr) PERSISTED) and identity; read computed definition and identity - sqlite: write GENERATED ALWAYS AS (expr) STORED; read generated columns via table_xinfo and parse the expression - drizzle: generatedAlwaysAs / generated(Always|ByDefault)AsIdentity, read and write - typeorm: asExpression/generatedType and identity decorators, read and write; decorator scan is now quote-aware - bun, gorm readers: read generated and identity markers - readmes updated
72 lines
2.3 KiB
Go
72 lines
2.3 KiB
Go
package drizzle
|
|
|
|
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"
|
|
drizzlereader "git.warky.dev/wdevs/relspecgo/pkg/readers/drizzle"
|
|
"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.AutoIncrement = true
|
|
seq.Identity = true
|
|
seq.IdentityGeneration = "BY DEFAULT"
|
|
table.Columns["seq"] = seq
|
|
|
|
first := models.InitColumn("first", "people", "public")
|
|
first.Type = "text"
|
|
table.Columns["first"] = first
|
|
|
|
full := models.InitColumn("full_name", "people", "public")
|
|
full.Type = "text"
|
|
full.Generated = true
|
|
full.GenerationExpression = "first || ' ' || upper(first)"
|
|
table.Columns["full_name"] = full
|
|
|
|
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(), "schema.ts")
|
|
require.NoError(t, NewWriter(&writers.WriterOptions{OutputPath: out}).WriteDatabase(db))
|
|
|
|
content, err := os.ReadFile(out)
|
|
require.NoError(t, err)
|
|
assert.Contains(t, string(content), "generatedAlwaysAs(sql`first || ' ' || upper(first)`)")
|
|
assert.Contains(t, string(content), "generatedAlwaysAsIdentity()")
|
|
assert.Contains(t, string(content), "generatedByDefaultAsIdentity()")
|
|
|
|
got, err := drizzlereader.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.True(t, cols["seq"].Identity)
|
|
assert.Equal(t, "BY DEFAULT", cols["seq"].IdentityGeneration)
|
|
assert.True(t, cols["full_name"].Generated)
|
|
assert.Equal(t, "first || ' ' || upper(first)", cols["full_name"].GenerationExpression)
|
|
assert.False(t, cols["first"].Generated)
|
|
assert.False(t, cols["first"].Identity)
|
|
}
|