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

Convert DROP COLUMN SQL to pgroll operation #521

Merged
merged 7 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pkg/sql2pgroll/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

pgq "github.com/pganalyze/pg_query_go/v6"

"github.com/xataio/pgroll/pkg/migrations"
)

Expand Down Expand Up @@ -35,6 +36,8 @@ func convertAlterTableStmt(stmt *pgq.AlterTableStmt) (migrations.Operations, err
op, err = convertAlterTableAlterColumnType(stmt, alterTableCmd)
case pgq.AlterTableType_AT_AddConstraint:
op, err = convertAlterTableAddConstraint(stmt, alterTableCmd)
case pgq.AlterTableType_AT_DropColumn:
op, err = convertAlterTableDropColumn(stmt, alterTableCmd)
}

if err != nil {
Expand Down Expand Up @@ -155,6 +158,19 @@ func convertAlterTableAddUniqueConstraint(stmt *pgq.AlterTableStmt, constraint *
}, nil
}

// convertAlterTableDropColumn converts SQL statements like:
//
// `ALTER TABLE foo DROP COLUMN bar
//
// to an OpDropColumn operation.
func convertAlterTableDropColumn(stmt *pgq.AlterTableStmt, cmd *pgq.AlterTableCmd) (migrations.Operation, error) {
return &migrations.OpDropColumn{
Table: stmt.GetRelation().GetRelname(),
Column: cmd.GetName(),
Down: PlaceHolderSQL,
}, nil
}

// canConvertUniqueConstraint checks if the unique constraint `constraint` can
// be faithfully converted to an OpCreateConstraint operation without losing
// information.
Expand Down
5 changes: 5 additions & 0 deletions pkg/sql2pgroll/alter_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/xataio/pgroll/pkg/migrations"
"github.com/xataio/pgroll/pkg/sql2pgroll"
"github.com/xataio/pgroll/pkg/sql2pgroll/expect"
Expand Down Expand Up @@ -43,6 +44,10 @@ func TestConvertAlterTableStatements(t *testing.T) {
sql: "ALTER TABLE foo ADD CONSTRAINT bar UNIQUE (a, b)",
expectedOp: expect.CreateConstraintOp2,
},
{
sql: "ALTER TABLE foo DROP COLUMN bar",
expectedOp: expect.DropColumnOp1,
},
}

for _, tc := range tests {
Expand Down
1 change: 1 addition & 0 deletions pkg/sql2pgroll/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

pgq "github.com/pganalyze/pg_query_go/v6"

andrew-farries marked this conversation as resolved.
Show resolved Hide resolved
"github.com/xataio/pgroll/pkg/migrations"
)

Expand Down
1 change: 1 addition & 0 deletions pkg/sql2pgroll/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package sql2pgroll

import (
pgq "github.com/pganalyze/pg_query_go/v6"

"github.com/xataio/pgroll/pkg/migrations"
)

Expand Down
1 change: 1 addition & 0 deletions pkg/sql2pgroll/create_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/xataio/pgroll/pkg/migrations"
"github.com/xataio/pgroll/pkg/sql2pgroll"
"github.com/xataio/pgroll/pkg/sql2pgroll/expect"
Expand Down
12 changes: 12 additions & 0 deletions pkg/sql2pgroll/expect/drop_column.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package expect

import (
"github.com/xataio/pgroll/pkg/migrations"
"github.com/xataio/pgroll/pkg/sql2pgroll"
)

var DropColumnOp1 = &migrations.OpDropColumn{
Table: "foo",
Column: "bar",
Down: sql2pgroll.PlaceHolderSQL,
}
1 change: 1 addition & 0 deletions pkg/sql2pgroll/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package sql2pgroll

import (
pgq "github.com/pganalyze/pg_query_go/v6"

"github.com/xataio/pgroll/pkg/migrations"
)

Expand Down
1 change: 1 addition & 0 deletions pkg/sql2pgroll/rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/xataio/pgroll/pkg/migrations"
"github.com/xataio/pgroll/pkg/sql2pgroll"
"github.com/xataio/pgroll/pkg/sql2pgroll/expect"
Expand Down
Loading