diff --git a/ccdb/experiments/migrations/0010_DATA_collections_experiments.py b/ccdb/experiments/migrations/0010_DATA_collections_experiments.py new file mode 100644 index 0000000..4118eb5 --- /dev/null +++ b/ccdb/experiments/migrations/0010_DATA_collections_experiments.py @@ -0,0 +1,38 @@ +from django.db import migrations + +from ccdb.utils.data import get_data_sources + + +class Migration(migrations.Migration): + def migrate(apps, schema_editor): + sources = get_data_sources() + if not sources: + return + + c = sources['db0'] + + Collection = apps.get_model('collections_ccdb', 'Collection') + Experiment = apps.get_model('experiments', 'Experiment') + + for experiment in Experiment.objects.all(): + experiment.collections.all().delete() + + for r in c.execute('SELECT * FROM tbl_hash_collection_experiments;'): + c = Collection.objects.get(id=r[0]) + e = Experiment.objects.get(id=r[1]) + e.collections.add(c) + e.save() + + def rollback(apps, schema_editor): + Experiment = apps.get_model('experiments', 'Experiment') + + for experiment in Experiment.objects.all(): + experiment.collections.all().delete() + + dependencies = [ + ('experiments', '0009_DATA_initial'), + ] + + operations = [ + migrations.RunPython(migrate, rollback), + ]