Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions internal/migration_acceptance_tests/column_cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,149 @@ var columnAcceptanceTestCases = []acceptanceTestCase{
`,
},
},
{
name: "Add generated column",
oldSchemaDDL: []string{
`
CREATE TABLE tabs (
id SERIAL PRIMARY KEY,
title TEXT NOT NULL,
artist TEXT
);
`,
},
newSchemaDDL: []string{
`
CREATE TABLE tabs (
id SERIAL PRIMARY KEY,
title TEXT NOT NULL,
artist TEXT,
search_vector tsvector GENERATED ALWAYS AS (
to_tsvector('simple', title || ' ' || coalesce(artist, ''))
) STORED
);
`,
},
},
{
name: "Drop generated column",
oldSchemaDDL: []string{
`
CREATE TABLE tabs (
id SERIAL PRIMARY KEY,
title TEXT NOT NULL,
artist TEXT,
search_vector tsvector GENERATED ALWAYS AS (
to_tsvector('simple', title || ' ' || coalesce(artist, ''))
) STORED
);
`,
},
newSchemaDDL: []string{
`
CREATE TABLE tabs (
id SERIAL PRIMARY KEY,
title TEXT NOT NULL,
artist TEXT
);
`,
},
expectedHazardTypes: []diff.MigrationHazardType{
diff.MigrationHazardTypeDeletesData,
},
},
{
name: "Add multiple generated columns",
oldSchemaDDL: []string{
`
CREATE TABLE products (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
price NUMERIC(10,2) NOT NULL
);
`,
},
newSchemaDDL: []string{
`
CREATE TABLE products (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
price NUMERIC(10,2) NOT NULL,
price_with_tax NUMERIC(10,2) GENERATED ALWAYS AS (price * 1.1) STORED,
display_name TEXT GENERATED ALWAYS AS (upper(name)) STORED
);
`,
},
// expectedDBSchemaDDL reflects the actual column order after migration
// PostgreSQL adds columns in the order of execution, not declaration order
expectedDBSchemaDDL: []string{
`
CREATE TABLE products (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
price NUMERIC(10,2) NOT NULL,
display_name TEXT GENERATED ALWAYS AS (upper(name)) STORED,
price_with_tax NUMERIC(10,2) GENERATED ALWAYS AS (price * 1.1) STORED
);
`,
},
},
{
name: "Generated column with index",
oldSchemaDDL: []string{
`
CREATE TABLE articles (
id SERIAL PRIMARY KEY,
title TEXT NOT NULL,
content TEXT
);
`,
},
newSchemaDDL: []string{
`
CREATE TABLE articles (
id SERIAL PRIMARY KEY,
title TEXT NOT NULL,
content TEXT,
search_vector tsvector GENERATED ALWAYS AS (
to_tsvector('english', title || ' ' || coalesce(content, ''))
) STORED
);
CREATE INDEX idx_articles_search_vector ON articles USING gin (search_vector);
`,
},
expectedHazardTypes: []diff.MigrationHazardType{
diff.MigrationHazardTypeIndexBuild,
},
},
{
name: "Generated column no-op",
oldSchemaDDL: []string{
`
CREATE TABLE tabs (
id SERIAL PRIMARY KEY,
title TEXT NOT NULL,
artist TEXT,
search_vector tsvector GENERATED ALWAYS AS (
to_tsvector('simple', title || ' ' || coalesce(artist, ''))
) STORED
);
`,
},
newSchemaDDL: []string{
`
CREATE TABLE tabs (
id SERIAL PRIMARY KEY,
title TEXT NOT NULL,
artist TEXT,
search_vector tsvector GENERATED ALWAYS AS (
to_tsvector('simple', title || ' ' || coalesce(artist, ''))
) STORED
);
`,
},
expectEmptyPlan: true,
},
}

func TestColumnTestCases(t *testing.T) {
Expand Down
21 changes: 16 additions & 5 deletions internal/queries/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@ WITH identity_col_seq AS (

SELECT
a.attname::TEXT AS column_name,
COALESCE(coll.collname, '')::TEXT AS collation_name,
COALESCE(collation_namespace.nspname, '')::TEXT AS collation_schema_name,
COALESCE(
pg_catalog.pg_get_expr(d.adbin, d.adrelid), ''
)::TEXT AS default_value,
a.attnotnull AS is_not_null,
a.attlen AS column_size,
a.attidentity::TEXT AS identity_type,
Expand All @@ -103,6 +98,22 @@ SELECT
identity_col_seq.seqmin AS min_value,
identity_col_seq.seqcache AS cache_size,
identity_col_seq.seqcycle AS is_cycle,
COALESCE(coll.collname, '')::TEXT AS collation_name,
COALESCE(collation_namespace.nspname, '')::TEXT AS collation_schema_name,
COALESCE(
CASE
WHEN a.attgenerated = 's' THEN ''
ELSE pg_catalog.pg_get_expr(d.adbin, d.adrelid)
END, ''
)::TEXT AS default_value,
COALESCE(
CASE
WHEN a.attgenerated = 's'
THEN pg_catalog.pg_get_expr(d.adbin, d.adrelid)
ELSE ''
END, ''
)::TEXT AS generation_expression,
(a.attgenerated = 's') AS is_generated,
pg_catalog.format_type(a.atttypid, a.atttypmod) AS column_type
FROM pg_catalog.pg_attribute AS a
LEFT JOIN
Expand Down
59 changes: 37 additions & 22 deletions internal/queries/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 15 additions & 5 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,16 @@ type (
// ''::text
// CURRENT_TIMESTAMP
// If empty, indicates that there is no default value.
Default string
IsNullable bool
Default string
// If the column is a generated column, this will be true.
IsGenerated bool
// If the column is a generated column, this will be the generation expression.
// Examples:
// to_tsvector('simple', title || ' ' || coalesce(artist, ''))
// (price * 1.1)
// Only populated if IsGenerated is true.
GenerationExpression string
IsNullable bool
// Size is the number of bytes required to store the value.
// It is used for data-packing purposes
Size int
Expand Down Expand Up @@ -981,9 +989,11 @@ func (s *schemaFetcher) buildTable(
// ''::text
// CURRENT_TIMESTAMP
// If empty, indicates that there is no default value.
Default: column.DefaultValue,
Size: int(column.ColumnSize),
Identity: identity,
Default: column.DefaultValue,
IsGenerated: column.IsGenerated,
GenerationExpression: column.GenerationExpression,
Size: int(column.ColumnSize),
Identity: identity,
})
}

Expand Down
4 changes: 2 additions & 2 deletions internal/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ var (
SELECT id, author
FROM schema_2.foo;
`},
expectedHash: "f0fb3f95f68ba482",
expectedHash: "ff9ed400558572aa",
expectedSchema: Schema{
NamedSchemas: []NamedSchema{
{Name: "public"},
Expand Down Expand Up @@ -571,7 +571,7 @@ var (
ALTER TABLE foo_fk_1 ADD CONSTRAINT foo_fk_1_fk FOREIGN KEY (author, content) REFERENCES foo_1 (author, content)
NOT VALID;
`},
expectedHash: "bcad7c978a081c30",
expectedHash: "9647ef46a878d426",
expectedSchema: Schema{
NamedSchemas: []NamedSchema{
{Name: "public"},
Expand Down
8 changes: 5 additions & 3 deletions pkg/diff/sql_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2637,12 +2637,14 @@ func buildColumnDefinition(column schema.Column) (string, error) {
if column.IsCollated() {
sb.WriteString(fmt.Sprintf(" COLLATE %s", column.Collation.GetFQEscapedName()))
}
if column.IsGenerated {
sb.WriteString(fmt.Sprintf(" GENERATED ALWAYS AS (%s) STORED", column.GenerationExpression))
} else if len(column.Default) > 0 {
sb.WriteString(fmt.Sprintf(" DEFAULT %s", column.Default))
}
if !column.IsNullable {
sb.WriteString(" NOT NULL")
}
if len(column.Default) > 0 {
sb.WriteString(fmt.Sprintf(" DEFAULT %s", column.Default))
}
if column.Identity != nil {
identityDef, err := buildColumnIdentityDefinition(*column.Identity)
if err != nil {
Expand Down
Loading