-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #250 from open-contracting/change-uniqueness-rules…
…-for-collections Change uniqueness rules for collections
- Loading branch information
Showing
4 changed files
with
52 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...s/maindatabase/migrations/versions/8e3f80979dc9_change_unique_constraint_on_collection.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"""Change unique constraint on collection | ||
Revision ID: 8e3f80979dc9 | ||
Revises: 3d5fae27a215 | ||
Create Date: 2019-12-18 13:14:56.466907 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '8e3f80979dc9' | ||
down_revision = '3d5fae27a215' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
""" | ||
SELECT source_id, data_version, sample, COUNT(*) FROM collection | ||
WHERE transform_type IS NULL or transform_type = '' | ||
GROUP BY source_id, data_version, sample | ||
HAVING COUNT(*) > 1; | ||
""" | ||
# 0 rows | ||
|
||
op.drop_constraint('unique_collection_identifiers', 'collection') | ||
op.create_index('unique_collection_identifiers', 'collection', ['source_id', 'data_version', 'sample'], | ||
unique=True, postgresql_where=sa.text("transform_type = ''")) | ||
|
||
op.execute("UPDATE collection SET transform_type = '' WHERE transform_type IS NULL") | ||
op.alter_column('collection', 'transform_type', nullable=False) | ||
|
||
|
||
def downgrade(): | ||
op.drop_index('unique_collection_identifiers', 'collection') | ||
op.create_unique_constraint('unique_collection_identifiers', 'collection', [ | ||
'source_id', 'data_version', 'sample', 'transform_from_collection_id', 'transform_type', | ||
]) | ||
|
||
op.alter_column('collection', 'transform_type', nullable=True) | ||
op.execute("UPDATE collection SET transform_type = NULL WHERE transform_type = ''") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters