-
Notifications
You must be signed in to change notification settings - Fork 312
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_regression_on_modify_column #648
base: master
Are you sure you want to change the base?
Conversation
defp extract_opts(opts) do | ||
with {:ok, from} <- Keyword.fetch(opts, :from), | ||
{_type, from_opts} <- from do | ||
from_opts | ||
else | ||
_ -> opts | ||
end | ||
end |
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.
defp extract_opts(opts) do | |
with {:ok, from} <- Keyword.fetch(opts, :from), | |
{_type, from_opts} <- from do | |
from_opts | |
else | |
_ -> opts | |
end | |
end | |
defp extract_from_opts({_type, opts}), do: opts | |
defp extract_from_opts(_), do: [] |
I think we might need to do something like this. Otherwise if someone supplies something like this it will think they are equal:
modify(:field, :string, size: 100, from: :string)
Thank you for the PR. I'm wondering if we have an issue here too? ecto_sql/lib/ecto/adapters/postgres/connection.ex Line 1561 in 3738456
|
Here as well: ecto_sql/lib/ecto/adapters/postgres/connection.ex Line 1550 in 3738456
if we are allowing |
Yeah, I am afraid this is a bit too tricky to the point a separate helper may be better after all. :( |
Thanks @greg-rychlewski for the review. The 2 places you marked are in the same function. I think it should be good there now
I'm not sure if this one will not crash, because the reference column type is handled differently, when wrapped in tuple then instead of returning type it will return the reference struct.
Feel free to update the pr. We should test the type system on this piece of code :D |
Sorry what I meant was defp column_change(table, {:modify, name, %Reference{} = ref, opts}) do
reference_column_type = reference_column_type(ref.type, opts)
from_column_type = extract_column_type(opts[:from])
from_opts = extract_opts(opts[:from])
If 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 But I also agree with Jose this is kind of getting hairy. It's also making me wonder if it people will use it wrong without realizing |
But in this case extract_column_type will also return nil because no other pattern matches, the first one will not match because defp extract_column_type_and_opts({type, opts}) when is_atom(type), do: {type, opts}
defp extract_column_type_and_opts({%Reference{type: type}, opts}), do: {type, opts}
defp extract_column_type_and_opts(type) when is_atom(type), do: {type, []}
defp extract_column_type_and_opts(%Reference{type: type}), do: {type, []}
defp extract_column_type_and_opts(_), do: {nil, []} |
That's a good point. I think you uncovered another potential issue. So then the question is do we want to continue trying to make it work this way or would it be simpler to take the functionality out of |
I did not analyzed the code much, it's just the minimal change that fixes the #647 and it's compatible with existing code.