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

safe_make_column_not_nullable #106

Open
alexspeller opened this issue Nov 6, 2024 · 1 comment
Open

safe_make_column_not_nullable #106

alexspeller opened this issue Nov 6, 2024 · 1 comment

Comments

@alexspeller
Copy link

I think it would be possible to add a safe_make_column_not_nullable method, source

I think something like:

  def safe_make_column_not_nullable(table, column)
    unsafe_execute "ALTER TABLE #{table} ADD CONSTRAINT tmp_not_null_constraint CHECK (#{column} IS NOT NULL) NOT VALID"
    unsafe_execute "ALTER TABLE #{table} VALIDATE CONSTRAINT tmp_not_null_constraint"
    unsafe_execute "ALTER TABLE #{table} ALTER COLUMN #{column} SET NOT NULL"
    unsafe_execute "ALTER TABLE #{table} DROP CONSTRAINT tmp_not_null_constraint"
  end
@rkrage
Copy link
Contributor

rkrage commented Nov 27, 2024

Seems like a good idea to me!

Some notes:

  • We should probably have a guard at the top of the method that raises an error if not on PG 12+
  • We should call out to existing methods when possible to avoid duplication
  • When executing raw SQL we should be sure the table / column names are properly quoted

If you want to take a stab at a PR that would be awesome

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

No branches or pull requests

2 participants