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

adding a null constraint is unsafe #28

Closed
wants to merge 2 commits into from

Conversation

abcm1989
Copy link

@abcm1989 abcm1989 commented Oct 9, 2017

@ankane
Copy link
Owner

ankane commented Oct 10, 2017

Hey @abcm1989, thanks for the PR 👍 Can you explain more about what happened and why this should fix it?

@abcm1989
Copy link
Author

@ankane Ah, I got my logic flipped in the code, which I've fixed. Adding the constraint should be unsafe, rather than removing it.

We had a migration that added a null constraint using change_column_null that wouldn't run in some environments because there were rows with null values. I had it raising the error only if no default value is passed based on this from the docs

The method accepts an optional fourth argument to replace existing +NULL+s with some other value. Use that one when enabling the constraint if needed, since otherwise those rows would not be valid.

@ankane
Copy link
Owner

ankane commented Oct 26, 2017

Hey @abcm1989, I don't think adding a default value to replace NULLs is a safe operation.

@abcm1989
Copy link
Author

@ankane alrighty, updated so that adding a null constraint always is unsafe

@ankane
Copy link
Owner

ankane commented Oct 30, 2017

Hey @abcm1989, I'm not sure it's unsafe if all values are filled in.

@abcm1989
Copy link
Author

@ankane sorry can you rephrase, I'm not sure what you mean

@ankane
Copy link
Owner

ankane commented Oct 30, 2017

@abcm1989 Your initial report is about an error if you try to add a not null constraint and have rows with null values. If you don't have rows with null values, I believe this operation is safe.

@abcm1989
Copy link
Author

@ankane that's right, do you think it should not raise a warning at all then?

@ankane
Copy link
Owner

ankane commented Nov 7, 2017

Yeah, that's my current thinking.

@abcm1989
Copy link
Author

abcm1989 commented Nov 8, 2017

@ankane okay...just to make sure I understand, your position is that because the migration is safe sometimes, we shouldn't wrap it in a safety assured block?

It's a bit inconsistent that change_column_null would not require a safety assured block
while change_column :my_table, :my_column, :null => false would.

LMK your thoughts

@ankane
Copy link
Owner

ankane commented Nov 8, 2017

Hey @abcm1989, all change_column calls require a safety_assured block right now (doesn't have to do with changing null value). The one case that seems dangerous to me would be changing to null: false with a default, but I haven't tested it to see if it locks the table for an extended period if there are many null values (I suspect it would).

@abcm1989
Copy link
Author

abcm1989 commented Nov 9, 2017

@ankane okay, I see your point. I'll update this so it's only dangerous when setting a default value. It likely makes sense to catch change_column_default migrations too in that case

@ankane
Copy link
Owner

ankane commented Nov 9, 2017

@abcm1989 I haven't run into this, so would need a proof of concept. Also, changing the default by itself should be fine.

@abcm1989
Copy link
Author

abcm1989 commented Nov 9, 2017

gotcha, okay, well thanks for reviewing

@abcm1989 abcm1989 closed this Nov 9, 2017
@abcm1989 abcm1989 deleted the null-constraint-is-unsafe branch November 11, 2017 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants