From a738de43094c4899f3d840301d3512884f4e8655 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Mon, 11 Sep 2017 13:58:29 +0200
Subject: [PATCH] db changes for subject default flying team location

---
 .../migrations/0046_subject_flying_team.py    | 21 ++++++++++++++++++
 ...047_subject_flying_team_from_annotation.py | 22 +++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100644 smash/web/migrations/0046_subject_flying_team.py
 create mode 100644 smash/web/migrations/0047_subject_flying_team_from_annotation.py

diff --git a/smash/web/migrations/0046_subject_flying_team.py b/smash/web/migrations/0046_subject_flying_team.py
new file mode 100644
index 00000000..3101c686
--- /dev/null
+++ b/smash/web/migrations/0046_subject_flying_team.py
@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.10.7 on 2017-09-11 10:07
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('web', '0045_holiday_info'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='subject',
+            name='flying_team',
+            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='web.FlyingTeam', verbose_name=b'Flying team (if applicable)'),
+        ),
+    ]
diff --git a/smash/web/migrations/0047_subject_flying_team_from_annotation.py b/smash/web/migrations/0047_subject_flying_team_from_annotation.py
new file mode 100644
index 00000000..7b514e53
--- /dev/null
+++ b/smash/web/migrations/0047_subject_flying_team_from_annotation.py
@@ -0,0 +1,22 @@
+# -*- coding: utf-8 -*-
+# assigns default flying team location for subjects that had in the past finished appointment at flying team location
+
+from __future__ import unicode_literals
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+    dependencies = [
+        ('web', '0046_subject_flying_team'),
+    ]
+
+    operations = [
+        migrations.RunSQL(
+            "update web_subject set flying_team_id = web_appointment.flying_team_id from web_visit, web_appointment " +
+            "where web_subject.id=web_visit.subject_id and " +
+            "web_subject.flying_team_id is null and " +
+            "web_visit.id = web_appointment.visit_id and " +
+            "web_appointment.flying_team_id is not null and status = 'FINISHED';"
+        ),
+    ]
-- 
GitLab