This repository has been archived by the owner on Jun 15, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #187: convert uniqkeys to hashes and remove dependence on long te…
…xt fields
- Loading branch information
1 parent
71db6df
commit ea31e49
Showing
3 changed files
with
222 additions
and
19 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
alembic/versions/41996f6d756_change_uniques_to_sha1_keys.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,62 @@ | ||
"""change uniques to sha1 keys | ||
Revision ID: 41996f6d756 | ||
Revises: 54672c4d904 | ||
Create Date: 2015-03-27 21:22:02.796920 | ||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '41996f6d756' | ||
down_revision = '54672c4d904' | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
from pynab.db import Release, windowed_query | ||
import hashlib | ||
from sqlalchemy.orm import sessionmaker | ||
|
||
def upgrade(): | ||
# drop duplicate pres | ||
conn = op.get_bind() | ||
|
||
conn.execute(''' | ||
delete from pres | ||
where id not in | ||
( | ||
select min(id) | ||
from pres | ||
group by requestid, pretime, requestgroup | ||
) | ||
''') | ||
|
||
### commands auto generated by Alembic - please adjust! ### | ||
op.drop_constraint('pres_name_key', 'pres', type_='unique') | ||
op.create_unique_constraint('pres_uniq', 'pres', ['requestid', 'pretime', 'requestgroup']) | ||
op.add_column('releases', sa.Column('uniqhash', sa.String(length=40), nullable=True)) | ||
op.drop_constraint('releases_name_group_id_posted_key', 'releases', type_='unique') | ||
op.create_unique_constraint('releases_uniq', 'releases', ['uniqhash']) | ||
|
||
session = sessionmaker(bind=conn)() | ||
# update the hashes | ||
q = session.query(Release) | ||
for release in windowed_query(q, Release.id, 1000): | ||
release.uniqhash = hashlib.sha1( | ||
'{}.{}.{}'.format( | ||
release.name, | ||
release.group_id, | ||
release.posted, | ||
).encode('utf-8')).hexdigest() | ||
|
||
session.commit() | ||
### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
### commands auto generated by Alembic - please adjust! ### | ||
op.drop_constraint('releases_uniq', 'releases', type_='unique') | ||
op.create_unique_constraint('releases_name_group_id_posted_key', 'releases', ['name', 'group_id', 'posted']) | ||
op.drop_column('releases', 'uniqhash') | ||
op.drop_constraint('pres_uniq', 'pres', type_='unique') | ||
op.create_unique_constraint('pres_name_key', 'pres', ['name']) | ||
### end Alembic commands ### |
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
Oops, something went wrong.