Skip to content

Commit

Permalink
Merge 1b1fcef into 5675f38
Browse files Browse the repository at this point in the history
  • Loading branch information
arielpontes committed Jan 19, 2021
2 parents 5675f38 + 1b1fcef commit 30c053e
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions gemet/thesaurus/migrations/0014_string_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,46 @@
# Generated by Django 1.10.6 on 2021-01-18 09:46
from __future__ import unicode_literals

from django.db import migrations, models
from django.db import migrations, models, connection


def forwards_func(apps, schema_editor):
def drop_if_exists(apps, schema_editor):
try:
for model_name in [
'concept', 'property', 'relation', 'foreignrelation'
]:
with connection.cursor() as cursor:
status_column_exists = cursor.execute(
"SHOW COLUMNS FROM thesaurus_{} LIKE 'status'".format(
model_name
),
)
string_status_column_exists = cursor.execute(
(
"SHOW COLUMNS FROM thesaurus_{} LIKE 'string_status'"
).format(
model_name
),
)
if string_status_column_exists and status_column_exists:
# The database is in an inconsistent state for some reason,
# so we must manually drop the 'string_status' column. No
# data should be lost as long as the 'status' column also
# exists.
cursor.execute(
(
"ALTER TABLE thesaurus_{}"
" DROP COLUMN string_status"
).format(
model_name
),
)
except Exception:
# During CI tests are run on SQLite and this is unecessary.
pass


def int_to_str(apps, schema_editor):
Concept = apps.get_model("thesaurus", "Concept")
Property = apps.get_model("thesaurus", "Property")
Relation = apps.get_model("thesaurus", "Relation")
Expand All @@ -24,7 +60,7 @@ def forwards_func(apps, schema_editor):
)


def reverse_func(apps, schema_editor):
def str_to_int(apps, schema_editor):
Concept = apps.get_model("thesaurus", "Concept")
Property = apps.get_model("thesaurus", "Property")
Relation = apps.get_model("thesaurus", "Relation")
Expand All @@ -50,6 +86,7 @@ class Migration(migrations.Migration):
]

operations = [
migrations.RunPython(drop_if_exists, migrations.RunPython.noop),
migrations.AddField(
model_name='concept',
name='string_status',
Expand All @@ -70,7 +107,7 @@ class Migration(migrations.Migration):
name='string_status',
field=models.CharField(choices=[(b'pending', b'pending'), (b'published', b'published'), (b'deleted', b'deleted'), (b'deleted_pending', b'deleted pending')], default=b'pending', max_length=64),
),
migrations.RunPython(forwards_func, reverse_func),
migrations.RunPython(int_to_str, str_to_int),
migrations.RemoveField(
model_name='concept',
name='status',
Expand Down

0 comments on commit 30c053e

Please sign in to comment.