|
| 1 | +# Generated by Django 4.2.10 on 2025-03-09 23:41 |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +from django.db import migrations, models |
| 6 | +import django.db.models.deletion |
| 7 | +from django.utils import timezone |
| 8 | + |
| 9 | +logger = logging.getLogger(__name__) |
| 10 | + |
| 11 | + |
| 12 | +def transfer_m2m_data(apps, schema_editor): |
| 13 | + Pipeline = apps.get_model("ml", "Pipeline") |
| 14 | + ProjectPipelineConfig = apps.get_model("ml", "ProjectPipelineConfig") |
| 15 | + |
| 16 | + logger.info("Transferring Project-Pipeline M2M data to ProjectPipelineConfig") |
| 17 | + for pipeline in Pipeline.objects.only("id").all(): |
| 18 | + # Create new ProjectPipelineConfig entries |
| 19 | + for existing_project in pipeline.projects.only("id").all(): |
| 20 | + logger.info(f"Creating ProjectPipelineConfig for {existing_project} and {pipeline}") |
| 21 | + ProjectPipelineConfig.objects.create( |
| 22 | + project_id=existing_project.pk, |
| 23 | + pipeline_id=pipeline.pk, |
| 24 | + enabled=True, |
| 25 | + config={}, |
| 26 | + ) |
| 27 | + |
| 28 | + |
| 29 | +def revert_m2m_data(apps, schema_editor): |
| 30 | + # collect all projects related to pipelines through ProjectPipelineConfig |
| 31 | + # and set the projects field in Pipeline to those projects |
| 32 | + ProjectPipelineConfig = apps.get_model("ml", "ProjectPipelineConfig") |
| 33 | + Pipeline = apps.get_model("ml", "Pipeline") |
| 34 | + Project = apps.get_model("main", "Project") |
| 35 | + |
| 36 | + logger.info("Reverting Project-Pipeline M2M data from ProjectPipelineConfig") |
| 37 | + for pipeline in Pipeline.objects.only("id").all(): |
| 38 | + project_ids = ProjectPipelineConfig.objects.filter(pipeline_id=pipeline.pk).values_list( |
| 39 | + "project_id", flat=True |
| 40 | + ) |
| 41 | + projects = Project.objects.filter(id__in=project_ids) |
| 42 | + pipeline.projects.set(projects) |
| 43 | + |
| 44 | + |
| 45 | +# Generated by Django 4.2.10 on 2025-03-09 23:41 |
| 46 | + |
| 47 | + |
| 48 | +class Migration(migrations.Migration): |
| 49 | + dependencies = [ |
| 50 | + ("main", "0058_alter_project_options"), |
| 51 | + ("ml", "0019_alter_algorithm_task_type"), |
| 52 | + ] |
| 53 | + |
| 54 | + operations = [ |
| 55 | + migrations.CreateModel( |
| 56 | + name="ProjectPipelineConfig", |
| 57 | + fields=[ |
| 58 | + ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")), |
| 59 | + ("created_at", models.DateTimeField(auto_now_add=True)), |
| 60 | + ("updated_at", models.DateTimeField(auto_now=True)), |
| 61 | + ("enabled", models.BooleanField(default=True)), |
| 62 | + ("config", models.JSONField(blank=True, default=dict, null=True)), |
| 63 | + ( |
| 64 | + "pipeline", |
| 65 | + models.ForeignKey( |
| 66 | + on_delete=django.db.models.deletion.CASCADE, |
| 67 | + related_name="project_pipeline_configs", |
| 68 | + to="ml.pipeline", |
| 69 | + ), |
| 70 | + ), |
| 71 | + ( |
| 72 | + "project", |
| 73 | + models.ForeignKey( |
| 74 | + on_delete=django.db.models.deletion.CASCADE, |
| 75 | + related_name="project_pipeline_configs", |
| 76 | + to="main.project", |
| 77 | + ), |
| 78 | + ), |
| 79 | + ], |
| 80 | + options={ |
| 81 | + "verbose_name": "Project-Pipeline Configuration", |
| 82 | + "verbose_name_plural": "Project-Pipeline Configurations", |
| 83 | + "unique_together": {("pipeline", "project")}, |
| 84 | + }, |
| 85 | + ), |
| 86 | + migrations.AddField( |
| 87 | + model_name="pipeline", |
| 88 | + name="new_projects", |
| 89 | + field=models.ManyToManyField( |
| 90 | + blank=True, related_name="new_pipelines", through="ml.ProjectPipelineConfig", to="main.Project" |
| 91 | + ), |
| 92 | + ), |
| 93 | + migrations.RunPython(transfer_m2m_data, reverse_code=revert_m2m_data), |
| 94 | + migrations.RemoveField( |
| 95 | + model_name="pipeline", |
| 96 | + name="projects", |
| 97 | + ), |
| 98 | + migrations.RenameField( |
| 99 | + model_name="pipeline", |
| 100 | + old_name="new_projects", |
| 101 | + new_name="projects", |
| 102 | + ), |
| 103 | + migrations.AlterField( |
| 104 | + model_name="pipeline", |
| 105 | + name="projects", |
| 106 | + field=models.ManyToManyField( |
| 107 | + blank=True, related_name="pipelines", through="ml.ProjectPipelineConfig", to="main.Project" |
| 108 | + ), |
| 109 | + ), |
| 110 | + ] |
0 commit comments