From 9bb07874105672a924937b02e8b5de3fa6ab9870 Mon Sep 17 00:00:00 2001 From: kevgliss Date: Tue, 15 Nov 2016 16:43:45 -0800 Subject: [PATCH] Ensuring that duplicates are migrated correctly. (#496) * Ensuring that duplicates are migrated correctly. * fixing typo --- lemur/migrations/versions/412b22cb656a_.py | 2 +- lemur/migrations/versions/7f71c0cea31a_.py | 24 +++++++++++----------- lemur/migrations/versions/932525b82f1a_.py | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lemur/migrations/versions/412b22cb656a_.py b/lemur/migrations/versions/412b22cb656a_.py index 2e0c811d..d95ec701 100644 --- a/lemur/migrations/versions/412b22cb656a_.py +++ b/lemur/migrations/versions/412b22cb656a_.py @@ -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) diff --git a/lemur/migrations/versions/7f71c0cea31a_.py b/lemur/migrations/versions/7f71c0cea31a_.py index ab42322b..78353209 100644 --- a/lemur/migrations/versions/7f71c0cea31a_.py +++ b/lemur/migrations/versions/7f71c0cea31a_.py @@ -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') \ No newline at end of file diff --git a/lemur/migrations/versions/932525b82f1a_.py b/lemur/migrations/versions/932525b82f1a_.py index 9bab2634..2ee95d07 100644 --- a/lemur/migrations/versions/932525b82f1a_.py +++ b/lemur/migrations/versions/932525b82f1a_.py @@ -1,4 +1,4 @@ -"""empty message +"""Changing the column name to the more accurately named 'notify'. Revision ID: 932525b82f1a Revises: 7f71c0cea31a