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

Fix column renames for indexes #147

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 7 additions & 8 deletions internal/queries/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ SELECT
i.indisunique AS index_is_unique,
COALESCE(parent_c.relname, '')::TEXT AS parent_index_name,
COALESCE(parent_namespace.nspname, '')::TEXT AS parent_index_schema_name,
(
SELECT ARRAY_AGG(att.attname ORDER BY indkey_ord.ord)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I almost wonder if it's worth a comment on this block here that this is just i.indkey.map(attnum -> column_name(attnum)) but in sql.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might leave out the comment, since it's probably best to read from the docs on it. Pretty nifty SQL function!

FROM UNNEST(i.indkey) WITH ORDINALITY AS indkey_ord (attnum, ord)
Comment on lines +99 to +100
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ordinality here is just to make it explicit that the unnested array should be aggregated back in the same order after converting to column names right? And this isn't technically necessary since that should be the order anyways?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep exactly. It's to enforce the order. I don't think it's technically necessary, but I definitely feel more comfortable with explicit ORDER BY

INNER JOIN
pg_catalog.pg_attribute AS att
ON att.attrelid = table_c.oid AND att.attnum = indkey_ord.attnum
)::TEXT [] AS column_names,
COALESCE(con.conislocal, false) AS constraint_is_local
FROM pg_catalog.pg_class AS c
INNER JOIN pg_catalog.pg_index AS i ON (i.indexrelid = c.oid)
Expand All @@ -119,14 +126,6 @@ WHERE
AND table_namespace.nspname !~ '^pg_temp'
AND (c.relkind = 'i' OR c.relkind = 'I');

-- name: GetColumnsForIndex :many
SELECT a.attname::TEXT AS column_name
FROM pg_catalog.pg_attribute AS a
WHERE
a.attrelid = $1
AND a.attnum > 0
ORDER BY a.attnum;

-- name: GetCheckConstraints :many
SELECT
pg_constraint.oid,
Expand Down
41 changes: 9 additions & 32 deletions internal/queries/queries.sql.go

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

34 changes: 7 additions & 27 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -931,31 +931,16 @@ func (s *schemaFetcher) buildCheckConstraint(ctx context.Context, cc queries.Get
}, nil
}

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

goroutineRunner := s.goroutineRunnerFactory()
var idxFutures []concurrent.Future[Index]
for _, _rawIndex := range rawIndexes {
rawIndex := _rawIndex // Capture loop variable for go routine
f, err := concurrent.SubmitFuture(ctx, goroutineRunner, func() (Index, error) {
return s.buildIndex(ctx, rawIndex)
})
if err != nil {
return nil, fmt.Errorf("starting index future: %w", err)
}

idxFutures = append(idxFutures, f)
}

idxs, err := concurrent.GetAll(ctx, idxFutures...)
if err != nil {
return nil, fmt.Errorf("getting indexes: %w", err)
var idxs []Index
for _, idx := range rawIndexes {
idxs = append(idxs, s.buildIndex(idx))
}

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

func (s *schemaFetcher) buildIndex(ctx context.Context, rawIndex queries.GetIndexesRow) (Index, error) {
rawColumns, err := s.q.GetColumnsForIndex(ctx, rawIndex.Oid)
if err != nil {
return Index{}, fmt.Errorf("GetColumnsForIndex(%s): %w", rawIndex.Oid, err)
}

func (s *schemaFetcher) buildIndex(rawIndex queries.GetIndexesRow) Index {
var indexConstraint *IndexConstraint
if rawIndex.ConstraintName != "" {
indexConstraint = &IndexConstraint{
Expand All @@ -999,15 +979,15 @@ func (s *schemaFetcher) buildIndex(ctx context.Context, rawIndex queries.GetInde
EscapedName: EscapeIdentifier(rawIndex.TableName),
},
Name: rawIndex.IndexName,
Columns: rawColumns,
Columns: rawIndex.ColumnNames,
GetIndexDefStmt: GetIndexDefStatement(rawIndex.DefStmt),
IsInvalid: !rawIndex.IndexIsValid,
IsUnique: rawIndex.IndexIsUnique,

Constraint: indexConstraint,

ParentIdx: parentIdx,
}, nil
}
}

func (s *schemaFetcher) fetchForeignKeyCons(ctx context.Context) ([]ForeignKeyConstraint, error) {
Expand Down
32 changes: 32 additions & 0 deletions internal/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,38 @@ var (
},
},
},
{
name: "Column rename",
ddl: []string{`
CREATE TABLE foo (
value TEXT
);
CREATE INDEX foo_value_idx ON foo (value);
ALTER TABLE foo RENAME COLUMN value TO renamed_value;
`},
expectedSchema: Schema{
NamedSchemas: []NamedSchema{
{Name: "public"},
},
Tables: []Table{
{
SchemaQualifiedName: SchemaQualifiedName{SchemaName: "public", EscapedName: "\"foo\""},
Columns: []Column{
{Name: "renamed_value", Type: "text", IsNullable: true, Size: -1, Collation: defaultCollation},
},
CheckConstraints: nil,
ReplicaIdentity: ReplicaIdentityDefault,
},
},
Indexes: []Index{
{
OwningTable: SchemaQualifiedName{SchemaName: "public", EscapedName: "\"foo\""},
Name: "foo_value_idx", Columns: []string{"renamed_value"},
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, it would have fetched the column as "value" instead of "renamed_value"

GetIndexDefStmt: "CREATE INDEX foo_value_idx ON public.foo USING btree (renamed_value)",
},
},
},
},
{
name: "Filtering - filtering out the base table",
opts: []GetSchemaOpt{WithIncludeSchemas("public")},
Expand Down
1 change: 1 addition & 0 deletions pkg/diff/sql_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1718,6 +1718,7 @@ func (isg *indexSQLVertexGenerator) addDepsOnTableAddAlterIfNecessary(index sche
}
}

// Otherwise, we can drop the index whenever we want.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially was debugging this problem and thought adding a comment helped document it a little better

return nil
}

Expand Down
Loading