From 49d06f5a10e66e763182f6d2c7c991c16180bf65 Mon Sep 17 00:00:00 2001 From: Rudyard Richter Date: Fri, 15 Feb 2019 15:39:28 -0600 Subject: [PATCH] fix(migration): make entity id nullable --- datamodelutils/postgres_admin.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/datamodelutils/postgres_admin.py b/datamodelutils/postgres_admin.py index e6aa592..a33300c 100644 --- a/datamodelutils/postgres_admin.py +++ b/datamodelutils/postgres_admin.py @@ -162,14 +162,24 @@ def migrate_transaction_snapshots(driver): tablename = models.submission.TransactionSnapshot.__tablename__ snapshots_table = Table(tablename, md, autoload=True) if "entity_id" not in snapshots_table.c: + # change existing `id` column to `entity_id` which is just node UUID, doesn't + # have to be unique, should not be used as primary key + try: + execute( + driver, + "ALTER TABLE {name} DROP CONSTRAINT {name}_pkey;".format(name=tablename), + ) + except sa.exc.ProgrammingError: + pass execute( driver, - "ALTER TABLE {name} DROP CONSTRAINT {name}_pkey".format(name=tablename), + "ALTER TABLE {} RENAME id TO entity_id;".format(tablename), ) execute( driver, - "ALTER TABLE {} RENAME id TO entity_id".format(tablename), + "ALTER TABLE {} ALTER COLUMN entity_id DROP NOT NULL;".format(tablename), ) + # make new serial `id` column which *is* used for primary key execute( driver, "ALTER TABLE {} ADD COLUMN id SERIAL PRIMARY KEY;".format(tablename),