Ensuring that duplicates are migrated correctly. (#496)

* Ensuring that duplicates are migrated correctly.

* fixing typo
This commit is contained in:
kevgliss 2016-11-15 16:43:45 -08:00 committed by GitHub
parent dd14fd202d
commit 9bb0787410
3 changed files with 14 additions and 14 deletions

View File

@ -43,7 +43,7 @@ def upgrade():
# migrate existing authority_id relationship to many_to_many
conn = op.get_bind()
for id, authority_id in conn.execute(text('select id, authority_id from roles where authority_id is not null')):
stmt = text('insert into roles_authorities (role_id, authority_id) values (:role_id, :authority_id)')
stmt = text('insert into roles_authoritties (role_id, authority_id) values (:role_id, :authority_id)')
stmt = stmt.bindparams(role_id=id, authority_id=authority_id)
op.execute(stmt)

View File

@ -1,4 +1,6 @@
"""Ensures that certificate name is unique
"""Ensures that certificate name is unique.
If duplicates are found, we follow the standard naming convention of appending '-X'
with x being the number of duplicates starting at 1.
Revision ID: 7f71c0cea31a
Revises: 29d8c8455c86
@ -17,19 +19,17 @@ from sqlalchemy.sql import text
def upgrade():
conn = op.get_bind()
for id, body, chain in conn.execute(text('select id, body, chain from certificates')):
if body and chain:
stmt = text('update certificates set body=:body, chain=:chain where id=:id')
stmt = stmt.bindparams(body=body.strip(), chain=chain.strip(), id=id)
else:
stmt = text('update certificates set body=:body where id=:id')
stmt = stmt.bindparams(body=body.strip(), id=id)
op.execute(stmt)
for name in conn.execute(text('select name from certificates group by name having count(*) > 1')):
for idx, id in enumerate(conn.execute(text("select id from certificates where certificates.name like :name order by id ASC").bindparams(name=name[0]))):
if not idx:
continue
new_name = name[0] + '-' + str(idx)
stmt = text('update certificates set name=:name where id=:id')
stmt = stmt.bindparams(name=new_name, id=id[0])
op.execute(stmt)
op.create_unique_constraint(None, 'certificates', ['name'])
def downgrade():
op.drop_constraint(None, 'certificates', type_='unique')
op.drop_constraint(None, 'certificates', type_='unique')

View File

@ -1,4 +1,4 @@
"""empty message
"""Changing the column name to the more accurately named 'notify'.
Revision ID: 932525b82f1a
Revises: 7f71c0cea31a