- 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
65 lines
1.8 KiB
Go
65 lines
1.8 KiB
Go
package sqlite
|
|
|
|
import (
|
|
"database/sql"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"git.warky.dev/wdevs/relspecgo/pkg/readers"
|
|
)
|
|
|
|
func TestParseGeneratedExpression(t *testing.T) {
|
|
createSQL := `CREATE TABLE people (
|
|
id INTEGER PRIMARY KEY,
|
|
"first" TEXT,
|
|
last TEXT,
|
|
full_name TEXT GENERATED ALWAYS AS (coalesce("first", '') || ' ' || last) STORED,
|
|
initials TEXT AS (substr("first", 1, 1) || substr(last, 1, 1)),
|
|
plain TEXT NOT NULL
|
|
)`
|
|
|
|
tests := []struct {
|
|
column string
|
|
want string
|
|
}{
|
|
{"full_name", `coalesce("first", '') || ' ' || last`},
|
|
{"initials", `substr("first", 1, 1) || substr(last, 1, 1)`},
|
|
{"plain", ""},
|
|
{"missing", ""},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.column, func(t *testing.T) {
|
|
assert.Equal(t, tt.want, parseGeneratedExpression(createSQL, tt.column))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestReader_GeneratedColumns(t *testing.T) {
|
|
dbPath := filepath.Join(t.TempDir(), "gen.db")
|
|
db, err := sql.Open("sqlite", dbPath)
|
|
require.NoError(t, err)
|
|
_, err = db.Exec(`CREATE TABLE people (
|
|
id INTEGER PRIMARY KEY,
|
|
first TEXT,
|
|
last TEXT,
|
|
full_name TEXT GENERATED ALWAYS AS (first || ' ' || last) STORED,
|
|
initials TEXT AS (substr(first, 1, 1) || substr(last, 1, 1))
|
|
)`)
|
|
require.NoError(t, err)
|
|
require.NoError(t, db.Close())
|
|
|
|
got, err := NewReader(&readers.ReaderOptions{FilePath: dbPath}).ReadDatabase()
|
|
require.NoError(t, err)
|
|
|
|
cols := got.Schemas[0].Tables[0].Columns
|
|
require.Contains(t, cols, "full_name", "generated columns must be read")
|
|
assert.True(t, cols["full_name"].Generated)
|
|
assert.Equal(t, "first || ' ' || last", cols["full_name"].GenerationExpression)
|
|
assert.True(t, cols["initials"].Generated)
|
|
assert.Equal(t, "substr(first, 1, 1) || substr(last, 1, 1)", cols["initials"].GenerationExpression)
|
|
assert.False(t, cols["first"].Generated)
|
|
}
|