From 11dbc56ecbcce0b800c9ff94eff9650f77f11b25 Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Fri, 21 Apr 2023 15:38:54 -0400 Subject: [PATCH] Updating old migrations for psycopg3 We have both psycopg2 and 3 installed in the AWX venv. Old versions of Django only used psycopg2 but 4.2 now supports 3 Django 4.2 detects psycopg3 first and will use that over psycopg2 So old migrations needed to be updated to support psycopg3 --- awx/main/migrations/0006_v320_release.py | 8 ++++++-- awx/main/migrations/0113_v370_event_bigint.py | 6 ++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/awx/main/migrations/0006_v320_release.py b/awx/main/migrations/0006_v320_release.py index d60d331192..0a851bab28 100644 --- a/awx/main/migrations/0006_v320_release.py +++ b/awx/main/migrations/0006_v320_release.py @@ -3,7 +3,7 @@ from __future__ import unicode_literals # Psycopg2 -from psycopg2.extensions import AsIs +from psycopg import sql # Django from django.db import connection, migrations, models, OperationalError, ProgrammingError @@ -136,7 +136,11 @@ class Migration(migrations.Migration): ), ), migrations.RunSQL( - [("CREATE INDEX host_ansible_facts_default_gin ON %s USING gin" "(ansible_facts jsonb_path_ops);", [AsIs(Host._meta.db_table)])], + [ + "CREATE INDEX host_ansible_facts_default_gin ON {} USING gin(ansible_facts jsonb_path_ops);".format( + sql.Identifier(Host._meta.db_table).as_string(connection.cursor()) + ) + ], [('DROP INDEX host_ansible_facts_default_gin;', None)], ), # SCM file-based inventories diff --git a/awx/main/migrations/0113_v370_event_bigint.py b/awx/main/migrations/0113_v370_event_bigint.py index fc85ac1708..8b32c768e7 100644 --- a/awx/main/migrations/0113_v370_event_bigint.py +++ b/awx/main/migrations/0113_v370_event_bigint.py @@ -22,10 +22,8 @@ def migrate_event_data(apps, schema_editor): # recreate counter for the new table's primary key to # start where the *old* table left off (we have to do this because the # counter changed from an int to a bigint) - cursor.execute(f'DROP SEQUENCE IF EXISTS "{tblname}_id_seq" CASCADE;') - cursor.execute(f'CREATE SEQUENCE "{tblname}_id_seq";') - cursor.execute(f'ALTER TABLE "{tblname}" ALTER COLUMN "id" ' f"SET DEFAULT nextval('{tblname}_id_seq');") - cursor.execute(f"SELECT setval('{tblname}_id_seq', (SELECT MAX(id) FROM _old_{tblname}), true);") + cursor.execute(f'CREATE SEQUENCE IF NOT EXISTS "{tblname}_id_seq";') + cursor.execute(f"SELECT setval('{tblname}_id_seq', COALESCE((SELECT MAX(id)+1 FROM _old_{tblname}), 1), false);") cursor.execute(f'DROP TABLE _old_{tblname};')