From c6ad90eca9584c03ebda0809e6b123ba5216e6ac Mon Sep 17 00:00:00 2001 From: Tim Glaser Date: Mon, 19 Apr 2021 10:24:52 +0200 Subject: [PATCH] Skip migration 0141 if partitioned Closes #3993 --- .../0141_events_created_at_index.py | 23 +++++++++++++++---- posthog/migrations/0143_user_uuid.py | 6 ++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/posthog/migrations/0141_events_created_at_index.py b/posthog/migrations/0141_events_created_at_index.py index beaf7b57885..c511340b150 100644 --- a/posthog/migrations/0141_events_created_at_index.py +++ b/posthog/migrations/0141_events_created_at_index.py @@ -1,7 +1,24 @@ # Generated by Django 3.0.11 on 2021-03-26 12:10 from django.contrib.postgres.operations import AddIndexConcurrently # type: ignore -from django.db import migrations, models +from django.db import connection, migrations, models + + +def migrate(apps, schema_editor): + with connection.cursor() as cursor: + cursor.execute( + """SELECT exists(SELECT FROM information_schema.tables where table_name = \'posthog_event_default\')""" + ) + exists = cursor.fetchone() + if not exists[0]: + cursor.execute("CREATE INDEX CONCURRENTLY posthog_eve_created_6a34ca_idx ON posthog_event(created_at)") + return + else: + print("Skipping migration 0141 because of partitions!") + + +def backwards(apps, schema_editor): + pass class Migration(migrations.Migration): @@ -12,7 +29,5 @@ class Migration(migrations.Migration): ] operations = [ - AddIndexConcurrently( - model_name="event", index=models.Index(fields=["created_at"], name="posthog_eve_created_6a34ca_idx"), - ), + migrations.RunPython(migrate, backwards), ] diff --git a/posthog/migrations/0143_user_uuid.py b/posthog/migrations/0143_user_uuid.py index 4784b3591de..65cbd89b5d4 100644 --- a/posthog/migrations/0143_user_uuid.py +++ b/posthog/migrations/0143_user_uuid.py @@ -12,6 +12,10 @@ def create_user_uuid(apps, schema_editor): user.save() +def backwards(app, schema_editor): + pass + + class Migration(migrations.Migration): dependencies = [ @@ -20,7 +24,7 @@ class Migration(migrations.Migration): operations = [ migrations.AddField(model_name="user", name="uuid", field=models.UUIDField(blank=True, null=True),), - migrations.RunPython(create_user_uuid), + migrations.RunPython(create_user_uuid, backwards), migrations.AlterField( model_name="user", name="uuid",