-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert DROP INDEX SQL to pgroll operation (#524)
Converts SQL in the following forms to the equivalent pgroll operation: ```sql DROP INDEX foo DROP INDEX schema.foo DROP INDEX foo RESTRICT DROP INDEX CONCURRENTLY foo DROP INDEX IF EXISTS foo ``` The following forms are left as raw SQL operations since we do not support them in pgroll yet: ```sql DROP INDEX foo CASCADE ``` Part of #504
- Loading branch information
Showing
4 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package sql2pgroll | ||
|
||
import ( | ||
"strings" | ||
|
||
pgq "github.com/pganalyze/pg_query_go/v6" | ||
|
||
"github.com/xataio/pgroll/pkg/migrations" | ||
) | ||
|
||
// convertDropStatement converts supported drop statements to pgroll operations | ||
func convertDropStatement(stmt *pgq.DropStmt) (migrations.Operations, error) { | ||
if stmt.RemoveType == pgq.ObjectType_OBJECT_INDEX { | ||
return convertDropIndexStatement(stmt) | ||
} | ||
return nil, nil | ||
} | ||
|
||
// convertDropIndexStatement converts simple DROP INDEX statements to pgroll operations | ||
func convertDropIndexStatement(stmt *pgq.DropStmt) (migrations.Operations, error) { | ||
if !canConvertDropIndex(stmt) { | ||
return nil, nil | ||
} | ||
items := stmt.GetObjects()[0].GetList().GetItems() | ||
parts := make([]string, len(items)) | ||
for i, item := range items { | ||
parts[i] = item.GetString_().GetSval() | ||
} | ||
|
||
return migrations.Operations{ | ||
&migrations.OpDropIndex{ | ||
Name: strings.Join(parts, "."), | ||
}, | ||
}, nil | ||
} | ||
|
||
// canConvertDropIndex checks whether we can convert the statement without losing any information. | ||
func canConvertDropIndex(stmt *pgq.DropStmt) bool { | ||
if len(stmt.Objects) > 1 { | ||
return false | ||
} | ||
if stmt.Behavior == pgq.DropBehavior_DROP_CASCADE { | ||
return false | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package sql2pgroll_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"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" | ||
) | ||
|
||
func TestDropIndexStatements(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
sql string | ||
expectedOp migrations.Operation | ||
}{ | ||
{ | ||
sql: "DROP INDEX foo", | ||
expectedOp: expect.DropIndexOp1, | ||
}, | ||
{ | ||
sql: "DROP INDEX myschema.foo", | ||
expectedOp: expect.DropIndexOp2, | ||
}, | ||
{ | ||
sql: "DROP INDEX foo RESTRICT", | ||
expectedOp: expect.DropIndexOp1, | ||
}, | ||
{ | ||
sql: "DROP INDEX IF EXISTS foo", | ||
expectedOp: expect.DropIndexOp1, | ||
}, | ||
{ | ||
sql: "DROP INDEX CONCURRENTLY foo", | ||
expectedOp: expect.DropIndexOp1, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.sql, func(t *testing.T) { | ||
ops, err := sql2pgroll.Convert(tc.sql) | ||
require.NoError(t, err) | ||
|
||
require.Len(t, ops, 1) | ||
|
||
assert.Equal(t, tc.expectedOp, ops[0]) | ||
}) | ||
} | ||
} | ||
|
||
func TestUnconvertableDropIndexStatements(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []string{ | ||
"DROP INDEX foo CASCADE", | ||
} | ||
|
||
for _, sql := range tests { | ||
t.Run(sql, func(t *testing.T) { | ||
ops, err := sql2pgroll.Convert(sql) | ||
require.NoError(t, err) | ||
|
||
require.Len(t, ops, 1) | ||
|
||
assert.Equal(t, expect.RawSQLOp(sql), ops[0]) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package expect | ||
|
||
import ( | ||
"github.com/xataio/pgroll/pkg/migrations" | ||
) | ||
|
||
var DropIndexOp1 = &migrations.OpDropIndex{ | ||
Name: "foo", | ||
} | ||
|
||
var DropIndexOp2 = &migrations.OpDropIndex{ | ||
Name: "myschema.foo", | ||
} |