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

Add safe_make_column_not_nullable #114

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

alexspeller
Copy link

Addresses #106

I also added some quoting for some related methods where it seemed to be missing

Copy link
Contributor

@jcoleman jcoleman left a comment

Choose a reason for hiding this comment

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

I'm wondering if we want to also (whether it's in this PR or not) add a method that converts a validated check constraint to a regular NOT NULL constraint (the benefit basically being that it would assert that the constraint exists and is already validated).

README.md Outdated Show resolved Hide resolved
lib/pg_ha_migrations/safe_statements.rb Outdated Show resolved Hide resolved
lib/pg_ha_migrations/safe_statements.rb Outdated Show resolved Hide resolved
lib/pg_ha_migrations/safe_statements.rb Show resolved Hide resolved
quoted_table_name = connection.quote_table_name(table)
quoted_column_name = connection.quote_column_name(column)

tmp_constraint_name = :tmp_not_null_constraint
Copy link
Contributor

Choose a reason for hiding this comment

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

Are constraint names global? If so, do we want to add a random tag on the end of this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I think constraint names need to be unique within a schema. Because this method is doing multiple things non-transactionally, there is a possibility we could error out before the constraint is removed. So a) I agree with @jcoleman that we should add some random characters to this string and b) we should document that this might leave behind a constraint on failure, similar to creating a partitioned index.

Copy link
Author

@alexspeller alexspeller Jan 7, 2025

Choose a reason for hiding this comment

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

The documentation says that constraints are only unique per-table:

https://www.postgresql.org/docs/current/sql-set-constraints.html#:~:text=Because%20PostgreSQL%20does%20not%20require,will%20act%20on%20all%20matches.

I don't have a strong opinion on the naming of the constraint or if it should still have randomness regardless

Copy link
Contributor

Choose a reason for hiding this comment

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

I think adding a random tag would make re-running a failed migration here safer, albeit at the cost of you not discovering what's been left behind by the previous run.

I think I'd probably say this: let's add a tag using a the first N chars of a sha of the column name (so it's stable), and at the beginning of the method raise a specific error if that constraint already exists.

spec/safe_statements_spec.rb Show resolved Hide resolved
@alexspeller alexspeller requested review from rkrage and jcoleman January 7, 2025 01:24
@jcoleman
Copy link
Contributor

@alexspeller Apologies, for some reason I'm not getting notifications on this repo anymore; I'll take a look at this again.

@jcoleman
Copy link
Contributor

jcoleman commented Feb 4, 2025

@alexspeller I think @rkrage are going to add wrapping this PR up to our internal development list, and after we wrap it up, we'll merge.

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.

3 participants