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 # migrate existing authority_id relationship to many_to_many
conn = op.get_bind() conn = op.get_bind()
for id, authority_id in conn.execute(text('select id, authority_id from roles where authority_id is not null')): 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) stmt = stmt.bindparams(role_id=id, authority_id=authority_id)
op.execute(stmt) 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 Revision ID: 7f71c0cea31a
Revises: 29d8c8455c86 Revises: 29d8c8455c86
@ -17,16 +19,14 @@ from sqlalchemy.sql import text
def upgrade(): def upgrade():
conn = op.get_bind() conn = op.get_bind()
for name in conn.execute(text('select name from certificates group by name having count(*) > 1')):
for id, body, chain in conn.execute(text('select id, body, chain from certificates')): 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 body and chain: if not idx:
stmt = text('update certificates set body=:body, chain=:chain where id=:id') continue
stmt = stmt.bindparams(body=body.strip(), chain=chain.strip(), id=id) new_name = name[0] + '-' + str(idx)
else: stmt = text('update certificates set name=:name where id=:id')
stmt = text('update certificates set body=:body where id=:id') stmt = stmt.bindparams(name=new_name, id=id[0])
stmt = stmt.bindparams(body=body.strip(), id=id) op.execute(stmt)
op.execute(stmt)
op.create_unique_constraint(None, 'certificates', ['name']) op.create_unique_constraint(None, 'certificates', ['name'])

View File

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