-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preserve foreign key constraints on columns duplicated for backfilling (
#230) When duplicating a column for backfilling to add `NOT NULL` constraint, ensure that any foreign keys on the duplicated column are preserved. This fixes the issue where adding `NOT NULL` to an FK column would drop the FK from the column. This is part of a larger class of issues where duplicated columns are not faithfully preserving all properties of the original, tracked in #227.
- Loading branch information
1 parent
4b124b0
commit b81c42c
Showing
5 changed files
with
213 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package migrations | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"slices" | ||
"strings" | ||
|
||
"github.com/lib/pq" | ||
"github.com/xataio/pgroll/pkg/schema" | ||
) | ||
|
||
type Duplicator struct { | ||
conn *sql.DB | ||
table *schema.Table | ||
column *schema.Column | ||
asName string | ||
} | ||
|
||
// NewColumnDuplicator creates a new Duplicator for a column. | ||
func NewColumnDuplicator(conn *sql.DB, table *schema.Table, column *schema.Column) *Duplicator { | ||
return &Duplicator{ | ||
conn: conn, | ||
table: table, | ||
column: column, | ||
asName: TemporaryName(column.Name), | ||
} | ||
} | ||
|
||
// Duplicate creates a new column with the same type and foreign key | ||
// constraints as the original column. | ||
func (d *Duplicator) Duplicate(ctx context.Context) error { | ||
const ( | ||
cAlterTableSQL = `ALTER TABLE %s ADD COLUMN %s %s` | ||
cAddForeignKeySQL = `ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)` | ||
) | ||
|
||
sql := fmt.Sprintf(cAlterTableSQL, | ||
pq.QuoteIdentifier(d.table.Name), | ||
pq.QuoteIdentifier(d.asName), | ||
d.column.Type) | ||
|
||
for _, fk := range d.table.ForeignKeys { | ||
if slices.Contains(fk.Columns, d.column.Name) { | ||
sql += fmt.Sprintf(", "+cAddForeignKeySQL, | ||
pq.QuoteIdentifier(TemporaryName(fk.Name)), | ||
strings.Join(quoteColumnNames(copyAndReplace(fk.Columns, d.column.Name, d.asName)), ", "), | ||
pq.QuoteIdentifier(fk.ReferencedTable), | ||
strings.Join(quoteColumnNames(fk.ReferencedColumns), ", ")) | ||
} | ||
} | ||
|
||
_, err := d.conn.ExecContext(ctx, sql) | ||
|
||
return err | ||
} | ||
|
||
func copyAndReplace(xs []string, oldValue, newValue string) []string { | ||
ys := slices.Clone(xs) | ||
|
||
for i, c := range ys { | ||
if c == oldValue { | ||
ys[i] = newValue | ||
} | ||
} | ||
return ys | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package migrations | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"slices" | ||
|
||
"github.com/lib/pq" | ||
"github.com/xataio/pgroll/pkg/schema" | ||
) | ||
|
||
// RenameDuplicatedColumn renames a duplicated column to its original name and renames any foreign keys | ||
// on the duplicated column to their original name. | ||
func RenameDuplicatedColumn(ctx context.Context, conn *sql.DB, table *schema.Table, column *schema.Column) error { | ||
const ( | ||
cRenameColumnSQL = `ALTER TABLE IF EXISTS %s RENAME COLUMN %s TO %s` | ||
cRenameConstraintSQL = `ALTER TABLE IF EXISTS %s RENAME CONSTRAINT %s TO %s` | ||
) | ||
|
||
// Rename the old column to the new column name | ||
renameColumnSQL := fmt.Sprintf(cRenameColumnSQL, | ||
pq.QuoteIdentifier(table.Name), | ||
pq.QuoteIdentifier(TemporaryName(column.Name)), | ||
pq.QuoteIdentifier(column.Name)) | ||
|
||
_, err := conn.ExecContext(ctx, renameColumnSQL) | ||
if err != nil { | ||
return fmt.Errorf("failed to rename duplicated column %q: %w", column.Name, err) | ||
} | ||
|
||
// Rename any foreign keys on the duplicated column from their temporary name | ||
// to their original name | ||
var renameConstraintSQL string | ||
for _, fk := range table.ForeignKeys { | ||
if slices.Contains(fk.Columns, TemporaryName(column.Name)) { | ||
renameConstraintSQL = fmt.Sprintf(cRenameConstraintSQL, | ||
pq.QuoteIdentifier(table.Name), | ||
pq.QuoteIdentifier(fk.Name), | ||
pq.QuoteIdentifier(StripTemporaryPrefix(fk.Name)), | ||
) | ||
|
||
_, err = conn.ExecContext(ctx, renameConstraintSQL) | ||
if err != nil { | ||
return fmt.Errorf("failed to rename column constraint %q: %w", fk.Name, err) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |