Updated migrations to enable transition from earlier db versions
User action is needed to cleanup everything afterwards
This commit is contained in:
parent
e438cfd78a
commit
ea9cd1b9b8
2 changed files with 31 additions and 0 deletions
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
Loading…
Reference in a new issue