-
Notifications
You must be signed in to change notification settings - Fork 26
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
FROM UNNEST(i.indkey) WITH ORDINALITY AS indkey_ord (attnum, ord) | ||
Comment on lines
+99
to
+100
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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) | ||
|
@@ -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, | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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")}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1718,6 +1718,7 @@ func (isg *indexSQLVertexGenerator) addDepsOnTableAddAlterIfNecessary(index sche | |
} | ||
} | ||
|
||
// Otherwise, we can drop the index whenever we want. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
|
||
|
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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!