|
| 1 | +"""IO-389: ProjectPhaseDetail, suspension fields, new phases and details.""" |
| 2 | + |
| 3 | +from django.db import migrations, models |
| 4 | +from django.db.models import Max |
| 5 | +import django.db.models.deletion |
| 6 | + |
| 7 | + |
| 8 | +def populate_phases_and_details(apps, schema_editor): |
| 9 | + ProjectPhase = apps.get_model("infraohjelmointi_api", "ProjectPhase") |
| 10 | + ProjectPhaseDetail = apps.get_model("infraohjelmointi_api", "ProjectPhaseDetail") |
| 11 | + Project = apps.get_model("infraohjelmointi_api", "Project") |
| 12 | + |
| 13 | + construction_phase = ProjectPhase.objects.filter(value="construction").first() |
| 14 | + if construction_phase: |
| 15 | + ProjectPhaseDetail.objects.filter(projectPhase__isnull=True).update( |
| 16 | + projectPhase=construction_phase |
| 17 | + ) |
| 18 | + Project.objects.exclude(phase=construction_phase).filter( |
| 19 | + phaseDetail__isnull=False |
| 20 | + ).update(phaseDetail=None) |
| 21 | + |
| 22 | + for phase in ProjectPhase.objects.all(): |
| 23 | + if phase.order is not None and phase.order != phase.index: |
| 24 | + phase.index = phase.order |
| 25 | + phase.save(update_fields=["index"]) |
| 26 | + |
| 27 | + construction_phase_obj = ProjectPhase.objects.filter(value="construction").first() |
| 28 | + if construction_phase_obj and construction_phase_obj.order is not None: |
| 29 | + insert_order = construction_phase_obj.order |
| 30 | + ProjectPhase.objects.filter(order__gte=insert_order).update( |
| 31 | + order=models.F("order") + 1, |
| 32 | + index=models.F("index") + 1, |
| 33 | + ) |
| 34 | + ProjectPhase.objects.get_or_create( |
| 35 | + value="constructionPreparation", |
| 36 | + defaults={"order": insert_order, "index": insert_order}, |
| 37 | + ) |
| 38 | + else: |
| 39 | + max_order = ProjectPhase.objects.aggregate(Max("order"))["order__max"] or -1 |
| 40 | + ProjectPhase.objects.get_or_create( |
| 41 | + value="constructionPreparation", |
| 42 | + defaults={"order": max_order + 1, "index": max_order + 1}, |
| 43 | + ) |
| 44 | + |
| 45 | + max_order = ProjectPhase.objects.aggregate(Max("order"))["order__max"] or -1 |
| 46 | + ProjectPhase.objects.get_or_create( |
| 47 | + value="suspended", |
| 48 | + defaults={"order": max_order + 1, "index": max_order + 1}, |
| 49 | + ) |
| 50 | + |
| 51 | + phase_details_to_create = { |
| 52 | + "programming": [ |
| 53 | + "waitingProjectManager", |
| 54 | + "waitingPlanningStart", |
| 55 | + ], |
| 56 | + "draftInitiation": [ |
| 57 | + "streetParkPlanDraft", |
| 58 | + ], |
| 59 | + "draftApproval": [ |
| 60 | + "streetParkPlanApproval", |
| 61 | + ], |
| 62 | + "constructionPlan": [ |
| 63 | + "constructionDesign", |
| 64 | + ], |
| 65 | + "constructionWait": [ |
| 66 | + "waitingFunding", |
| 67 | + ], |
| 68 | + "constructionPreparation": [ |
| 69 | + "movedToConstruction", |
| 70 | + "contractPreparation", |
| 71 | + ], |
| 72 | + } |
| 73 | + |
| 74 | + for phase_value, detail_values in phase_details_to_create.items(): |
| 75 | + phase = ProjectPhase.objects.filter(value=phase_value).first() |
| 76 | + if not phase: |
| 77 | + continue |
| 78 | + for detail_value in detail_values: |
| 79 | + ProjectPhaseDetail.objects.get_or_create( |
| 80 | + value=detail_value, |
| 81 | + projectPhase=phase, |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +def reverse_populate(apps, schema_editor): |
| 86 | + ProjectPhaseDetail = apps.get_model("infraohjelmointi_api", "ProjectPhaseDetail") |
| 87 | + ProjectPhase = apps.get_model("infraohjelmointi_api", "ProjectPhase") |
| 88 | + Project = apps.get_model("infraohjelmointi_api", "Project") |
| 89 | + |
| 90 | + new_detail_values = [ |
| 91 | + "waitingProjectManager", "waitingPlanningStart", |
| 92 | + "streetParkPlanDraft", "streetParkPlanApproval", |
| 93 | + "constructionDesign", "waitingFunding", |
| 94 | + "movedToConstruction", "contractPreparation", |
| 95 | + ] |
| 96 | + # Clear phaseDetail FK on projects before deleting the detail rows (DO_NOTHING won't cascade) |
| 97 | + Project.objects.filter(phaseDetail__value__in=new_detail_values).update(phaseDetail=None) |
| 98 | + ProjectPhaseDetail.objects.filter(value__in=new_detail_values).delete() |
| 99 | + |
| 100 | + phases_to_delete = ["constructionPreparation", "suspended"] |
| 101 | + # Move projects off phases that are about to be deleted |
| 102 | + fallback_phase = ProjectPhase.objects.filter(value="proposal").first() |
| 103 | + if fallback_phase: |
| 104 | + Project.objects.filter(phase__value__in=phases_to_delete).update( |
| 105 | + phase=fallback_phase |
| 106 | + ) |
| 107 | + # Clear suspendedFromPhase references |
| 108 | + Project.objects.filter(suspendedFromPhase__value__in=phases_to_delete).update( |
| 109 | + suspendedFromPhase=None |
| 110 | + ) |
| 111 | + ProjectPhase.objects.filter(value__in=phases_to_delete).delete() |
| 112 | + |
| 113 | + |
| 114 | +class Migration(migrations.Migration): |
| 115 | + dependencies = [ |
| 116 | + ("infraohjelmointi_api", "0096_alter_budgetoverrunreason_options_and_more"), |
| 117 | + ] |
| 118 | + |
| 119 | + operations = [ |
| 120 | + # 1. Rename model |
| 121 | + migrations.RenameModel( |
| 122 | + old_name="ConstructionPhaseDetail", |
| 123 | + new_name="ProjectPhaseDetail", |
| 124 | + ), |
| 125 | + # 2. Increase value max_length |
| 126 | + migrations.AlterField( |
| 127 | + model_name="projectphasedetail", |
| 128 | + name="value", |
| 129 | + field=models.CharField(max_length=100), |
| 130 | + ), |
| 131 | + # 3. Add projectPhase FK (nullable initially for data migration) |
| 132 | + migrations.AddField( |
| 133 | + model_name="projectphasedetail", |
| 134 | + name="projectPhase", |
| 135 | + field=models.ForeignKey( |
| 136 | + null=True, |
| 137 | + on_delete=django.db.models.deletion.CASCADE, |
| 138 | + related_name="phaseDetails", |
| 139 | + to="infraohjelmointi_api.projectphase", |
| 140 | + ), |
| 141 | + ), |
| 142 | + # 4. Rename FK on Project |
| 143 | + migrations.RenameField( |
| 144 | + model_name="project", |
| 145 | + old_name="constructionPhaseDetail", |
| 146 | + new_name="phaseDetail", |
| 147 | + ), |
| 148 | + # 5. Add suspension fields on Project |
| 149 | + migrations.AddField( |
| 150 | + model_name="project", |
| 151 | + name="suspendedDate", |
| 152 | + field=models.DateField(blank=True, null=True), |
| 153 | + ), |
| 154 | + migrations.AddField( |
| 155 | + model_name="project", |
| 156 | + name="suspendedFromPhase", |
| 157 | + field=models.ForeignKey( |
| 158 | + blank=True, |
| 159 | + null=True, |
| 160 | + on_delete=django.db.models.deletion.DO_NOTHING, |
| 161 | + related_name="suspended_projects", |
| 162 | + to="infraohjelmointi_api.projectphase", |
| 163 | + ), |
| 164 | + ), |
| 165 | + # 6. Data migration |
| 166 | + migrations.RunPython(populate_phases_and_details, reverse_populate), |
| 167 | + # 7. Make projectPhase required |
| 168 | + migrations.AlterField( |
| 169 | + model_name="projectphasedetail", |
| 170 | + name="projectPhase", |
| 171 | + field=models.ForeignKey( |
| 172 | + on_delete=django.db.models.deletion.CASCADE, |
| 173 | + related_name="phaseDetails", |
| 174 | + to="infraohjelmointi_api.projectphase", |
| 175 | + ), |
| 176 | + ), |
| 177 | + ] |
0 commit comments