Skip to content

Commit

Permalink
fixed migration with sql
Browse files Browse the repository at this point in the history
  • Loading branch information
federicofantini committed Jan 9, 2024
1 parent 3161e99 commit c51ea3d
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions web/users/migrations/0003_rename_field_subscription.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
from django.db import migrations
from django.db import connection, migrations


def migrate(apps, schema_editor):
with schema_editor.connection.cursor() as cursor:
if connection.vendor == "postgresql":
cursor.execute("SELECT column_name FROM information_schema.columns WHERE table_name = 'users_userprofile';")
elif connection.vendor == "sqlite":
cursor.execute("SELECT sql FROM sqlite_master WHERE tbl_name = 'users_userprofile' AND type = 'table';")
columns = [el[0] for el in cursor.fetchall()]
if "suscription" in columns:
cursor.execute("ALTER TABLE users_userprofile RENAME COLUMN suscription TO subscription;")


def reverse_migrate(apps, schema_editor):
with schema_editor.connection.cursor() as cursor:
if connection.vendor == "postgresql":
cursor.execute("SELECT column_name FROM information_schema.columns WHERE table_name = 'users_userprofile';")
elif connection.vendor == "sqlite":
cursor.execute("SELECT sql FROM sqlite_master WHERE tbl_name = 'users_userprofile' AND type = 'table';")
columns = [el[0] for el in cursor.fetchall()]
if "subscription" in columns:
cursor.execute("ALTER TABLE users_userprofile RENAME COLUMN subscription TO suscription;")


class Migration(migrations.Migration):
Expand All @@ -7,10 +29,4 @@ class Migration(migrations.Migration):
("users", "0002_reports"),
]

operations = [
migrations.RenameField(
model_name="userprofile",
old_name="suscription",
new_name="subscription",
),
]
operations = [migrations.RunPython(migrate, reverse_migrate)]

0 comments on commit c51ea3d

Please sign in to comment.