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

Migrations: fix the rename of name to label for DbComputer #4926

Merged

Conversation

sphuber
Copy link
Contributor

@sphuber sphuber commented May 6, 2021

Fixes #4925

The migration was broken for SqlAlchemy and wasn't detected because
there were no tests. The migration of new profiles still worked because
it only failed for databases that have at least one computer.

The new migration properly uses alter_column to change the column
name, instead of creating a new one and dropping the old one. Not only
does this incur the exception that the new column will have null values,
which is not allowed since it is not nullable, but it would also have
lost all the data.

Finally, the label column has a uniqueness constraint and the name
should technically also be changed, so the constraint is dropped and
added with the new name based on label.

@codecov
Copy link

codecov bot commented May 6, 2021

Codecov Report

Merging #4926 (2940666) into develop (bff117a) will decrease coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #4926      +/-   ##
===========================================
- Coverage    80.06%   80.05%   -0.00%     
===========================================
  Files          515      515              
  Lines        36622    36619       -3     
===========================================
- Hits         29316    29313       -3     
  Misses        7306     7306              
Flag Coverage Δ
django 74.51% <0.00%> (+0.04%) ⬆️
sqlalchemy 73.44% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...ns/versions/535039300e4a_computer_name_to_label.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bff117a...2940666. Read the comment docs.

@sphuber sphuber requested a review from giovannipizzi May 7, 2021 07:41
op.create_unique_constraint('db_dbcomputer_label_key', 'db_dbcomputer', ['label'])
op.drop_column('db_dbcomputer', 'name')
op.drop_constraint('db_dbcomputer_name_key', 'db_dbcomputer')
Copy link
Member

Choose a reason for hiding this comment

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

What happens to this constraint when you alter the column? Maybe it's better to drop it before altering (also for performance, so the index does not exist and does not need to be updated while altering)? Same for downgrade (drop before altering)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure to be honest. Since this should be a small index (shouldn't be a huge number of computers) I think it won't have a huge impact, but happy to swap the order.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

swapped them around

The migration was broken for SqlAlchemy and wasn't detected because
there were no tests. The migration of new profiles still worked because
it only failed for databases that have at least one computer.

The new migration properly uses `alter_column` to change the column
name, instead of creating a new one and dropping the old one. Not only
does this incur the exception that the new column will have null values,
which is not allowed since it is not nullable, but it would also have
lost all the data.

Finally, the `label` column has a uniqueness constraint and the name
should technically also be changed, so the constraint is dropped and
added with the new name based on `label`.
@sphuber sphuber force-pushed the fix/4925/migration-computer-name-to-label branch from 966126c to 2940666 Compare May 7, 2021 08:54
@sphuber sphuber requested a review from giovannipizzi May 7, 2021 09:28
Copy link
Member

@giovannipizzi giovannipizzi left a comment

Choose a reason for hiding this comment

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

Thanks! I guess that it would also just work to rename the constraint - I don't see any other thing the DB can do other than making db_dbcomputer_label_key actually be an index/constraing on the new column. But I think that in this way we're safe

@sphuber sphuber merged commit e0003c7 into aiidateam:develop May 7, 2021
@sphuber sphuber deleted the fix/4925/migration-computer-name-to-label branch May 7, 2021 10:21
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.

Migration for Computer column name rename to label is broken for SqlAlchemy
2 participants