Migration bug fix 2 (#4977)

* Additional migration fix:

- In #4961 we did not notice that the migration files had been renamed
- There is still a chance that a production db (running from master) has a corrupted set of migrations
- Check if the duplicate columns already exist
- If they do, delete them first

* Typo fix

* Add PR reference
This commit is contained in:
Oliver 2023-06-06 15:45:24 +10:00 committed by GitHub
parent ba24ff570a
commit f65281c801
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,8 +1,40 @@
# Generated by Django 3.2.19 on 2023-05-31 12:05
from django.core.exceptions import FieldDoesNotExist
from django.db import migrations, models
def delete_columns(apps, schema_editor):
"""Hack method to delete columns (if they already exist).
Due to an improper set of migrations merges,
there may exist a situation where the columns (defined in the migrations below) already exist.
In this case, we want to delete the columns, and then re-add them.
Original error: https://github.com/inventree/InvenTree/pull/4898
Attempted fix: https://github.com/inventree/InvenTree/pull/4961
This fix: https://github.com/inventree/InvenTree/pull/4977
"""
PartParameterTemplate = apps.get_model('part', 'PartParameterTemplate')
# Check if the 'checkbox' column exists
try:
print("Checking for column 'checkbox' in table 'part_partparametertemplate'")
PartParameterTemplate._meta.get_field('checkbox')
schema_editor.execute("ALTER TABLE part_partparametertemplate DROP COLUMN checkbox;")
except (AttributeError, FieldDoesNotExist):
print("Column 'checkbox' does not exist (skipping)")
try:
print("Checking for column 'choices' in table 'part_partparametertemplate'")
PartParameterTemplate._meta.get_field('choices')
schema_editor.execute("ALTER TABLE part_partparametertemplate DROP COLUMN choices;")
except (AttributeError, FieldDoesNotExist):
print("Column 'choices' does not exist (skipping)")
class Migration(migrations.Migration):
dependencies = [
@ -10,6 +42,9 @@ class Migration(migrations.Migration):
]
operations = [
migrations.RunPython(
delete_columns, reverse_code=migrations.RunPython.noop
),
migrations.AddField(
model_name='partparametertemplate',
name='checkbox',