Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constraints option to create_table and unique constraint support #585

Merged
merged 14 commits into from
Jan 13, 2025
42 changes: 16 additions & 26 deletions pkg/migrations/op_create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,9 @@ func constraintsToSQL(constraints []Constraint) (string, error) {
Columns: c.Columns,
InitiallyDeferred: c.InitiallyDeferred,
Deferrable: c.Deferrable,
}
if c.IndexParameters != nil {
writer.IncludeColumns = c.IndexParameters.IncludeColumns
if c.IndexParameters.StorageParameters != nil {
writer.StorageParameters = *c.IndexParameters.StorageParameters
}
if c.IndexParameters.Tablespace != nil {
writer.Tablespace = *c.IndexParameters.Tablespace
}
IncludeColumns: c.IndexParameters.IncludeColumns,
StorageParameters: c.IndexParameters.StorageParameters,
Tablespace: c.IndexParameters.Tablespace,
}

switch c.Type { //nolint:gocritic // more cases are coming soon
Expand All @@ -220,22 +214,22 @@ func constraintsToSQL(constraints []Constraint) (string, error) {
type ConstraintSQLWriter struct {
Name string
Columns []string
InitiallyDeferred *bool
Deferrable *bool
InitiallyDeferred bool
Deferrable bool

// unique, exclude, primary key constraints support the following options
IncludeColumns []string
StorageParameters string
Tablespace string
}

func (w *ConstraintSQLWriter) WriteUnique(nullsNotDistinct *bool) string {
func (w *ConstraintSQLWriter) WriteUnique(nullsNotDistinct bool) string {
var constraint string
if w.Name != "" {
constraint = fmt.Sprintf("CONSTRAINT %s ", pq.QuoteIdentifier(w.Name))
}
nullsDistinct := ""
if nullsNotDistinct != nil && *nullsNotDistinct {
if nullsNotDistinct {
nullsDistinct = "NULLS NOT DISTINCT"
}
constraint += fmt.Sprintf("UNIQUE %s (%s)", nullsDistinct, strings.Join(quoteColumnNames(w.Columns), ", "))
Expand All @@ -259,23 +253,19 @@ func (w *ConstraintSQLWriter) addIndexParameters() string {
}

func (w *ConstraintSQLWriter) addDeferrable() string {
if w.InitiallyDeferred != nil && *w.Deferrable {
if !w.InitiallyDeferred && !w.Deferrable {
return ""
}
deferrable := ""
if w.Deferrable != nil {
if *w.Deferrable {
deferrable += " DEFERRABLE"
} else {
deferrable += " NOT DEFERRABLE"
}
if w.Deferrable {
deferrable += " DEFERRABLE"
} else {
deferrable += " NOT DEFERRABLE"
}
if w.InitiallyDeferred != nil {
if *w.InitiallyDeferred {
deferrable = " INITIALLY DEFERRED"
} else {
deferrable = " INITIALLY IMMEDIATE"
}
if w.InitiallyDeferred {
deferrable += " INITIALLY DEFERRED"
} else {
deferrable += " INITIALLY IMMEDIATE"
}
return deferrable
}
10 changes: 5 additions & 5 deletions pkg/migrations/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/sql2pgroll/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func Convert(sql string) (migrations.Operations, error) {
}

if ops == nil {
fmt.Println("ops is nil")
kvch marked this conversation as resolved.
Show resolved Hide resolved
return makeRawSQLOperation(sql), nil
}

Expand Down
23 changes: 8 additions & 15 deletions pkg/sql2pgroll/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
func convertCreateStmt(stmt *pgq.CreateStmt) (migrations.Operations, error) {
// Check if the statement can be converted
if !canConvertCreateStatement(stmt) {
fmt.Println("cannot convert create statement")
kvch marked this conversation as resolved.
Show resolved Hide resolved
return nil, nil
}

Expand Down Expand Up @@ -203,12 +204,12 @@ func convertColumnDef(tableName string, col *pgq.ColumnDef) (*migrations.Column,

func convertConstraint(c *pgq.Constraint) (*migrations.Constraint, error) {
var constraintType migrations.ConstraintType
var nullsNotDistinct *bool
var nullsNotDistinct bool

switch c.Contype {
case pgq.ConstrType_CONSTR_UNIQUE:
constraintType = migrations.ConstraintTypeUnique
nullsNotDistinct = ptr(c.NullsNotDistinct)
nullsNotDistinct = c.NullsNotDistinct
default:
return nil, nil
}
Expand Down Expand Up @@ -240,20 +241,12 @@ func convertConstraint(c *pgq.Constraint) (*migrations.Constraint, error) {
}
options[i] = fmt.Sprintf("%s = '%s'", option.GetDefElem().Defname, val)
}
var storageParameters *string
if len(options) != 0 {
storageParameters = ptr(strings.Join(options, ", "))
}

var tablespace *string
if c.Indexspace != "" {
tablespace = ptr(c.Indexspace)
}
var indexParameters *migrations.ConstraintIndexParameters
if storageParameters != nil || tablespace != nil || len(including) != 0 {
if len(options) != 0 || c.Indexspace != "" || len(including) != 0 {
indexParameters = &migrations.ConstraintIndexParameters{
StorageParameters: storageParameters,
Tablespace: tablespace,
StorageParameters: strings.Join(options, ", "),
Tablespace: c.Indexspace,
IncludeColumns: including,
}
}
Expand All @@ -263,8 +256,8 @@ func convertConstraint(c *pgq.Constraint) (*migrations.Constraint, error) {
Type: constraintType,
Columns: columns,
NullsNotDistinct: nullsNotDistinct,
Deferrable: ptr(c.Deferrable),
InitiallyDeferred: ptr(c.Initdeferred),
Deferrable: c.Deferrable,
InitiallyDeferred: c.Initdeferred,
IndexParameters: indexParameters,
}, nil
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/sql2pgroll/create_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package sql2pgroll_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -181,6 +182,7 @@ func TestConvertCreateTableStatements(t *testing.T) {

require.Len(t, ops, 1)

fmt.Println(ops[0])
kvch marked this conversation as resolved.
Show resolved Hide resolved
createTableOp, ok := ops[0].(*migrations.OpCreateTable)
require.True(t, ok)

Expand Down
16 changes: 8 additions & 8 deletions pkg/sql2pgroll/expect/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ var CreateTableOp22 = &migrations.OpCreateTable{
{
Type: migrations.ConstraintTypeUnique,
Columns: []string{"b", "c"},
NullsNotDistinct: ptr(false),
Deferrable: ptr(false),
InitiallyDeferred: ptr(false),
NullsNotDistinct: false,
Deferrable: false,
InitiallyDeferred: false,
},
},
}
Expand All @@ -343,13 +343,13 @@ var CreateTableOp23 = &migrations.OpCreateTable{
{
Type: migrations.ConstraintTypeUnique,
Columns: []string{"b"},
NullsNotDistinct: ptr(false),
Deferrable: ptr(false),
InitiallyDeferred: ptr(false),
NullsNotDistinct: false,
Deferrable: false,
InitiallyDeferred: false,
IndexParameters: &migrations.ConstraintIndexParameters{
IncludeColumns: []string{"c"},
StorageParameters: ptr("fillfactor = '70'"),
Tablespace: ptr("my_tablespace"),
StorageParameters: "fillfactor = '70'",
Tablespace: "my_tablespace",
},
},
},
Expand Down
15 changes: 10 additions & 5 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,25 +120,30 @@
},
"deferrable": {
"description": "Deferable constraint",
"type": "boolean"
"type": "boolean",
"default": false
},
"initially_deferred": {
"description": "Initially deferred constraint",
"type": "boolean"
"type": "boolean",
"default": false
},
"nulls_not_distinct": {
"description": "Nulls not distinct constraint",
"type": "boolean"
"type": "boolean",
"default": false
},
"index_parameters": {
"type": "object",
"additionalProperties": false,
"properties": {
"tablespace": {
"type": "string"
"type": "string",
"default": ""
},
"storage_parameters": {
"type": "string"
"type": "string",
"default": ""
},
"include_columns": {
"type": "array",
Expand Down