# Fixes a DB/migration-state mismatch: migration 0002 was edited after being
# applied, so Django's recorded state (cargo_quantity, cargo_unit,
# missing_quantity) never matched the actual sqlite table, which still had
# the old columns (cargo_description, missing_weight) from before that edit.
# This migration brings the real table in line with the model state that
# 0002 already claims, and migrates any data from the old missing_weight
# column instead of dropping it silently.
from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('trucks', '0002_truckmovement_cargo_description_and_more'),
    ]

    operations = [
        migrations.RunSQL(
            sql=[
                "ALTER TABLE trucks_truckmovement ADD COLUMN cargo_quantity decimal NULL",
                "ALTER TABLE trucks_truckmovement ADD COLUMN cargo_unit varchar(20) NULL",
                "ALTER TABLE trucks_truckmovement ADD COLUMN missing_quantity decimal NULL",
                "UPDATE trucks_truckmovement SET missing_quantity = missing_weight",
                "ALTER TABLE trucks_truckmovement DROP COLUMN missing_weight",
            ],
            reverse_sql=[
                "ALTER TABLE trucks_truckmovement ADD COLUMN missing_weight decimal NULL",
                "UPDATE trucks_truckmovement SET missing_weight = missing_quantity",
                "ALTER TABLE trucks_truckmovement DROP COLUMN missing_quantity",
                "ALTER TABLE trucks_truckmovement DROP COLUMN cargo_unit",
                "ALTER TABLE trucks_truckmovement DROP COLUMN cargo_quantity",
            ],
            state_operations=[],
        ),
    ]
