Skip to content

Commit

Permalink
Convert SQL CREATE TABLE inline CHECK constraints (#552)
Browse files Browse the repository at this point in the history
Convert SQL `CREATE TABLE` statements with inline `CHECK` constraints
like this:

```
CREATE TABLE foo(a int CHECK (a > 0))
```

to `OpCreateTable` operations like this:

```json
[
  {
    "create_table": {
      "columns": [
        {
          "check": {
            "constraint": "a > 0",
            "name": "foo_a_check"
          },
          "name": "a",
          "nullable": true,
          "type": "int"
        }
      ],
      "name": "foo"
    }
  }
]
```
  • Loading branch information
andrew-farries authored Dec 19, 2024
1 parent 3d0b93f commit 5450d7a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
35 changes: 31 additions & 4 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 All @@ -97,6 +97,7 @@ func convertColumnDef(col *pgq.ColumnDef) (*migrations.Column, error) {

// Convert column constraints
var notNull, pk, unique bool
var check *migrations.CheckConstraint
for _, c := range col.GetConstraints() {
switch c.GetConstraint().GetContype() {
case pgq.ConstrType_CONSTR_NULL:
Expand All @@ -115,7 +116,11 @@ func convertColumnDef(col *pgq.ColumnDef) (*migrations.Column, error) {
pk = true
notNull = true
case pgq.ConstrType_CONSTR_CHECK:
if !canConvertCheckConstraint(c.GetConstraint()) {
check, err = convertInlineCheckConstraint(tableName, col.GetColname(), c.GetConstraint())
if err != nil {
return nil, fmt.Errorf("error converting inline check constraint: %w", err)
}
if check == nil {
return nil, nil
}
case pgq.ConstrType_CONSTR_FOREIGN:
Expand All @@ -130,6 +135,7 @@ func convertColumnDef(col *pgq.ColumnDef) (*migrations.Column, error) {
Type: typeString,
Nullable: !notNull,
Pk: pk,
Check: check,
Unique: unique,
}, nil
}
Expand Down Expand Up @@ -164,3 +170,24 @@ func canConvertPrimaryKeyConstraint(constraint *pgq.Constraint) bool {
return true
}
}

func convertInlineCheckConstraint(tableName, columnName string, constraint *pgq.Constraint) (*migrations.CheckConstraint, error) {
if !canConvertCheckConstraint(constraint) {
return nil, nil
}

expr, err := pgq.DeparseExpr(constraint.GetRawExpr())
if err != nil {
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
}
20 changes: 12 additions & 8 deletions pkg/sql2pgroll/create_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ func TestConvertCreateTableStatements(t *testing.T) {
sql: "CREATE TABLE foo(a int NOT NULL)",
expectedOp: expect.CreateTableOp2,
},
{
sql: "CREATE TABLE foo(a varchar(255))",
expectedOp: expect.CreateTableOp3,
},
{
sql: "CREATE TABLE foo(a numeric(10, 2))",
expectedOp: expect.CreateTableOp4,
},
{
sql: "CREATE TABLE foo(a int UNIQUE)",
expectedOp: expect.CreateTableOp5,
Expand All @@ -48,6 +40,18 @@ func TestConvertCreateTableStatements(t *testing.T) {
sql: "CREATE TABLE foo(a int PRIMARY KEY)",
expectedOp: expect.CreateTableOp6,
},
{
sql: "CREATE TABLE foo(a int CHECK (a > 0))",
expectedOp: expect.CreateTableOp10,
},
{
sql: "CREATE TABLE foo(a varchar(255))",
expectedOp: expect.CreateTableOp3,
},
{
sql: "CREATE TABLE foo(a numeric(10, 2))",
expectedOp: expect.CreateTableOp4,
},
{
sql: "CREATE TABLE foo(a text[])",
expectedOp: expect.CreateTableOp7,
Expand Down
15 changes: 15 additions & 0 deletions pkg/sql2pgroll/expect/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,18 @@ var CreateTableOp9 = &migrations.OpCreateTable{
},
},
}

var CreateTableOp10 = &migrations.OpCreateTable{
Name: "foo",
Columns: []migrations.Column{
{
Name: "a",
Type: "int",
Nullable: true,
Check: &migrations.CheckConstraint{
Name: "foo_a_check",
Constraint: "a > 0",
},
},
},
}

0 comments on commit 5450d7a

Please sign in to comment.