Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-27 07:41:44

0001 """
0002 Rename EMI to PCS: rename database tables and migrate PersistentState keys.
0003 
0004 PRE-REQUISITE: The deploy script updates django_migrations and django_content_type
0005 to change app='emi' to app='pcs' BEFORE this migration runs. Without that step,
0006 Django won't know 0001/0002 are already applied and will fail.
0007 """
0008 from django.db import migrations
0009 
0010 
0011 def migrate_persistent_state_keys(apps, schema_editor):
0012     """Rename emi_* keys to pcs_* in PersistentState."""
0013     PersistentState = apps.get_model('monitor_app', 'PersistentState')
0014     try:
0015         ps = PersistentState.objects.get(id=1)
0016     except PersistentState.DoesNotExist:
0017         return
0018 
0019     changed = False
0020     new_data = {}
0021     for key, value in ps.state_data.items():
0022         if key.startswith('emi_'):
0023             new_key = 'pcs_' + key[4:]
0024             new_data[new_key] = value
0025             changed = True
0026         else:
0027             new_data[key] = value
0028 
0029     if changed:
0030         ps.state_data = new_data
0031         ps.save(update_fields=['state_data'])
0032 
0033 
0034 def reverse_persistent_state_keys(apps, schema_editor):
0035     """Reverse: rename pcs_* keys back to emi_*."""
0036     PersistentState = apps.get_model('monitor_app', 'PersistentState')
0037     try:
0038         ps = PersistentState.objects.get(id=1)
0039     except PersistentState.DoesNotExist:
0040         return
0041 
0042     changed = False
0043     new_data = {}
0044     for key, value in ps.state_data.items():
0045         if key.startswith('pcs_'):
0046             new_key = 'emi_' + key[4:]
0047             new_data[new_key] = value
0048             changed = True
0049         else:
0050             new_data[key] = value
0051 
0052     if changed:
0053         ps.state_data = new_data
0054         ps.save(update_fields=['state_data'])
0055 
0056 
0057 class Migration(migrations.Migration):
0058 
0059     dependencies = [
0060         ('pcs', '0002_prodconfig'),
0061         ('monitor_app', '0001_initial'),
0062     ]
0063 
0064     operations = [
0065         # Rename all emi_* tables to pcs_*
0066         migrations.RunSQL(
0067             sql="ALTER TABLE IF EXISTS emi_physics_category RENAME TO pcs_physics_category;",
0068             reverse_sql="ALTER TABLE IF EXISTS pcs_physics_category RENAME TO emi_physics_category;",
0069         ),
0070         migrations.RunSQL(
0071             sql="ALTER TABLE IF EXISTS emi_physics_tag RENAME TO pcs_physics_tag;",
0072             reverse_sql="ALTER TABLE IF EXISTS pcs_physics_tag RENAME TO emi_physics_tag;",
0073         ),
0074         migrations.RunSQL(
0075             sql="ALTER TABLE IF EXISTS emi_evgen_tag RENAME TO pcs_evgen_tag;",
0076             reverse_sql="ALTER TABLE IF EXISTS pcs_evgen_tag RENAME TO emi_evgen_tag;",
0077         ),
0078         migrations.RunSQL(
0079             sql="ALTER TABLE IF EXISTS emi_simu_tag RENAME TO pcs_simu_tag;",
0080             reverse_sql="ALTER TABLE IF EXISTS pcs_simu_tag RENAME TO emi_simu_tag;",
0081         ),
0082         migrations.RunSQL(
0083             sql="ALTER TABLE IF EXISTS emi_reco_tag RENAME TO pcs_reco_tag;",
0084             reverse_sql="ALTER TABLE IF EXISTS pcs_reco_tag RENAME TO emi_reco_tag;",
0085         ),
0086         migrations.RunSQL(
0087             sql="ALTER TABLE IF EXISTS emi_dataset RENAME TO pcs_dataset;",
0088             reverse_sql="ALTER TABLE IF EXISTS pcs_dataset RENAME TO emi_dataset;",
0089         ),
0090         migrations.RunSQL(
0091             sql="ALTER TABLE IF EXISTS emi_prod_config RENAME TO pcs_prod_config;",
0092             reverse_sql="ALTER TABLE IF EXISTS pcs_prod_config RENAME TO emi_prod_config;",
0093         ),
0094 
0095         # Migrate PersistentState keys (emi_next_* → pcs_next_*, emi_param_defs_* → pcs_param_defs_*)
0096         migrations.RunPython(
0097             migrate_persistent_state_keys,
0098             reverse_persistent_state_keys,
0099         ),
0100     ]