feat: generated and identity columns for mssql, sqlite, drizzle, typeorm, bun and gorm readers
- 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
This commit is contained in:
@@ -113,6 +113,7 @@ export const postsRelations = relations(posts, ({ one }) => ({
|
||||
|
||||
## Notes
|
||||
|
||||
- Generated columns: `.generatedAlwaysAs(sql`expr`)`; identity columns: `.generatedAlwaysAsIdentity()` / `.generatedByDefaultAsIdentity()`
|
||||
- Table names and column names are preserved as-is
|
||||
- Relationships are generated as separate relation definitions
|
||||
- Constraint actions (CASCADE, etc.) are included in references
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
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)
|
||||
}
|
||||
@@ -140,7 +140,13 @@ func (tm *TypeMapper) buildColumnModifiers(col *models.Column, table *models.Tab
|
||||
}
|
||||
|
||||
// Default value
|
||||
if col.AutoIncrement {
|
||||
if col.Generated && col.GenerationExpression != "" {
|
||||
// GENERATED ALWAYS AS (expr) STORED
|
||||
expr := strings.ReplaceAll(col.GenerationExpression, "`", "\\`")
|
||||
modifiers = append(modifiers, fmt.Sprintf("generatedAlwaysAs(sql`%s`)", expr))
|
||||
} else if col.Identity && strings.EqualFold(col.IdentityGeneration, "BY DEFAULT") {
|
||||
modifiers = append(modifiers, "generatedByDefaultAsIdentity()")
|
||||
} else if col.AutoIncrement || col.Identity {
|
||||
// For auto-increment, use generatedAlwaysAsIdentity()
|
||||
modifiers = append(modifiers, "generatedAlwaysAsIdentity()")
|
||||
} else if col.Default != nil {
|
||||
|
||||
Reference in New Issue
Block a user