-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
OpRenameColumn
for renaming columns (#602)
Add a new `rename_column` operation for performing column renames: ```json { "name": "13_rename_column", "operations": [ { "rename_column": { "table": "employees", "from": "role", "to": "job_title" } } ] } ``` Renaming columns is currently done using the `alter_column` operation: ```json { "name": "13_rename_column", "operations": [ { "alter_column": { "table": "employees", "column": "role", "name": "job_title" } } ] } ``` As of this PR both approaches are valid, but the second approach (using `alter_column` to perform column renames) will be removed as part of #601 Part of #601
- Loading branch information
1 parent
5df19ff
commit 895afe4
Showing
10 changed files
with
278 additions
and
9 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
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
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,64 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/lib/pq" | ||
"github.com/xataio/pgroll/pkg/db" | ||
"github.com/xataio/pgroll/pkg/schema" | ||
) | ||
|
||
var _ Operation = (*OpRenameColumn)(nil) | ||
|
||
func (o *OpRenameColumn) Start(ctx context.Context, conn db.DB, latestSchema string, tr SQLTransformer, s *schema.Schema, cbs ...CallbackFn) (*schema.Table, error) { | ||
// Rename the table in the in-memory schema. | ||
table := s.GetTable(o.Table) | ||
table.RenameColumn(o.From, o.To) | ||
|
||
return nil, nil | ||
} | ||
|
||
func (o *OpRenameColumn) Complete(ctx context.Context, conn db.DB, tr SQLTransformer, s *schema.Schema) error { | ||
// Rename the column | ||
_, err := conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE IF EXISTS %s RENAME COLUMN %s TO %s", | ||
pq.QuoteIdentifier(o.Table), | ||
pq.QuoteIdentifier(o.From), | ||
pq.QuoteIdentifier(o.To))) | ||
|
||
return err | ||
} | ||
|
||
func (o *OpRenameColumn) Rollback(ctx context.Context, conn db.DB, tr SQLTransformer, s *schema.Schema) error { | ||
// Rename the column back to the original name in the in-memory schema. | ||
table := s.GetTable(o.Table) | ||
table.RenameColumn(o.To, o.From) | ||
|
||
return nil | ||
} | ||
|
||
func (o *OpRenameColumn) Validate(ctx context.Context, s *schema.Schema) error { | ||
table := s.GetTable(o.Table) | ||
|
||
// Ensure that the table exists. | ||
if table == nil { | ||
return TableDoesNotExistError{Name: o.Table} | ||
} | ||
|
||
// Ensure that the column exists. | ||
if table.GetColumn(o.From) == nil { | ||
return ColumnDoesNotExistError{Table: o.Table, Name: o.From} | ||
} | ||
|
||
// Ensure that the new column name does not already exist | ||
if table.GetColumn(o.To) != nil { | ||
return ColumnAlreadyExistsError{Table: o.Table, Name: o.To} | ||
} | ||
|
||
// Update the in-memory schema to reflect the column rename so that it is | ||
// visible to subsequent operations' validation steps. | ||
table.RenameColumn(o.From, o.To) | ||
|
||
return nil | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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