Updated migrations to enable transition from earlier db versions

User action is needed to cleanup everything afterwards
This commit is contained in:
Edgar P. Burkhart 2022-12-30 14:10:33 +01:00
parent e438cfd78a
commit ea9cd1b9b8
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
2 changed files with 31 additions and 0 deletions

View file

@ -4,6 +4,16 @@ import django.db.models.deletion
from django.db import migrations, models 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): class Migration(migrations.Migration):
dependencies = [ dependencies = [
@ -23,6 +33,7 @@ class Migration(migrations.Migration):
verbose_name="Account", verbose_name="Account",
), ),
), ),
migrations.RunPython(set_snapshot),
migrations.AlterField( migrations.AlterField(
model_name="transaction", model_name="transaction",
name="snapshot", name="snapshot",

View file

@ -4,13 +4,33 @@ import django.db.models.deletion
from django.db import migrations, models 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): class Migration(migrations.Migration):
atomic = False
dependencies = [ dependencies = [
("main", "0016_alter_transaction_account_alter_transaction_snapshot"), ("main", "0016_alter_transaction_account_alter_transaction_snapshot"),
] ]
operations = [ operations = [
migrations.RunPython(set_account, atomic=True),
migrations.AlterField( migrations.AlterField(
model_name="transaction", model_name="transaction",
name="account", name="account",