Skip to content

Commit

Permalink
More unconvertable cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanslade authored and andrew-farries committed Dec 20, 2024
1 parent e0df622 commit 2673a27
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
31 changes: 30 additions & 1 deletion pkg/sql2pgroll/create_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (

// convertCreateIndexStmt converts CREATE INDEX statements into pgroll operations.
func convertCreateIndexStmt(stmt *pgq.IndexStmt) (migrations.Operations, error) {
if !canConvertCreateIndexStmt(stmt) {
return nil, nil
}

tableName := getQualifiedRelationName(stmt.GetRelation())
var columns []string

Expand Down Expand Up @@ -42,7 +46,6 @@ func convertCreateIndexStmt(stmt *pgq.IndexStmt) (migrations.Operations, error)
predicate = &deparsed
}

// TODO: We don't support more than one storage param
var storageParams *string
for _, option := range stmt.GetOptions() {
// TODO: It may be easier to deparse this in pgq, but for now it is not supported
Expand Down Expand Up @@ -82,3 +85,29 @@ func convertCreateIndexStmt(stmt *pgq.IndexStmt) (migrations.Operations, error)
},
}, nil
}

func canConvertCreateIndexStmt(stmt *pgq.IndexStmt) bool {
if len(stmt.GetOptions()) > 1 {
return false
}
for _, param := range stmt.GetIndexParams() {
if param.GetIndexElem().GetCollation() != nil {
return false
}
ordering := param.GetIndexElem().GetOrdering()
if ordering != pgq.SortByDir_SORTBY_DEFAULT && ordering != pgq.SortByDir_SORTBY_ASC {
return false
}
if param.GetIndexElem().GetNullsOrdering() != pgq.SortByNulls_SORTBY_NULLS_DEFAULT {
return false
}
}
if stmt.GetTableSpace() != "" {
return false
}
if stmt.GetIndexIncludingParams() != nil {
return false
}

return true
}
33 changes: 33 additions & 0 deletions pkg/sql2pgroll/create_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ func TestConvertCreateIndexStatements(t *testing.T) {
sql: "CREATE INDEX idx_name ON foo (bar)",
expectedOp: expect.CreateIndexOp1,
},
{
sql: "CREATE INDEX idx_name ON foo (bar ASC)",
expectedOp: expect.CreateIndexOp1,
},
{
sql: "CREATE INDEX idx_name ON foo (bar NULLS)",
expectedOp: expect.CreateIndexOp1,
},
{
sql: "CREATE INDEX idx_name ON foo USING btree (bar)",
expectedOp: expect.CreateIndexOp1,
Expand Down Expand Up @@ -117,3 +125,28 @@ func TestConvertCreateIndexStatements(t *testing.T) {
})
}
}

func TestUnconvertableCreateIndexStatements(t *testing.T) {
t.Parallel()

tests := []string{
"CREATE INDEX idx_name ON foo (bar) WITH (fillfactor = 70, deduplicate_items = true)",
"CREATE INDEX idx_name ON foo (bar) TABLESPACE baz",
"CREATE INDEX idx_name ON foo (bar COLLATE en_US)",
"CREATE INDEX idx_name ON foo (bar DESC)",
"CREATE INDEX idx_name ON foo (bar NULLS FIRST)",
"CREATE INDEX idx_name ON foo (bar NULLS LAST)",
"CREATE INDEX idx_name ON foo (bar) INCLUDE (baz)",
}

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])
})
}
}

0 comments on commit 2673a27

Please sign in to comment.