Skip to content

Commit 4fc0bff

Browse files
Fix column renames for indexes (#147)
1 parent 7b45b90 commit 4fc0bff

File tree

5 files changed

+56
-67
lines changed

5 files changed

+56
-67
lines changed

internal/queries/queries.sql

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ SELECT
9595
i.indisunique AS index_is_unique,
9696
COALESCE(parent_c.relname, '')::TEXT AS parent_index_name,
9797
COALESCE(parent_namespace.nspname, '')::TEXT AS parent_index_schema_name,
98+
(
99+
SELECT ARRAY_AGG(att.attname ORDER BY indkey_ord.ord)
100+
FROM UNNEST(i.indkey) WITH ORDINALITY AS indkey_ord (attnum, ord)
101+
INNER JOIN
102+
pg_catalog.pg_attribute AS att
103+
ON att.attrelid = table_c.oid AND att.attnum = indkey_ord.attnum
104+
)::TEXT [] AS column_names,
98105
COALESCE(con.conislocal, false) AS constraint_is_local
99106
FROM pg_catalog.pg_class AS c
100107
INNER JOIN pg_catalog.pg_index AS i ON (i.indexrelid = c.oid)
@@ -119,14 +126,6 @@ WHERE
119126
AND table_namespace.nspname !~ '^pg_temp'
120127
AND (c.relkind = 'i' OR c.relkind = 'I');
121128

122-
-- name: GetColumnsForIndex :many
123-
SELECT a.attname::TEXT AS column_name
124-
FROM pg_catalog.pg_attribute AS a
125-
WHERE
126-
a.attrelid = $1
127-
AND a.attnum > 0
128-
ORDER BY a.attnum;
129-
130129
-- name: GetCheckConstraints :many
131130
SELECT
132131
pg_constraint.oid,

internal/queries/queries.sql.go

Lines changed: 9 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/schema/schema.go

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -931,31 +931,16 @@ func (s *schemaFetcher) buildCheckConstraint(ctx context.Context, cc queries.Get
931931
}, nil
932932
}
933933

934-
// fetchIndexes fetches the indexes We fetch all indexes at once to minimize number of queries, since each index needs
935-
// to fetch columns
934+
// fetchIndexes fetches the indexes. We fetch all the indexes at once to minimize the number of queries.
936935
func (s *schemaFetcher) fetchIndexes(ctx context.Context) ([]Index, error) {
937936
rawIndexes, err := s.q.GetIndexes(ctx)
938937
if err != nil {
939938
return nil, fmt.Errorf("GetIndexes: %w", err)
940939
}
941940

942-
goroutineRunner := s.goroutineRunnerFactory()
943-
var idxFutures []concurrent.Future[Index]
944-
for _, _rawIndex := range rawIndexes {
945-
rawIndex := _rawIndex // Capture loop variable for go routine
946-
f, err := concurrent.SubmitFuture(ctx, goroutineRunner, func() (Index, error) {
947-
return s.buildIndex(ctx, rawIndex)
948-
})
949-
if err != nil {
950-
return nil, fmt.Errorf("starting index future: %w", err)
951-
}
952-
953-
idxFutures = append(idxFutures, f)
954-
}
955-
956-
idxs, err := concurrent.GetAll(ctx, idxFutures...)
957-
if err != nil {
958-
return nil, fmt.Errorf("getting indexes: %w", err)
941+
var idxs []Index
942+
for _, idx := range rawIndexes {
943+
idxs = append(idxs, s.buildIndex(idx))
959944
}
960945

961946
idxs = filterSliceByName(
@@ -969,12 +954,7 @@ func (s *schemaFetcher) fetchIndexes(ctx context.Context) ([]Index, error) {
969954
return idxs, nil
970955
}
971956

972-
func (s *schemaFetcher) buildIndex(ctx context.Context, rawIndex queries.GetIndexesRow) (Index, error) {
973-
rawColumns, err := s.q.GetColumnsForIndex(ctx, rawIndex.Oid)
974-
if err != nil {
975-
return Index{}, fmt.Errorf("GetColumnsForIndex(%s): %w", rawIndex.Oid, err)
976-
}
977-
957+
func (s *schemaFetcher) buildIndex(rawIndex queries.GetIndexesRow) Index {
978958
var indexConstraint *IndexConstraint
979959
if rawIndex.ConstraintName != "" {
980960
indexConstraint = &IndexConstraint{
@@ -999,15 +979,15 @@ func (s *schemaFetcher) buildIndex(ctx context.Context, rawIndex queries.GetInde
999979
EscapedName: EscapeIdentifier(rawIndex.TableName),
1000980
},
1001981
Name: rawIndex.IndexName,
1002-
Columns: rawColumns,
982+
Columns: rawIndex.ColumnNames,
1003983
GetIndexDefStmt: GetIndexDefStatement(rawIndex.DefStmt),
1004984
IsInvalid: !rawIndex.IndexIsValid,
1005985
IsUnique: rawIndex.IndexIsUnique,
1006986

1007987
Constraint: indexConstraint,
1008988

1009989
ParentIdx: parentIdx,
1010-
}, nil
990+
}
1011991
}
1012992

1013993
func (s *schemaFetcher) fetchForeignKeyCons(ctx context.Context) ([]ForeignKeyConstraint, error) {

internal/schema/schema_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,38 @@ var (
892892
},
893893
},
894894
},
895+
{
896+
name: "Column rename",
897+
ddl: []string{`
898+
CREATE TABLE foo (
899+
value TEXT
900+
);
901+
CREATE INDEX foo_value_idx ON foo (value);
902+
ALTER TABLE foo RENAME COLUMN value TO renamed_value;
903+
`},
904+
expectedSchema: Schema{
905+
NamedSchemas: []NamedSchema{
906+
{Name: "public"},
907+
},
908+
Tables: []Table{
909+
{
910+
SchemaQualifiedName: SchemaQualifiedName{SchemaName: "public", EscapedName: "\"foo\""},
911+
Columns: []Column{
912+
{Name: "renamed_value", Type: "text", IsNullable: true, Size: -1, Collation: defaultCollation},
913+
},
914+
CheckConstraints: nil,
915+
ReplicaIdentity: ReplicaIdentityDefault,
916+
},
917+
},
918+
Indexes: []Index{
919+
{
920+
OwningTable: SchemaQualifiedName{SchemaName: "public", EscapedName: "\"foo\""},
921+
Name: "foo_value_idx", Columns: []string{"renamed_value"},
922+
GetIndexDefStmt: "CREATE INDEX foo_value_idx ON public.foo USING btree (renamed_value)",
923+
},
924+
},
925+
},
926+
},
895927
{
896928
name: "Filtering - filtering out the base table",
897929
opts: []GetSchemaOpt{WithIncludeSchemas("public")},

pkg/diff/sql_generator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,6 +1718,7 @@ func (isg *indexSQLVertexGenerator) addDepsOnTableAddAlterIfNecessary(index sche
17181718
}
17191719
}
17201720

1721+
// Otherwise, we can drop the index whenever we want.
17211722
return nil
17221723
}
17231724

0 commit comments

Comments
 (0)