-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an ontology class for volumes, create class instances for all existing volumes, and relate volume class instances to volume rows via a `volume_class_instance` table. Also move the `model_of` relation from the needed classes for the tracing tool to the default project needed classes. Written with @tomka, @clbarnes, and @Willp24. See #1765.
- Loading branch information
1 parent
a51b8b1
commit 1a61198
Showing
4 changed files
with
147 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
django/applications/catmaid/migrations/0050_create_volume_class_and_instances.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.14 on 2018-07-20 14:27 | ||
from __future__ import unicode_literals | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import django.utils.timezone | ||
|
||
from catmaid.apps import get_system_user | ||
|
||
|
||
def forwards(apps, schema_editor): | ||
"""Make sure all required class and relations are existing for all | ||
projects. We can't use the regular model classes, but have to get | ||
them through the migration system. | ||
""" | ||
from catmaid.control.project import validate_project_setup | ||
|
||
Class = apps.get_model('catmaid', 'Class') | ||
Project = apps.get_model('catmaid', 'Project') | ||
Relation = apps.get_model('catmaid', 'Relation') | ||
User = apps.get_model('auth', 'User') | ||
ClientDatastore = apps.get_model('catmaid', 'ClientDatastore') | ||
|
||
projects = Project.objects.all() | ||
# If there are no projects, don't continue, because there is nothing to | ||
# migrate. | ||
if 0 == len(projects) or 0 == User.objects.count(): | ||
return | ||
|
||
system_user = get_system_user(User) | ||
for p in projects: | ||
validate_project_setup(p.id, system_user.id, True, Class, Relation, ClientDatastore) | ||
|
||
forward_create_relations = """ | ||
CREATE TABLE volume_class_instance ( | ||
volume_id bigint NOT NULL, | ||
class_instance_id integer NOT NULL | ||
) | ||
INHERITS (relation_instance); | ||
-- Volume Class Instance constraints | ||
ALTER TABLE ONLY volume_class_instance | ||
ADD CONSTRAINT volume_class_instance_pkey PRIMARY KEY (id); | ||
ALTER TABLE ONLY volume_class_instance | ||
ADD CONSTRAINT volume_class_instance_sa_id | ||
FOREIGN KEY (volume_id) | ||
REFERENCES catmaid_volume(id) DEFERRABLE INITIALLY DEFERRED; | ||
ALTER TABLE ONLY volume_class_instance | ||
ADD CONSTRAINT volume_class_instance_id_fkey | ||
FOREIGN KEY (class_instance_id) | ||
REFERENCES class_instance(id) DEFERRABLE INITIALLY DEFERRED; | ||
WITH new_cis AS ( | ||
INSERT INTO class_instance (user_id, project_id, name, class_id) | ||
SELECT | ||
v.user_id, | ||
v.project_id, | ||
v.name, | ||
c.id | ||
FROM catmaid_volume v | ||
JOIN class c ON (c.project_id = v.project_id AND c.class_name = 'volume') | ||
RETURNING id, user_id, project_id, name | ||
) | ||
INSERT INTO volume_class_instance (user_id, project_id, relation_id, volume_id, class_instance_id) | ||
SELECT | ||
v.user_id, | ||
v.project_id, | ||
r.id, | ||
v.id, | ||
ci.id | ||
FROM catmaid_volume v | ||
JOIN relation r ON (r.project_id = v.project_id AND r.relation_name = 'model_of') | ||
JOIN new_cis ci ON ( | ||
ci.user_id = v.user_id AND | ||
ci.project_id = v.project_id AND | ||
ci.name = v.name | ||
); | ||
-- Create history tables | ||
SELECT create_history_table('volume_class_instance'::regclass, 'edition_time', 'txid'); | ||
""" | ||
|
||
backward_create_relations = """ | ||
SELECT disable_history_tracking_for_table('volume_class_instance'::regclass, | ||
get_history_table_name('volume_class_instance'::regclass)); | ||
SELECT drop_history_table('volume_class_instance'::regclass); | ||
DROP TABLE volume_class_instance; | ||
DELETE FROM class_instance | ||
USING class c | ||
WHERE class_instance.class_id = c.id | ||
AND c.class_name = 'volume'; | ||
""" | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('catmaid', '0049_volume_tin_representation'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(forwards, migrations.RunPython.noop), | ||
migrations.RunSQL( | ||
forward_create_relations, | ||
backward_create_relations, | ||
[ | ||
migrations.CreateModel( | ||
name='VolumeClassInstance', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('creation_time', models.DateTimeField(default=django.utils.timezone.now)), | ||
('edition_time', models.DateTimeField(default=django.utils.timezone.now)), | ||
('class_instance', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='catmaid.ClassInstance')), | ||
('project', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='catmaid.Project')), | ||
('relation', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='catmaid.Relation')), | ||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), | ||
('volume', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='catmaid.Volume')), | ||
], | ||
options={ | ||
'db_table': 'volume_class_instance', | ||
}, | ||
), | ||
] | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters