-
Notifications
You must be signed in to change notification settings - Fork 11
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
[feature] Added constraint support in migrations #108
Merged
ruslandoga
merged 5 commits into
plausible:master
from
senconscious:feature/add_constraint_support
Aug 28, 2023
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f3d2e34
Added check constraint support for create
senconscious 183750d
Added drop and drop_if_exists support for constraint
senconscious 31c011b
Added check constraint support for create_if_not_exists
senconscious f80da55
Moved constraint checks into one function
senconscious 43c40a9
Added check option validation for constraints
senconscious 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 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 |
---|---|---|
|
@@ -108,14 +108,37 @@ defmodule Ecto.Adapters.ClickHouse.Migration do | |
[[drop | @conn.quote_table(index.prefix, index.name)]] | ||
end | ||
|
||
def execute_ddl({command, %Constraint{} = _constraint}) | ||
def execute_ddl({command, %Constraint{} = constraint}) | ||
when command in [:create, :create_if_not_exists] do | ||
raise "TODO" | ||
table_name = @conn.quote_table(constraint.prefix, constraint.table) | ||
|
||
add = | ||
case command do | ||
:create -> " ADD CONSTRAINT " | ||
:create_if_not_exists -> " ADD CONSTRAINT IF NOT EXISTS " | ||
end | ||
|
||
constraint_expr = constraint |> constraint_expr() |> Enum.join("") | ||
|
||
[["ALTER TABLE ", table_name, add, constraint_expr]] | ||
end | ||
|
||
def execute_ddl({command, %Constraint{} = _constraint, _mode}) | ||
def execute_ddl({command, %Constraint{} = constraint, _mode}) | ||
when command in [:drop, :drop_if_exists] do | ||
raise "TODO" | ||
drop = | ||
case command do | ||
:drop -> " DROP CONSTRAINT " | ||
:drop_if_exists -> " DROP CONSTRAINT IF EXISTS " | ||
end | ||
|
||
[ | ||
[ | ||
"ALTER TABLE ", | ||
@conn.quote_table(constraint.prefix, constraint.table), | ||
drop, | ||
@conn.quote_name(constraint.name) | ||
] | ||
] | ||
end | ||
|
||
def execute_ddl({:rename, %Table{} = current_table, %Table{} = new_table}) do | ||
|
@@ -305,6 +328,29 @@ defmodule Ecto.Adapters.ClickHouse.Migration do | |
defp index_expr(literal) when is_binary(literal), do: literal | ||
defp index_expr(literal), do: @conn.quote_name(literal) | ||
|
||
defp constraint_expr(%Constraint{check: check, validate: true, comment: nil} = constraint) | ||
when is_binary(check) do | ||
[ | ||
@conn.quote_name(constraint.name), | ||
" CHECK (", | ||
check, | ||
")" | ||
] | ||
end | ||
|
||
defp constraint_expr(%Constraint{check: check, validate: true, comment: comment}) | ||
when is_binary(check) and is_binary(comment) do | ||
raise "Clickhouse adapter does not support comments on check constraints" | ||
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. I'd probably put all these checks into a single function clause like this defp constraint_expr(%Constraint{} = constraint) do
if constraint.comment do
raise "Clickhouse adapter does not support comments on check constraints"
end
unless constraint.validate do
raise ...
end
# etc. checks for other unsupported fields / options
[@conn.quote_name(constraint.name), " CHECK (", constraint.check, ?)]
end 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. In that case maybe it's more appropriate to move them inside execute_ddl/1 |
||
end | ||
|
||
defp constraint_expr(%Constraint{check: check, validate: false}) when is_binary(check) do | ||
raise "Clickhouse adapter does not support check constraints without validation on creation" | ||
end | ||
|
||
defp constraint_expr(%Constraint{exclude: exclude}) when is_binary(exclude) do | ||
raise "Clickhouse adapter does not support exclude constraints" | ||
end | ||
|
||
defp options_expr(nil), do: [] | ||
|
||
defp options_expr(options) when is_list(options) do | ||
|
This file contains 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
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.
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.
We probably don't need to
Enum.join
since we are returning an iolist anyway.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.
Removed Enum.join as removed constraint_expr/1