From dbfe88610aa642a953434c7cbc7ec538a62d0204 Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Wed, 15 Jun 2016 10:22:39 -0700 Subject: [PATCH] Experiment-Collections --- .../0010_DATA_collections_experiments.py | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 ccdb/experiments/migrations/0010_DATA_collections_experiments.py 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), + ]