Skip to content
Draft
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
46 changes: 46 additions & 0 deletions internal/migration_acceptance_tests/table_cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,52 @@ var tableAcceptanceTestCases = []acceptanceTestCase{
`,
},
},
{
name: "Set table unlogged",
oldSchemaDDL: []string{
`
CREATE TABLE foobar(
id INT PRIMARY KEY
);
`,
},
newSchemaDDL: []string{
`
CREATE UNLOGGED TABLE foobar(
id INT PRIMARY KEY
);
`,
},
expectedPlanDDL: []string{
`ALTER TABLE "public"."foobar" SET UNLOGGED`,
},
expectedHazardTypes: []diff.MigrationHazardType{
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
},
},
{
name: "Set table logged",
oldSchemaDDL: []string{
`
CREATE UNLOGGED TABLE foobar(
id INT PRIMARY KEY
);
`,
},
newSchemaDDL: []string{
`
CREATE TABLE foobar(
id INT PRIMARY KEY
);
`,
},
expectedPlanDDL: []string{
`ALTER TABLE "public"."foobar" SET LOGGED`,
},
expectedHazardTypes: []diff.MigrationHazardType{
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
},
},
{
name: "Create table with RLS enabled",
oldSchemaDDL: nil,
Expand Down
1 change: 1 addition & 0 deletions internal/queries/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ SELECT
c.oid,
c.relname::TEXT AS table_name,
table_namespace.nspname::TEXT AS table_schema_name,
c.relpersistence = 'u' AS is_unlogged,
c.relreplident::TEXT AS replica_identity,
c.relrowsecurity AS rls_enabled,
c.relforcerowsecurity AS rls_forced,
Expand Down
3 changes: 3 additions & 0 deletions internal/queries/queries.sql.go

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

2 changes: 2 additions & 0 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ type Table struct {
CheckConstraints []CheckConstraint
Policies []Policy
Privileges []TablePrivilege
IsUnlogged bool
ReplicaIdentity ReplicaIdentity
RLSEnabled bool
RLSForced bool
Expand Down Expand Up @@ -1056,6 +1057,7 @@ func (s *schemaFetcher) buildTable(
CheckConstraints: checkConsByTable[schemaQualifiedName.GetFQEscapedName()],
Policies: policiesByTable[schemaQualifiedName.GetFQEscapedName()],
Privileges: privilegesByTable[schemaQualifiedName.GetFQEscapedName()],
IsUnlogged: table.IsUnlogged,
ReplicaIdentity: ReplicaIdentity(table.ReplicaIdentity),
RLSEnabled: table.RlsEnabled,
RLSForced: table.RlsForced,
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 @@ -239,7 +239,7 @@ var (
GRANT SELECT ON schema_2.foo TO some_role_1;
GRANT INSERT ON schema_2.foo TO some_role_2 WITH GRANT OPTION;
`},
expectedHash: "4c2174e2cac3956b",
expectedHash: "63bdda1e60bbc81",
expectedSchema: Schema{
NamedSchemas: []NamedSchema{
{Name: "public"},
Expand Down Expand Up @@ -591,7 +591,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: "32c5a9c52dcfb15e",
expectedHash: "528eed3f5ac35bd1",
expectedSchema: Schema{
NamedSchemas: []NamedSchema{
{Name: "public"},
Expand Down
27 changes: 26 additions & 1 deletion pkg/diff/sql_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,12 @@ func (t *tableSQLVertexGenerator) Add(table schema.Table) ([]Statement, error) {
columnDefs = append(columnDefs, "\t"+columnDef)
}
createTableSb := strings.Builder{}
createTableSb.WriteString(fmt.Sprintf("CREATE TABLE %s (\n%s\n)",
tableKind := "TABLE"
if table.IsUnlogged {
tableKind = "UNLOGGED TABLE"
}
createTableSb.WriteString(fmt.Sprintf("CREATE %s %s (\n%s\n)",
tableKind,
table.GetFQEscapedName(),
strings.Join(columnDefs, ",\n"),
))
Expand Down Expand Up @@ -954,6 +959,10 @@ func (t *tableSQLVertexGenerator) Alter(diff tableDiff) ([]Statement, error) {
stmts = append(stmts, alterBaseTableStmts...)
}

if diff.old.IsUnlogged != diff.new.IsUnlogged {
stmts = append(stmts, alterTablePersistenceStatement(diff.new.SchemaQualifiedName, diff.new.IsUnlogged))
}

if diff.old.ReplicaIdentity != diff.new.ReplicaIdentity {
alterReplicaIdentityStmt, err := alterReplicaIdentityStatement(diff.new.SchemaQualifiedName, diff.new.ReplicaIdentity)
if err != nil {
Expand Down Expand Up @@ -1060,6 +1069,22 @@ func (t *tableSQLVertexGenerator) alterBaseTable(diff tableDiff) ([]Statement, e
return stmts, nil
}

func alterTablePersistenceStatement(table schema.SchemaQualifiedName, isUnlogged bool) Statement {
persistence := "LOGGED"
if isUnlogged {
persistence = "UNLOGGED"
}
return Statement{
DDL: fmt.Sprintf("%s SET %s", alterTablePrefix(table), persistence),
Timeout: statementTimeoutDefault,
LockTimeout: lockTimeoutDefault,
Hazards: []MigrationHazard{{
Type: MigrationHazardTypeAcquiresAccessExclusiveLock,
Message: "Changing table persistence requires an ACCESS EXCLUSIVE lock and rewrites the table",
}},
}
}

func (t *tableSQLVertexGenerator) alterPartition(diff tableDiff) ([]Statement, error) {
if diff.old.ForValues != diff.new.ForValues {
return nil, fmt.Errorf("altering partition FOR VALUES: %w", ErrNotImplemented)
Expand Down