Skip to content
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

Conversation

senconscious
Copy link
Contributor

Hello, there was no support for constraint in create, create_if_not_exists, drop, drop_if_exists. Need to run those through execute. So I decided to contribute and add this small feature.

@senconscious senconscious marked this pull request as ready for review August 26, 2023 12:35
Copy link
Contributor

@ruslandoga ruslandoga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋 @senconscious

Thank you for the PR! I left some comments inline.

:create_if_not_exists -> " ADD CONSTRAINT IF NOT EXISTS "
end

constraint_expr = constraint |> constraint_expr() |> Enum.join("")
Copy link
Contributor

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.

Copy link
Contributor Author

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


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"
Copy link
Contributor

@ruslandoga ruslandoga Aug 26, 2023

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Contributor

@ruslandoga ruslandoga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️❤️❤️

@ruslandoga ruslandoga merged commit 46ce680 into plausible:master Aug 28, 2023
2 checks passed
@ruslandoga
Copy link
Contributor

ruslandoga commented Aug 28, 2023

@senconscious thank you! I released these changes as v0.2.1

end

unless constraint.validate do
raise "Clickhouse adapter does not support check constraints without validation on creation"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@senconscious 👋

I've been refactoring migrations and noticed

Constraint check will not be executed on existing data if it was added.

on https://clickhouse.com/docs/en/sql-reference/statements/alter/constraint

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't it mean that we should have the opposite check?

if constraint.validate do
  raise "ClickHouse does not support check constraints with validation on creation"
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants