diff --git a/nummi/main/migrations/0016_alter_transaction_account_alter_transaction_snapshot.py b/nummi/main/migrations/0016_alter_transaction_account_alter_transaction_snapshot.py index b2e7a10..bf3f148 100644 --- a/nummi/main/migrations/0016_alter_transaction_account_alter_transaction_snapshot.py +++ b/nummi/main/migrations/0016_alter_transaction_account_alter_transaction_snapshot.py @@ -4,6 +4,16 @@ import django.db.models.deletion from django.db import migrations, models +def set_snapshot(apps, schema_editor): + Transaction = apps.get_model("main", "Transaction") + Snapshot = apps.get_model("main", "Snapshot") + for transaction in Transaction.objects.filter(snapshot__isnull=True): + transaction.snapshot = ( + Snapshot.objects.filter(user=transaction.user).order_by("-date").first() + ) + transaction.save() + + class Migration(migrations.Migration): dependencies = [ @@ -23,6 +33,7 @@ class Migration(migrations.Migration): verbose_name="Account", ), ), + migrations.RunPython(set_snapshot), migrations.AlterField( model_name="transaction", name="snapshot", diff --git a/nummi/main/migrations/0017_alter_transaction_account.py b/nummi/main/migrations/0017_alter_transaction_account.py index bbe985d..e8f1385 100644 --- a/nummi/main/migrations/0017_alter_transaction_account.py +++ b/nummi/main/migrations/0017_alter_transaction_account.py @@ -4,13 +4,33 @@ import django.db.models.deletion from django.db import migrations, models +def set_account(apps, schema_editor): + Transaction = apps.get_model("main", "Transaction") + Snapshot = apps.get_model("main", "Snapshot") + Account = apps.get_model("main", "Account") + for snapshot in Snapshot.objects.filter(account__isnull=True): + account = Account.objects.filter(user=snapshot.user).first() + if account is None: + account = Account(user=snapshot.user) + account.save() + snapshot.account = account + snapshot.save() + for transaction in Transaction.objects.filter( + account__isnull=True, snapshot__isnull=False + ): + transaction.account = transaction.snapshot.account + transaction.save() + + class Migration(migrations.Migration): + atomic = False dependencies = [ ("main", "0016_alter_transaction_account_alter_transaction_snapshot"), ] operations = [ + migrations.RunPython(set_account, atomic=True), migrations.AlterField( model_name="transaction", name="account",