-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a987b95
commit db2d9d1
Showing
3 changed files
with
75 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
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,56 @@ | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
|
||
"github.com/lib/pq" | ||
|
||
"pg-roll/pkg/schema" | ||
) | ||
|
||
type OpRenameColumn struct { | ||
Table string `json:"table"` | ||
From string `json:"from"` | ||
To string `json:"to"` | ||
} | ||
|
||
var _ Operation = (*OpRenameColumn)(nil) | ||
|
||
func (o *OpRenameColumn) Start(ctx context.Context, conn *sql.DB, schemaName string, stateSchema string, s *schema.Schema) error { | ||
table := s.GetTable(o.Table) | ||
table.RenameColumn(o.From, o.To) | ||
return nil | ||
} | ||
|
||
func (o *OpRenameColumn) Complete(ctx context.Context, conn *sql.DB) error { | ||
// rename the column in the underlying table | ||
_, 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 *sql.DB) error { | ||
// no-op | ||
return nil | ||
} | ||
|
||
func (o *OpRenameColumn) Validate(ctx context.Context, s *schema.Schema) error { | ||
table := s.GetTable(o.Table) | ||
if table == nil { | ||
return TableDoesNotExistError{Name: o.Table} | ||
} | ||
|
||
if table.GetColumn(o.From) == nil { | ||
return ColumnDoesNotExistError{Table: o.Table, Name: o.From} | ||
} | ||
|
||
if table.GetColumn(o.To) != nil { | ||
return ColumnAlreadyExistsError{Table: o.Table, Name: o.From} | ||
} | ||
|
||
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