-
Notifications
You must be signed in to change notification settings - Fork 329
Do not modify column type while modifying fk constraint #644
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
Merged
greg-rychlewski
merged 13 commits into
elixir-ecto:master
from
milmazz:bugfix/migration/do-not-modify-field-type
Oct 23, 2024
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
70a375f
Do not modify column type while modifying fk constraint
milmazz 475cf8c
avoid alter column type on modify if not needed
milmazz fc2e173
should be smarter now?
milmazz daeed39
fix syntax error bug when setting default value
milmazz 79e6479
minor refactor
milmazz 609d4c1
Update docs and add a warning block for this function
milmazz 6b90290
minor doc update
milmazz fb1655e
Apply suggestions from code review
milmazz c18c93f
add unit test for postgresql adapter
milmazz 38e9365
revert unit tests modifications in sql/migration
milmazz ff60dd3
add unit tests to pg adapter
milmazz d951e49
update docs, minor unit test change
milmazz ce83d5a
update unit test
milmazz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1546,35 +1546,76 @@ if Code.ensure_loaded?(Postgrex) do | |
end | ||
|
||
defp column_change(table, {:modify, name, %Reference{} = ref, opts}) do | ||
[ | ||
drop_reference_expr(opts[:from], table, name), | ||
"ALTER COLUMN ", | ||
quote_name(name), | ||
" TYPE ", | ||
reference_column_type(ref.type, opts), | ||
", ADD ", | ||
reference_column_type = reference_column_type(ref.type, opts) | ||
from_column_type = extract_column_type(opts[:from]) | ||
|
||
drop_reference_expr = drop_reference_expr(opts[:from], table, name) | ||
prefix_with_comma = (drop_reference_expr != [] && ", ") || "" | ||
|
||
common_suffix = [ | ||
reference_expr(ref, table, name), | ||
modify_null(name, opts), | ||
modify_default(name, ref.type, opts) | ||
] | ||
|
||
if reference_column_type == reference_column_type(from_column_type, opts) do | ||
[ | ||
drop_reference_expr, | ||
prefix_with_comma, | ||
"ADD " | common_suffix | ||
] | ||
else | ||
[ | ||
drop_reference_expr, | ||
prefix_with_comma, | ||
"ALTER COLUMN ", | ||
quote_name(name), | ||
" TYPE ", | ||
reference_column_type, | ||
", ADD " | common_suffix | ||
] | ||
end | ||
end | ||
|
||
defp column_change(table, {:modify, name, type, opts}) do | ||
[ | ||
drop_reference_expr(opts[:from], table, name), | ||
"ALTER COLUMN ", | ||
quote_name(name), | ||
" TYPE ", | ||
column_type(type, opts), | ||
modify_null(name, opts), | ||
modify_default(name, type, opts) | ||
] | ||
column_type = column_type(type, opts) | ||
from_column_type = extract_column_type(opts[:from]) | ||
|
||
drop_reference_expr = drop_reference_expr(opts[:from], table, name) | ||
any_drop_ref? = drop_reference_expr != [] | ||
|
||
if column_type == column_type(from_column_type, opts) do | ||
modify_null = modify_null(name, Keyword.put(opts, :prefix_with_comma, any_drop_ref?)) | ||
any_modify_null? = modify_null != [] | ||
|
||
modify_default = | ||
modify_default( | ||
name, | ||
type, | ||
Keyword.put(opts, :prefix_with_comma, any_drop_ref? or any_modify_null?) | ||
) | ||
|
||
[drop_reference_expr, modify_null, modify_default] | ||
else | ||
[ | ||
drop_reference_expr, | ||
(any_drop_ref? && ", ") || "", | ||
"ALTER COLUMN ", | ||
quote_name(name), | ||
" TYPE ", | ||
column_type, | ||
Comment on lines
+1603
to
+1606
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. Similar behavior is observed when doing something like: modify :account_id, :binary_id, null: false, from: {:binary_id, null: true} The SQL syntax produced is this one: ALTER TABLE "my_table"
ALTER COLUMN "account_id" TYPE uuid,
ALTER COLUMN "account_id" SET NOT NULL; And that's producing way more |
||
modify_null(name, opts), | ||
modify_default(name, type, opts) | ||
] | ||
end | ||
end | ||
|
||
defp column_change(_table, {:remove, name}), do: ["DROP COLUMN ", quote_name(name)] | ||
|
||
defp column_change(table, {:remove, name, %Reference{} = ref, _opts}) do | ||
[drop_reference_expr(ref, table, name), "DROP COLUMN ", quote_name(name)] | ||
drop_reference_expr = drop_reference_expr(ref, table, name) | ||
prefix_with_comma = (drop_reference_expr != [] && ", ") || "" | ||
[drop_reference_expr, prefix_with_comma, "DROP COLUMN ", quote_name(name)] | ||
end | ||
|
||
defp column_change(_table, {:remove, name, _type, _opts}), | ||
|
@@ -1595,17 +1636,23 @@ if Code.ensure_loaded?(Postgrex) do | |
do: ["DROP COLUMN IF EXISTS ", quote_name(name)] | ||
|
||
defp modify_null(name, opts) do | ||
prefix_with_comma = Keyword.get(opts, :prefix_with_comma, true) | ||
prefix = if prefix_with_comma, do: ", ", else: "" | ||
|
||
case Keyword.get(opts, :null) do | ||
true -> [", ALTER COLUMN ", quote_name(name), " DROP NOT NULL"] | ||
false -> [", ALTER COLUMN ", quote_name(name), " SET NOT NULL"] | ||
true -> [prefix, "ALTER COLUMN ", quote_name(name), " DROP NOT NULL"] | ||
false -> [prefix, "ALTER COLUMN ", quote_name(name), " SET NOT NULL"] | ||
nil -> [] | ||
end | ||
end | ||
|
||
defp modify_default(name, type, opts) do | ||
prefix_with_comma = Keyword.get(opts, :prefix_with_comma, true) | ||
prefix = if prefix_with_comma, do: ", ", else: "" | ||
|
||
case Keyword.fetch(opts, :default) do | ||
{:ok, val} -> | ||
[", ALTER COLUMN ", quote_name(name), " SET", default_expr({:ok, val}, type)] | ||
[prefix, "ALTER COLUMN ", quote_name(name), " SET", default_expr({:ok, val}, type)] | ||
|
||
:error -> | ||
[] | ||
|
@@ -1797,6 +1844,11 @@ if Code.ensure_loaded?(Postgrex) do | |
[type, generated_expr(generated)] | ||
end | ||
|
||
defp extract_column_type({type, _}) when is_atom(type), do: type | ||
defp extract_column_type(type) when is_atom(type), do: type | ||
defp extract_column_type(%Reference{type: type}), do: type | ||
defp extract_column_type(_), do: nil | ||
|
||
defp generated_expr(nil), do: [] | ||
|
||
defp generated_expr(expr) when is_binary(expr) do | ||
|
@@ -1833,7 +1885,7 @@ if Code.ensure_loaded?(Postgrex) do | |
do: drop_reference_expr(ref, table, name) | ||
|
||
defp drop_reference_expr(%Reference{} = ref, table, name), | ||
do: ["DROP CONSTRAINT ", reference_name(ref, table, name), ", "] | ||
do: ["DROP CONSTRAINT ", reference_name(ref, table, name)] | ||
|
||
defp drop_reference_expr(_, _, _), | ||
do: [] | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.