Skip to content

Commit

Permalink
Ensure that inline CHECK constraints are named
Browse files Browse the repository at this point in the history
When converting inline `CHECK` constraints in `CREATE TABLE` statements,
ensure that the constraint is named according to Postgres naming
conventions for inline `CHECK` constraints.
  • Loading branch information
andrew-farries committed Dec 19, 2024
1 parent 001c7c5 commit 5202eb9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
16 changes: 11 additions & 5 deletions pkg/sql2pgroll/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func convertCreateStmt(stmt *pgq.CreateStmt) (migrations.Operations, error) {
for _, elt := range stmt.TableElts {
switch elt.Node.(type) {
case *pgq.Node_ColumnDef:
column, err := convertColumnDef(elt.GetColumnDef())
column, err := convertColumnDef(stmt.Relation.GetRelname(), elt.GetColumnDef())
if err != nil {
return nil, fmt.Errorf("error converting column definition: %w", err)
}
Expand All @@ -41,7 +41,7 @@ func convertCreateStmt(stmt *pgq.CreateStmt) (migrations.Operations, error) {

return migrations.Operations{
&migrations.OpCreateTable{
Name: stmt.Relation.Relname,
Name: stmt.Relation.GetRelname(),
Columns: columns,
},
}, nil
Expand Down Expand Up @@ -76,7 +76,7 @@ func canConvertCreateStatement(stmt *pgq.CreateStmt) bool {
}
}

func convertColumnDef(col *pgq.ColumnDef) (*migrations.Column, error) {
func convertColumnDef(tableName string, col *pgq.ColumnDef) (*migrations.Column, error) {
if !canConvertColumnDef(col) {
return nil, nil
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func convertColumnDef(col *pgq.ColumnDef) (*migrations.Column, error) {
pk = true
notNull = true
case pgq.ConstrType_CONSTR_CHECK:
check, err = convertInlineCheckConstraint(c.GetConstraint())
check, err = convertInlineCheckConstraint(tableName, col.GetColname(), c.GetConstraint())
if err != nil {
return nil, fmt.Errorf("error converting inline check constraint: %w", err)
}
Expand Down Expand Up @@ -171,7 +171,7 @@ func canConvertPrimaryKeyConstraint(constraint *pgq.Constraint) bool {
}
}

func convertInlineCheckConstraint(constraint *pgq.Constraint) (*migrations.CheckConstraint, error) {
func convertInlineCheckConstraint(tableName, columnName string, constraint *pgq.Constraint) (*migrations.CheckConstraint, error) {
if !canConvertCheckConstraint(constraint) {
return nil, nil
}
Expand All @@ -181,7 +181,13 @@ func convertInlineCheckConstraint(constraint *pgq.Constraint) (*migrations.Check
return nil, fmt.Errorf("failed to deparse CHECK expression: %w", err)
}

name := fmt.Sprintf("%s_%s_check", tableName, columnName)
if constraint.GetConname() != "" {
name = constraint.GetConname()
}

return &migrations.CheckConstraint{
Name: name,
Constraint: expr,
}, nil
}
1 change: 1 addition & 0 deletions pkg/sql2pgroll/expect/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ var CreateTableOp10 = &migrations.OpCreateTable{
Type: "int",
Nullable: true,
Check: &migrations.CheckConstraint{
Name: "foo_a_check",
Constraint: "a > 0",
},
},
Expand Down

0 comments on commit 5202eb9

Please sign in to comment.