from django.db import migrations def copy_inspections(apps, schema_editor): connection = schema_editor.connection with connection.cursor() as cursor: table_names = connection.introspection.table_names(cursor) if 'assets_assetinspection' in table_names: cursor.execute(""" INSERT INTO inspections_inspection ( id, asset_object_id, nature, inspector_role, inspection_date, result_status, verified_obsolescence, notes, apply_changes_to_asset, created_at, updated_at, asset_content_type_id, inspector_id, mission_intervention_id, checked_intervention_id ) SELECT id, asset_object_id, nature, inspector_role, inspection_date, result_status, verified_obsolescence, notes, apply_changes_to_asset, created_at, updated_at, asset_content_type_id, inspector_id, NULL, NULL FROM assets_assetinspection ON CONFLICT (id) DO NOTHING; """) if connection.vendor == 'postgresql': cursor.execute(""" SELECT setval(pg_get_serial_sequence('inspections_inspection', 'id'), COALESCE(MAX(id), 1)) FROM inspections_inspection; """) if 'assets_parkingspotinspection' in table_names: cursor.execute(""" INSERT INTO inspections_parkingspotinspection ( inspection_ptr_id, verified_standard_stand_count, verified_cargo_stand_count, verified_stand_type, verified_has_scooter_drop_zone, verified_marking_quality, apply_physical_counts_to_asset ) SELECT assetinspection_ptr_id, verified_standard_stand_count, verified_cargo_stand_count, verified_stand_type, verified_has_scooter_drop_zone, verified_marking_quality, apply_physical_counts_to_asset FROM assets_parkingspotinspection ON CONFLICT (inspection_ptr_id) DO NOTHING; """) if 'assets_inspectiondocument' in table_names: cursor.execute(""" INSERT INTO inspections_inspectiondocument ( id, file, created_at, inspection_id ) SELECT id, file, created_at, inspection_id FROM assets_inspectiondocument ON CONFLICT (id) DO NOTHING; """) if connection.vendor == 'postgresql': cursor.execute(""" SELECT setval(pg_get_serial_sequence('inspections_inspectiondocument', 'id'), COALESCE(MAX(id), 1)) FROM inspections_inspectiondocument; """) def reverse_copy_inspections(apps, schema_editor): pass class Migration(migrations.Migration): dependencies = [ ('inspections', '0001_initial'), ] operations = [ migrations.RunPython(copy_inspections, reverse_copy_inspections), ]