-
-
Notifications
You must be signed in to change notification settings - Fork 138
Fix/236 unique index before self fk #705
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
Changes from 9 commits
89c9a6f
13ac2e9
6635753
76e3ca1
f5adf76
6f29957
c00ef17
7765d59
21d11b2
48c2be5
ec79880
a45fbcf
1f82383
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 |
|---|---|---|
|
|
@@ -1518,6 +1518,76 @@ defmodule AshPostgres.MigrationGenerator do | |
| true | ||
| end | ||
|
|
||
| # CreateTable must appear before AddUniqueIndex for the same table (table must exist first). | ||
| defp after?( | ||
| %Operation.CreateTable{table: table, schema: schema}, | ||
| %Operation.AddUniqueIndex{table: table, schema: schema} | ||
| ), | ||
| do: false | ||
|
|
||
| # Unique index must be created before any alter that adds FKs referencing it (issue #236). | ||
| defp after?( | ||
| %Operation.AddUniqueIndex{table: table, schema: schema}, | ||
| %Operation.AlterAttribute{table: table, schema: schema} | ||
| ), | ||
| do: false | ||
|
|
||
| # Do not place AddUniqueIndex after CreateTable (must be after columns are added). | ||
| defp after?( | ||
| %Operation.AddUniqueIndex{table: table, schema: schema}, | ||
| %Operation.CreateTable{table: table, schema: schema} | ||
| ), | ||
| do: false | ||
|
|
||
| # Place AddUniqueIndex after the last AddAttribute for the same table so it | ||
| # appears before AlterAttributes (issue #236). | ||
| defp after?( | ||
| %Operation.AddUniqueIndex{ | ||
| insert_after_attribute_order: max_order, | ||
| table: table, | ||
| schema: schema | ||
| }, | ||
| %Operation.AddAttribute{ | ||
| table: table, | ||
| schema: schema, | ||
| attribute: %{order: order} | ||
| } | ||
| ) | ||
| when not is_nil(max_order) and order == max_order, | ||
| do: true | ||
|
|
||
| defp after?( | ||
| %Operation.AddUniqueIndex{ | ||
| insert_after_attribute_order: max_order, | ||
| table: table, | ||
| schema: schema | ||
| }, | ||
| %Operation.AddAttribute{ | ||
| table: table, | ||
| schema: schema, | ||
| attribute: %{order: order} | ||
| } | ||
| ) | ||
| when not is_nil(max_order) and order != max_order, | ||
| do: false | ||
|
|
||
| defp after?( | ||
| %Operation.AddUniqueIndex{ | ||
| identity: %{keys: keys}, | ||
| table: table, | ||
| schema: schema | ||
| }, | ||
| %Operation.AlterAttribute{ | ||
| table: table, | ||
| schema: schema, | ||
| new_attribute: %{ | ||
| references: %{table: table, destination_attribute: destination_attribute} | ||
| } | ||
| } | ||
| ) do | ||
| destination_attribute not in List.wrap(keys) | ||
| end | ||
|
|
||
| defp after?( | ||
| %Operation.AddUniqueIndex{ | ||
| table: table, | ||
|
|
@@ -2267,6 +2337,7 @@ defmodule AshPostgres.MigrationGenerator do | |
| identity: identity, | ||
| schema: snapshot.schema, | ||
| table: snapshot.table, | ||
| insert_after_attribute_order: max(0, length(snapshot.attributes) - 1), | ||
|
Contributor
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. Doesn't this just end up placing them at the end if its using the max order?
Contributor
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. If its being used to sort after a given attribute then it would make sense to track the name instead I think.
Contributor
Author
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. You’re right, the original code effectively used the last attribute order. I’ll update it so |
||
| concurrently: opts.concurrent_indexes | ||
| } | ||
| end) | ||
|
|
@@ -2301,13 +2372,22 @@ defmodule AshPostgres.MigrationGenerator do | |
| } | ||
| end) | ||
|
|
||
| # Place unique indexes after create/add attributes but before alter attributes | ||
| # so FKs that reference identity columns see the unique index (issue #236). | ||
| {creates_and_adds, alter_and_rest} = | ||
| Enum.split_while(attribute_operations, fn | ||
| %Operation.AlterAttribute{} -> false | ||
| _ -> true | ||
| end) | ||
|
|
||
| [ | ||
| pkey_operations, | ||
| unique_indexes_to_remove, | ||
| attribute_operations, | ||
| creates_and_adds, | ||
| unique_indexes_to_add, | ||
| alter_and_rest, | ||
| reference_indexes_to_add, | ||
| reference_indexes_to_remove, | ||
| unique_indexes_to_add, | ||
| unique_indexes_to_rename, | ||
| constraints_to_remove, | ||
| constraints_to_add, | ||
|
|
@@ -2786,7 +2866,6 @@ defmodule AshPostgres.MigrationGenerator do | |
|
|
||
| is_nil(old_refs) or is_nil(new_refs) -> | ||
| true | ||
|
|
||
| true -> | ||
| old_without_index = Map.delete(old_refs, :index?) | ||
| new_without_index = Map.delete(new_refs, :index?) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.