|
| 1 | +""" |
| 2 | +Management command to backfill auto-managed tasks (git pull/clone, dbt clean, dbt deps) |
| 3 | +in all existing pipelines. |
| 4 | +
|
| 5 | +For each orchestrate pipeline that has transform tasks, this command will |
| 6 | +re-run update_pipeline which automatically adds the missing auto-managed steps |
| 7 | +based on the org's workpool configuration. |
| 8 | +""" |
| 9 | + |
| 10 | +from django.core.management.base import BaseCommand |
| 11 | +from ddpui.models.org import Org, OrgDataFlowv1 |
| 12 | +from ddpui.models.tasks import DataflowOrgTask, TaskType |
| 13 | +from ddpui.ddpprefect.schema import PrefectDataFlowUpdateSchema3 |
| 14 | +from ddpui.ddpprefect import prefect_service |
| 15 | +from ddpui.utils.constants import TASK_DBTCLEAN, TASK_DBTDEPS |
| 16 | +from ddpui.utils.unified_logger import get_logger |
| 17 | +from ddpui.core.orchestrate.pipeline_service import PipelineService |
| 18 | + |
| 19 | +logger = get_logger() |
| 20 | + |
| 21 | + |
| 22 | +class Command(BaseCommand): |
| 23 | + help = "Backfill auto-managed tasks (git pull/clone, dbt clean, dbt deps) in all existing pipelines" |
| 24 | + |
| 25 | + def add_arguments(self, parser): |
| 26 | + parser.add_argument( |
| 27 | + "--org-slug", |
| 28 | + type=str, |
| 29 | + required=False, |
| 30 | + help="Only backfill for a specific organization (optional)", |
| 31 | + ) |
| 32 | + parser.add_argument( |
| 33 | + "--dry-run", |
| 34 | + action="store_true", |
| 35 | + help="Show what would be changed without making actual changes", |
| 36 | + ) |
| 37 | + |
| 38 | + def handle(self, *args, **options): |
| 39 | + org_slug = options.get("org_slug") |
| 40 | + dry_run = options["dry_run"] |
| 41 | + |
| 42 | + if org_slug: |
| 43 | + orgs = Org.objects.filter(slug=org_slug) |
| 44 | + if not orgs.exists(): |
| 45 | + self.stdout.write(self.style.ERROR(f"Organization '{org_slug}' not found")) |
| 46 | + return |
| 47 | + else: |
| 48 | + orgs = Org.objects.all() |
| 49 | + |
| 50 | + total_updated = 0 |
| 51 | + total_skipped = 0 |
| 52 | + total_errors = 0 |
| 53 | + |
| 54 | + for org in orgs: |
| 55 | + updated, skipped, errors = self.process_org(org, dry_run) |
| 56 | + total_updated += updated |
| 57 | + total_skipped += skipped |
| 58 | + total_errors += errors |
| 59 | + |
| 60 | + self.stdout.write(f"\n{'[DRY RUN] ' if dry_run else ''}Summary:") |
| 61 | + self.stdout.write(f" Pipelines updated: {total_updated}") |
| 62 | + self.stdout.write(f" Pipelines skipped (no transform tasks): {total_skipped}") |
| 63 | + self.stdout.write(f" Errors: {total_errors}") |
| 64 | + |
| 65 | + def process_org(self, org: Org, dry_run: bool): |
| 66 | + """Process all orchestrate pipelines for an organization""" |
| 67 | + dataflows = OrgDataFlowv1.objects.filter(org=org, dataflow_type="orchestrate") |
| 68 | + |
| 69 | + if not dataflows.exists(): |
| 70 | + return 0, 0, 0 |
| 71 | + |
| 72 | + self.stdout.write(f"\nOrg: {org.slug} ({org.name})") |
| 73 | + |
| 74 | + updated = 0 |
| 75 | + skipped = 0 |
| 76 | + errors = 0 |
| 77 | + |
| 78 | + for dataflow in dataflows: |
| 79 | + # Check if this pipeline has transform tasks |
| 80 | + has_transform = DataflowOrgTask.objects.filter( |
| 81 | + dataflow=dataflow, |
| 82 | + orgtask__task__type=TaskType.DBT, |
| 83 | + ).exists() |
| 84 | + |
| 85 | + if not has_transform: |
| 86 | + self.stdout.write(f" → Skipping {dataflow.deployment_name} (no transform tasks)") |
| 87 | + skipped += 1 |
| 88 | + continue |
| 89 | + |
| 90 | + # Check if dbt-clean and dbt-deps are already present |
| 91 | + has_dbt_clean = DataflowOrgTask.objects.filter( |
| 92 | + dataflow=dataflow, orgtask__task__slug=TASK_DBTCLEAN |
| 93 | + ).exists() |
| 94 | + has_dbt_deps = DataflowOrgTask.objects.filter( |
| 95 | + dataflow=dataflow, orgtask__task__slug=TASK_DBTDEPS |
| 96 | + ).exists() |
| 97 | + |
| 98 | + if has_dbt_clean and has_dbt_deps: |
| 99 | + self.stdout.write( |
| 100 | + f" → Skipping {dataflow.deployment_name} (already has dbt-clean and dbt-deps)" |
| 101 | + ) |
| 102 | + skipped += 1 |
| 103 | + continue |
| 104 | + |
| 105 | + missing = [] |
| 106 | + if not has_dbt_clean: |
| 107 | + missing.append("dbt-clean") |
| 108 | + if not has_dbt_deps: |
| 109 | + missing.append("dbt-deps") |
| 110 | + |
| 111 | + if dry_run: |
| 112 | + self.stdout.write( |
| 113 | + f" [DRY RUN] Would update {dataflow.deployment_name} " |
| 114 | + f"(missing: {', '.join(missing)})" |
| 115 | + ) |
| 116 | + updated += 1 |
| 117 | + continue |
| 118 | + |
| 119 | + try: |
| 120 | + self.update_pipeline(org, dataflow) |
| 121 | + self.stdout.write( |
| 122 | + self.style.SUCCESS( |
| 123 | + f" ✓ Updated {dataflow.deployment_name} (added: {', '.join(missing)})" |
| 124 | + ) |
| 125 | + ) |
| 126 | + updated += 1 |
| 127 | + except Exception as e: |
| 128 | + self.stdout.write( |
| 129 | + self.style.ERROR(f" ✗ Failed to update {dataflow.deployment_name}: {str(e)}") |
| 130 | + ) |
| 131 | + logger.error( |
| 132 | + f"Failed to backfill auto-managed tasks for {dataflow.deployment_name}: {str(e)}" |
| 133 | + ) |
| 134 | + errors += 1 |
| 135 | + |
| 136 | + return updated, skipped, errors |
| 137 | + |
| 138 | + def update_pipeline(self, org: Org, dataflow: OrgDataFlowv1): |
| 139 | + """Re-run update_pipeline to backfill auto-managed tasks""" |
| 140 | + pipeline_details = PipelineService.get_pipeline_details(org, dataflow.deployment_id) |
| 141 | + |
| 142 | + transform_tasks = pipeline_details.get("transformTasks", []) |
| 143 | + |
| 144 | + # Convert UUIDs to strings for Pydantic validation |
| 145 | + transform_tasks_str = [ |
| 146 | + {"uuid": str(task["uuid"]), "seq": task["seq"]} for task in transform_tasks |
| 147 | + ] |
| 148 | + |
| 149 | + update_payload = PrefectDataFlowUpdateSchema3( |
| 150 | + name=pipeline_details["name"], |
| 151 | + cron=pipeline_details["cron"], |
| 152 | + connections=pipeline_details["connections"], |
| 153 | + transformTasks=transform_tasks_str, |
| 154 | + ) |
| 155 | + |
| 156 | + PipelineService.update_pipeline(org, dataflow.deployment_id, update_payload) |
| 157 | + |
| 158 | + # Toggle schedule inactive → active to clear pre-scheduled runs. |
| 159 | + # Prefect schedules runs 1-2 days in advance; those won't pick up the |
| 160 | + # updated deployment params unless the schedule is reset. |
| 161 | + # Only do this for pipelines that have an active schedule. |
| 162 | + if dataflow.cron and pipeline_details.get("isScheduleActive", False): |
| 163 | + PipelineService.set_pipeline_schedule(org, dataflow.deployment_id, "inactive") |
| 164 | + PipelineService.set_pipeline_schedule(org, dataflow.deployment_id, "active") |
0 commit comments