Skip to content

Commit a6df553

Browse files
Reduce duplication in CREATE TABLE and ADD COLUMN
There is a lot of duplication between the `CREATE TABLE` and `ALTER TABLE ADD COLUMN` conversion code. Reduce the duplication by having the ALTER TABLE ADD COLUMN conversion code and the CREATE TABLE conversion code use the same `convertColumnDef` function, which handles the conversion of a column definition to a `migrations.Column` struct along with the conversion of the column constraints.
1 parent f03b847 commit a6df553

File tree

2 files changed

+12
-96
lines changed

2 files changed

+12
-96
lines changed

pkg/sql2pgroll/alter_table.go

Lines changed: 11 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -406,108 +406,24 @@ func convertAlterTableAddColumn(stmt *pgq.AlterTableStmt, cmd *pgq.AlterTableCmd
406406
return nil, nil
407407
}
408408

409-
columnDef := cmd.GetDef().GetColumnDef()
410-
if !canConvertColumnDef(columnDef) {
411-
return nil, nil
412-
}
413-
414-
columnType, err := pgq.DeparseTypeName(columnDef.GetTypeName())
409+
qualifiedName := getQualifiedRelationName(stmt.GetRelation())
410+
column, err := convertColumnDef(qualifiedName, cmd.GetDef().GetColumnDef())
415411
if err != nil {
416-
return nil, fmt.Errorf("failed to deparse type name: %w", err)
412+
return nil, fmt.Errorf("error converting column definition: %w", err)
417413
}
418-
419-
operation := &migrations.OpAddColumn{
420-
Column: migrations.Column{
421-
Name: columnDef.GetColname(),
422-
Type: columnType,
423-
Nullable: true,
424-
},
425-
Table: getQualifiedRelationName(stmt.GetRelation()),
426-
Up: PlaceHolderSQL,
427-
}
428-
429-
if len(columnDef.GetConstraints()) > 0 {
430-
for _, constraint := range columnDef.GetConstraints() {
431-
switch constraint.GetConstraint().GetContype() {
432-
case pgq.ConstrType_CONSTR_NULL:
433-
operation.Column.Nullable = true
434-
case pgq.ConstrType_CONSTR_NOTNULL:
435-
operation.Column.Nullable = false
436-
case pgq.ConstrType_CONSTR_PRIMARY:
437-
operation.Column.Pk = true
438-
operation.Column.Nullable = false
439-
case pgq.ConstrType_CONSTR_UNIQUE:
440-
operation.Column.Unique = true
441-
case pgq.ConstrType_CONSTR_CHECK:
442-
raw, err := pgq.DeparseExpr(constraint.GetConstraint().GetRawExpr())
443-
if err != nil {
444-
return nil, fmt.Errorf("failed to deparse raw expression: %w", err)
445-
}
446-
operation.Column.Check = &migrations.CheckConstraint{
447-
Constraint: raw,
448-
Name: constraint.GetConstraint().GetConname(),
449-
}
450-
case pgq.ConstrType_CONSTR_DEFAULT:
451-
defaultExpr := constraint.GetConstraint().GetRawExpr()
452-
def, err := extractDefault(defaultExpr)
453-
if err != nil {
454-
return nil, err
455-
}
456-
if !def.IsNull() {
457-
v := def.MustGet()
458-
operation.Column.Default = &v
459-
}
460-
case pgq.ConstrType_CONSTR_FOREIGN:
461-
onDelete, err := parseOnDeleteAction(constraint.GetConstraint().GetFkDelAction())
462-
if err != nil {
463-
return nil, err
464-
}
465-
fk := &migrations.ForeignKeyReference{
466-
Name: constraint.GetConstraint().GetConname(),
467-
OnDelete: onDelete,
468-
Column: constraint.GetConstraint().GetPkAttrs()[0].GetString_().GetSval(),
469-
Table: getQualifiedRelationName(constraint.GetConstraint().GetPktable()),
470-
}
471-
operation.Column.References = fk
472-
}
473-
}
414+
if column == nil {
415+
return nil, nil
474416
}
475417

476-
return operation, nil
418+
return &migrations.OpAddColumn{
419+
Column: *column,
420+
Table: qualifiedName,
421+
Up: PlaceHolderSQL,
422+
}, nil
477423
}
478424

479425
func canConvertAddColumn(cmd *pgq.AlterTableCmd) bool {
480-
if cmd.GetMissingOk() {
481-
return false
482-
}
483-
for _, constraint := range cmd.GetDef().GetColumnDef().GetConstraints() {
484-
switch constraint.GetConstraint().GetContype() {
485-
case pgq.ConstrType_CONSTR_DEFAULT,
486-
pgq.ConstrType_CONSTR_NULL,
487-
pgq.ConstrType_CONSTR_NOTNULL,
488-
pgq.ConstrType_CONSTR_PRIMARY,
489-
pgq.ConstrType_CONSTR_UNIQUE,
490-
pgq.ConstrType_CONSTR_FOREIGN,
491-
pgq.ConstrType_CONSTR_CHECK:
492-
switch constraint.GetConstraint().GetFkUpdAction() {
493-
case "r", "c", "n", "d":
494-
// RESTRICT, CASCADE, SET NULL, SET DEFAULT
495-
return false
496-
case "a":
497-
// NO ACTION, the default
498-
break
499-
}
500-
case pgq.ConstrType_CONSTR_ATTR_DEFERRABLE,
501-
pgq.ConstrType_CONSTR_ATTR_DEFERRED,
502-
pgq.ConstrType_CONSTR_IDENTITY,
503-
pgq.ConstrType_CONSTR_GENERATED:
504-
return false
505-
case pgq.ConstrType_CONSTR_ATTR_NOT_DEFERRABLE, pgq.ConstrType_CONSTR_ATTR_IMMEDIATE:
506-
break
507-
}
508-
}
509-
510-
return true
426+
return !cmd.GetMissingOk()
511427
}
512428

513429
func convertAlterTableDropColumn(stmt *pgq.AlterTableStmt, cmd *pgq.AlterTableCmd) (migrations.Operation, error) {

pkg/sql2pgroll/expect/add_column.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ var AddColumnOp6 = &migrations.OpAddColumn{
8080
Nullable: true,
8181
Check: &migrations.CheckConstraint{
8282
Constraint: "bar > 0",
83-
Name: "",
83+
Name: "foo_bar_check",
8484
},
8585
},
8686
}

0 commit comments

Comments
 (0)