From af6e1a5bcd0f324f7c5423cbbdc4252da8bf0147 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Thu, 22 Oct 2020 09:28:15 +0200
Subject: [PATCH] migration supports sqlite

---
 smash/web/migration_functions.py              |  8 +++
 .../0030_subject_information_sent.py          | 27 +++++---
 .../0039_pd_family_default_unknown.py         | 18 ++++--
 ...047_subject_flying_team_from_annotation.py | 32 +++++++---
 ...update_visit_number_for_existing_visits.py | 21 +++++--
 .../migrations/0060_remove_subject_country.py | 16 +++--
 smash/web/migrations/0066_subject.py          | 30 +++------
 smash/web/migrations/0067_subject.py          | 16 +++--
 .../web/migrations/0079_auto_20171204_1235.py | 55 ++++++++++------
 .../web/migrations/0080_auto_20171204_1341.py | 14 +++--
 ...1_studysubjectlist_last_contact_attempt.py | 47 +++++++++-----
 .../web/migrations/0085_auto_20171205_1650.py | 35 ++++++++---
 .../migrations/0086_unfinished_visit_list.py  | 36 ++++++++---
 ...proaching_visit_wihout_appointment_list.py | 37 ++++++++---
 .../0104_contact_required_list_update.py      | 18 ++++--
 ...105_unfinished_appointments_list_update.py | 21 +++++--
 .../0106_approaching_post_mail_list_update.py | 37 ++++++++---
 .../0107_exceeded_visit_time_list_update.py   | 21 +++++--
 .../0108_unfinished_visit_list_update.py      | 21 +++++--
 .../0109_missing_appointment_list_update.py   | 62 +++++++++++++------
 .../web/migrations/0140_auto_20190528_0953.py | 43 ++++++++-----
 21 files changed, 430 insertions(+), 185 deletions(-)
 create mode 100644 smash/web/migration_functions.py

diff --git a/smash/web/migration_functions.py b/smash/web/migration_functions.py
new file mode 100644
index 00000000..fe2d0d65
--- /dev/null
+++ b/smash/web/migration_functions.py
@@ -0,0 +1,8 @@
+from django.conf import settings
+
+db_name = 'default'
+db_backend = settings.DATABASES[db_name]['ENGINE'].split('.')[-1]
+
+
+def is_sqlite_db():
+    return db_backend == 'sqlite3'
diff --git a/smash/web/migrations/0030_subject_information_sent.py b/smash/web/migrations/0030_subject_information_sent.py
index 6bef7fd3..2225e278 100644
--- a/smash/web/migrations/0030_subject_information_sent.py
+++ b/smash/web/migrations/0030_subject_information_sent.py
@@ -2,7 +2,9 @@
 # Generated by Django 1.10.3 on 2017-04-04 14:16
 
 
-from django.db import migrations, models
+from django.db import migrations
+
+from ..migration_functions import is_sqlite_db
 
 
 class Migration(migrations.Migration):
@@ -10,10 +12,19 @@ class Migration(migrations.Migration):
         ('web', '0029_auto_20170404_1616'),
     ]
 
-    operations = [
-        migrations.RunSQL(
-            "update web_subject set information_sent=true where id in" +
-            "(select web_subject.id from web_appointment  " +
-            " left join web_visit on visit_id = web_visit.id " +
-            " left join web_subject on web_visit.subject_id = web_subject.id where status = 'FINISHED');"),
-    ]
+    if is_sqlite_db():
+        operations = [
+            migrations.RunSQL(
+                "update web_subject set information_sent=1 where id in" +
+                "(select web_subject.id from web_appointment  " +
+                " left join web_visit on visit_id = web_visit.id " +
+                " left join web_subject on web_visit.subject_id = web_subject.id where status = 'FINISHED');"),
+        ]
+    else:
+        operations = [
+            migrations.RunSQL(
+                "update web_subject set information_sent=true where id in" +
+                "(select web_subject.id from web_appointment  " +
+                " left join web_visit on visit_id = web_visit.id " +
+                " left join web_subject on web_visit.subject_id = web_subject.id where status = 'FINISHED');"),
+        ]
diff --git a/smash/web/migrations/0039_pd_family_default_unknown.py b/smash/web/migrations/0039_pd_family_default_unknown.py
index 7c718a07..d8f27658 100644
--- a/smash/web/migrations/0039_pd_family_default_unknown.py
+++ b/smash/web/migrations/0039_pd_family_default_unknown.py
@@ -4,6 +4,8 @@
 
 from django.db import migrations, models
 
+from ..migration_functions import is_sqlite_db
+
 
 class Migration(migrations.Migration):
     dependencies = [
@@ -15,8 +17,14 @@ class Migration(migrations.Migration):
             model_name='subject',
             name='pd_in_family',
             field=models.NullBooleanField(default=None, verbose_name=b'PD in family'),
-        ),
-        migrations.RunSQL(
-            "UPDATE web_subject SET pd_in_family=NULL WHERE pd_in_family = FALSE;",
-            reverse_sql="UPDATE web_subject SET pd_in_family=FALSE WHERE pd_in_family = NULL;")
-    ]
+        )]
+    if is_sqlite_db():
+        operations.append(
+            migrations.RunSQL(
+                "UPDATE web_subject SET pd_in_family=NULL WHERE pd_in_family = 0;",
+                reverse_sql="UPDATE web_subject SET pd_in_family=0 WHERE pd_in_family = NULL;"))
+    else:
+        operations.append(
+            migrations.RunSQL(
+                "UPDATE web_subject SET pd_in_family=NULL WHERE pd_in_family = FALSE;",
+                reverse_sql="UPDATE web_subject SET pd_in_family=FALSE WHERE pd_in_family = NULL;"))
diff --git a/smash/web/migrations/0047_subject_flying_team_from_annotation.py b/smash/web/migrations/0047_subject_flying_team_from_annotation.py
index 375fcb89..4747e562 100644
--- a/smash/web/migrations/0047_subject_flying_team_from_annotation.py
+++ b/smash/web/migrations/0047_subject_flying_team_from_annotation.py
@@ -2,21 +2,33 @@
 # assigns default flying team location for subjects that had in the past finished appointment at flying team location
 
 
-
 from django.db import migrations
 
+from ..migration_functions import is_sqlite_db
+
 
 class Migration(migrations.Migration):
     dependencies = [
         ('web', '0046_subject_flying_team'),
     ]
+    if is_sqlite_db():
+        operations = [
+            migrations.RunSQL(
+                "update web_subject set flying_team_id = (select flying_team_id from web_visit, web_appointment " +
+                "where web_subject.id=web_visit.subject_id and " +
+                "web_visit.id = web_appointment.visit_id and " +
+                "web_appointment.flying_team_id is not null and status = 'FINISHED') "
+                "where web_subject.flying_team_id is null"
 
-    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';"
-        ),
-    ]
+            ),
+        ]
+    else:
+        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';"
+            ),
+        ]
diff --git a/smash/web/migrations/0057_update_visit_number_for_existing_visits.py b/smash/web/migrations/0057_update_visit_number_for_existing_visits.py
index 53f8c82d..b9fbe5d1 100644
--- a/smash/web/migrations/0057_update_visit_number_for_existing_visits.py
+++ b/smash/web/migrations/0057_update_visit_number_for_existing_visits.py
@@ -2,7 +2,9 @@
 # Generated by Django 1.10.7 on 2017-10-27 10:22
 
 
-from django.db import migrations, models
+from django.db import migrations
+
+from ..migration_functions import is_sqlite_db
 
 
 class Migration(migrations.Migration):
@@ -10,8 +12,15 @@ class Migration(migrations.Migration):
         ('web', '0056_visit_visit_number'),
     ]
 
-    operations = [
-        migrations.RunSQL(
-            "update web_visit s set visit_number= (select count(*) from web_visit t " +
-            "where t.subject_id=s.subject_id and t.datetime_begin<= s.datetime_begin);")
-    ]
+    if is_sqlite_db():
+        operations = [
+            migrations.RunSQL(
+                "update web_visit set visit_number= (select count(*) from web_visit t " +
+                "where t.subject_id=subject_id and t.datetime_begin<= datetime_begin);")
+        ]
+    else:
+        operations = [
+            migrations.RunSQL(
+                "update web_visit s set visit_number= (select count(*) from web_visit t " +
+                "where t.subject_id=s.subject_id and t.datetime_begin<= s.datetime_begin);")
+        ]
diff --git a/smash/web/migrations/0060_remove_subject_country.py b/smash/web/migrations/0060_remove_subject_country.py
index 87262905..7ec9f237 100644
--- a/smash/web/migrations/0060_remove_subject_country.py
+++ b/smash/web/migrations/0060_remove_subject_country.py
@@ -4,6 +4,8 @@
 
 from django.db import migrations, models
 
+from ..migration_functions import is_sqlite_db
+
 
 class Migration(migrations.Migration):
     dependencies = [
@@ -163,7 +165,8 @@ class Migration(migrations.Migration):
         migrations.RunSQL('insert into web_country (name, "order") values(\'Rwanda\', 244);'),
         migrations.RunSQL('insert into web_country (name, "order") values(\'St Kitts & Nevis\', 245);'),
         migrations.RunSQL('insert into web_country (name, "order") values(\'St Lucia\', 246);'),
-        migrations.RunSQL('insert into web_country (name, "order") values(\'Saint Vincent & the Grenadines\', 247);'),
+        migrations.RunSQL(
+            'insert into web_country (name, "order") values(\'Saint Vincent & the Grenadines\', 247);'),
         migrations.RunSQL('insert into web_country (name, "order") values(\'Samoa\', 248);'),
         migrations.RunSQL('insert into web_country (name, "order") values(\'San Marino\', 249);'),
         migrations.RunSQL('insert into web_country (name, "order") values(\'Sao Tome & Principe\', 250);'),
@@ -213,7 +216,12 @@ class Migration(migrations.Migration):
         migrations.RunSQL('insert into web_country (name, "order") values(\'Zambia\', 294);'),
         migrations.RunSQL('insert into web_country (name, "order") values(\'Zimbabwe\', 295);'),
 
-        migrations.RunSQL('update web_subject t set country_2_id = '
-                          'case when (select id from web_country u where t.country=u.name) is Null then 1 '
-                          'else (select id from web_country u where t.country=u.name) end;'),
     ]
+    if is_sqlite_db():
+        operations.append(migrations.RunSQL('update web_subject set country_2_id = '
+                                            'case when (select id from web_country u where country=u.name) is Null then 1 '
+                                            'else (select id from web_country u where country=u.name) end;'))
+    else:
+        operations.append(migrations.RunSQL('update web_subject t set country_2_id = '
+                                            'case when (select id from web_country u where t.country=u.name) is Null then 1 '
+                                            'else (select id from web_country u where t.country=u.name) end;'))
diff --git a/smash/web/migrations/0066_subject.py b/smash/web/migrations/0066_subject.py
index 3b3fa937..a0d3357d 100644
--- a/smash/web/migrations/0066_subject.py
+++ b/smash/web/migrations/0066_subject.py
@@ -2,9 +2,10 @@
 # Generated by Django 1.10.7 on 2017-11-27 10:42
 
 
-from django.db import migrations, models
-import django.db.models.deletion
-from django import VERSION as DJANGO_VERSION
+from django.db import migrations
+
+from ..migration_functions import is_sqlite_db
+
 
 class Migration(migrations.Migration):
 
@@ -12,26 +13,7 @@ class Migration(migrations.Migration):
         ('web', '0065_auto_20171127_0957'),
     ]
 
-    if DJANGO_VERSION[:2] < (1, 11):
-        operations = [
-            migrations.RunSQL('alter sequence web_subject_id_seq rename to web_studysubject_id_seq;'),
-            migrations.RunSQL('alter sequence web_subject_languages_id_seq rename to web_studysubject_languages_id_seq;'),
-            migrations.RunSQL('alter table web_studysubject rename constraint web_subject_flying_team_id_ef78de5d_fk_web_flyingteam_id to web_studysubject_flying_team_id_ef78de5d_fk_web_flyingteam_id;'),
-            migrations.RunSQL('alter table web_studysubject_languages rename constraint web_subject_languages_language_id_80eca4c1_fk_web_language_id to web_studysubject_languages_language_id_80eca4c1_fk_web_language_id;'),
-            migrations.RunSQL('alter table web_studysubject rename constraint web_subject_country_id_5d976875_fk_web_country_id to web_studysubject_country_id_5d976875_fk_web_country_id;'),
-            migrations.RunSQL('alter table web_studysubject rename constraint web_subject_default_location_id_da83b714_fk_web_location_id to web_studyubject_default_location_id_da83b714_fk_web_location_id;'),
-            migrations.RunSQL('alter table web_studysubject rename constraint web_subject_pkey to web_studysubject_pkey;'),
-            migrations.RunSQL('alter table web_studysubject_languages rename constraint web_subject_languages_pkey to web_studysubject_languages_pkey;'),
-            migrations.RunSQL('alter table web_studysubject_languages rename constraint web_subject_languages_subject_id_a5eb4f49_uniq to web_studysubject_languages_subject_id_a5eb4f49_uniq;'),
-            migrations.RunSQL('alter index if exists web_subject_15d1b6cd rename to web_studysubject_15d1b6cd;'),
-            migrations.RunSQL('alter index if exists web_subject_496f9482 rename to web_studysubject_496f9482;'),
-            migrations.RunSQL('alter index if exists web_subject_84b500ff rename to web_studysubject_84b500ff;'),
-            migrations.RunSQL('alter index if exists web_subject_9571fdf9 rename to web_studysubject_9571fdf9;'),
-            migrations.RunSQL('alter index if exists web_subject_languages_468679bd rename to web_studysubject_languages_468679bd;'),
-            migrations.RunSQL('alter index if exists web_subject_languages_ffaba1d1 rename to web_studysubject_languages_ffaba1d1;'),
-            migrations.RunSQL('alter index if exists web_subject_screening_number_75aa70c7_like rename to web_studysubject_screening_number_75aa70c7_like;')
-        ]
-    else:
+    if not is_sqlite_db():
         operations = [
             migrations.RunSQL('alter sequence web_subject_id_seq rename to web_studysubject_id_seq;'),
             migrations.RunSQL('alter sequence web_subject_languages_id_seq rename to web_studysubject_languages_id_seq;'),
@@ -50,3 +32,5 @@ class Migration(migrations.Migration):
             migrations.RunSQL('alter index if exists web_subject_languages_ffaba1d1 rename to web_studysubject_languages_ffaba1d1;'),
             migrations.RunSQL('alter index if exists web_subject_screening_number_75aa70c7_like rename to web_studysubject_screening_number_75aa70c7_like;')
         ]
+    else:
+        operations = []
diff --git a/smash/web/migrations/0067_subject.py b/smash/web/migrations/0067_subject.py
index 42d8161b..890b3572 100644
--- a/smash/web/migrations/0067_subject.py
+++ b/smash/web/migrations/0067_subject.py
@@ -4,6 +4,7 @@
 
 import django.db.models.deletion
 from django.db import migrations, models
+from ..migration_functions import is_sqlite_db
 
 
 class Migration(migrations.Migration):
@@ -11,10 +12,17 @@ class Migration(migrations.Migration):
         ('web', '0066_subject'),
     ]
 
-    operations = [
-        migrations.RunSQL('alter index if exists web_subject_default_written_communication_language_id_0bce1d22 rename to web_studysubject_default_written_communication_language_id_0bce1d22;'),
-        migrations.RunSQL('alter index if exists web_subject_languages_subject_id_2cf93044 rename to web_studysubject_languages_subject_id_2cf93044;'),
-        migrations.RunSQL('alter index if exists web_subject_languages_language_id_80eca4c1 rename to web_studysubject_languages_language_id_80eca4c1;'),
+    operations = []
+    if not is_sqlite_db():
+        operations = operations + [
+            migrations.RunSQL(
+                'alter index if exists web_subject_default_written_communication_language_id_0bce1d22 rename to web_studysubject_default_written_communication_language_id_0bce1d22;'),
+            migrations.RunSQL(
+                'alter index if exists web_subject_languages_subject_id_2cf93044 rename to web_studysubject_languages_subject_id_2cf93044;'),
+            migrations.RunSQL(
+                'alter index if exists web_subject_languages_language_id_80eca4c1 rename to web_studysubject_languages_language_id_80eca4c1;'),
+        ]
+    operations = operations + [
         migrations.CreateModel(
             name='Subject',
             fields=[
diff --git a/smash/web/migrations/0079_auto_20171204_1235.py b/smash/web/migrations/0079_auto_20171204_1235.py
index a02da51c..e8f0588c 100644
--- a/smash/web/migrations/0079_auto_20171204_1235.py
+++ b/smash/web/migrations/0079_auto_20171204_1235.py
@@ -5,6 +5,8 @@
 import django.db.models.deletion
 from django.db import migrations, models
 
+from ..migration_functions import is_sqlite_db
+
 
 class Migration(migrations.Migration):
     dependencies = [
@@ -34,23 +36,36 @@ class Migration(migrations.Migration):
                 ('unfinished_appointments_visible',
                  models.BooleanField(default=True, verbose_name=b'unfinished appointments')),
             ],
-        ),
-        migrations.RunSQL('insert into web_studynotificationparameters (exceeded_visits_visible,' +
-                          'missing_redcap_subject_visible,' +
-                          'inconsistent_redcap_subject_visible,' +
-                          'subject_require_contact_visible,' +
-                          'subject_no_visits_visible,' +
-                          'unfinished_visits_visible,' +
-                          'visits_with_missing_appointments_visible,' +
-                          'approaching_visits_without_appointments_visible,' +
-                          'approaching_visits_for_mail_contact_visible,' +
-                          'unfinished_appointments_visible' +
-                          ') values(true, true, true, true, true, true, true, true, true, true)'),
-        migrations.AddField(
-            model_name='study',
-            name='notification_parameters',
-            field=models.OneToOneField(default=1, on_delete=django.db.models.deletion.CASCADE,
-                                       to='web.StudyNotificationParameters'),
-            preserve_default=False,
-        ),
-    ]
+        )]
+
+    if not is_sqlite_db():
+        operations.append(migrations.RunSQL('insert into web_studynotificationparameters (exceeded_visits_visible,' +
+                                            'missing_redcap_subject_visible,' +
+                                            'inconsistent_redcap_subject_visible,' +
+                                            'subject_require_contact_visible,' +
+                                            'subject_no_visits_visible,' +
+                                            'unfinished_visits_visible,' +
+                                            'visits_with_missing_appointments_visible,' +
+                                            'approaching_visits_without_appointments_visible,' +
+                                            'approaching_visits_for_mail_contact_visible,' +
+                                            'unfinished_appointments_visible' +
+                                            ') values(true, true, true, true, true, true, true, true, true, true)'))
+    else:
+        operations.append(migrations.RunSQL('insert into web_studynotificationparameters (exceeded_visits_visible,' +
+                                            'missing_redcap_subject_visible,' +
+                                            'inconsistent_redcap_subject_visible,' +
+                                            'subject_require_contact_visible,' +
+                                            'subject_no_visits_visible,' +
+                                            'unfinished_visits_visible,' +
+                                            'visits_with_missing_appointments_visible,' +
+                                            'approaching_visits_without_appointments_visible,' +
+                                            'approaching_visits_for_mail_contact_visible,' +
+                                            'unfinished_appointments_visible' +
+                                            ') values(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)'))
+    operations.append(migrations.AddField(
+        model_name='study',
+        name='notification_parameters',
+        field=models.OneToOneField(default=1, on_delete=django.db.models.deletion.CASCADE,
+                                   to='web.StudyNotificationParameters'),
+        preserve_default=False,
+    ))
diff --git a/smash/web/migrations/0080_auto_20171204_1341.py b/smash/web/migrations/0080_auto_20171204_1341.py
index 6988fe55..07c8ee1a 100644
--- a/smash/web/migrations/0080_auto_20171204_1341.py
+++ b/smash/web/migrations/0080_auto_20171204_1341.py
@@ -4,6 +4,8 @@
 
 from django.db import migrations, models
 
+from ..migration_functions import is_sqlite_db
+
 
 # noinspection PyUnusedLocal
 # noinspection PyPep8Naming
@@ -61,11 +63,15 @@ class Migration(migrations.Migration):
             name='datetime_contact_reminder',
             field=models.BooleanField(choices=[(True, b'Yes'), (False, b'No')], default=True,
                                       verbose_name=b'Last contact attempt'),
-        ),
-        migrations.RunSQL('UPDATE web_studycolumns SET datetime_contact_reminder =FALSE WHERE ' +
-                          'NOT(id IN (SELECT columns_id FROM web_study));'),
+        )]
+    if is_sqlite_db():
+        operations.append(migrations.RunSQL('UPDATE web_studycolumns SET datetime_contact_reminder =0 WHERE ' +
+                                            'NOT(id IN (SELECT columns_id FROM web_study));'))
+    else:
+        operations.append(migrations.RunSQL('UPDATE web_studycolumns SET datetime_contact_reminder =FALSE WHERE ' +
+                                            'NOT(id IN (SELECT columns_id FROM web_study));'))
 
-        migrations.RunPython(create_default_columns_for_SUBJECT_LIST_REQUIRE_CONTACT),
+    operations = operations + [migrations.RunPython(create_default_columns_for_SUBJECT_LIST_REQUIRE_CONTACT),
 
         migrations.RunSQL('INSERT INTO web_studysubjectlist (' +
                           'study_id, ' +
diff --git a/smash/web/migrations/0081_studysubjectlist_last_contact_attempt.py b/smash/web/migrations/0081_studysubjectlist_last_contact_attempt.py
index da4c5e8b..c3f963b4 100644
--- a/smash/web/migrations/0081_studysubjectlist_last_contact_attempt.py
+++ b/smash/web/migrations/0081_studysubjectlist_last_contact_attempt.py
@@ -4,6 +4,8 @@
 
 from django.db import migrations, models
 
+from ..migration_functions import is_sqlite_db
+
 
 # noinspection PyUnusedLocal
 # noinspection PyPep8Naming
@@ -67,19 +69,34 @@ class Migration(migrations.Migration):
             field=models.BooleanField(default=False, verbose_name=b'Last contact attempt'),
         ),
         migrations.RunPython(create_default_columns_for_SUBJECT_LIST_NO_VISIT),
-
-        migrations.RunSQL('INSERT INTO web_studysubjectlist (' +
-                          'study_id, ' +
-                          'visible_subject_study_columns_id, ' +
-                          'visible_subject_columns_id, ' +
-                          'last_contact_attempt,'
-                          'visits,'
-                          'type) ' +
-                          "SELECT " +
-                          "1, " +
-                          "max(web_studycolumns.id), " +
-                          "max(web_subjectcolumns.id), " +
-                          "false, " +
-                          "false, " +
-                          "'NO_VISIT' FROM web_studycolumns, web_subjectcolumns;"),
     ]
+    if is_sqlite_db():
+        operations.append(migrations.RunSQL('INSERT INTO web_studysubjectlist (' +
+                                            'study_id, ' +
+                                            'visible_subject_study_columns_id, ' +
+                                            'visible_subject_columns_id, ' +
+                                            'last_contact_attempt,'
+                                            'visits,'
+                                            'type) ' +
+                                            "SELECT " +
+                                            "1, " +
+                                            "max(web_studycolumns.id), " +
+                                            "max(web_subjectcolumns.id), " +
+                                            "0, " +
+                                            "0, " +
+                                            "'NO_VISIT' FROM web_studycolumns, web_subjectcolumns;"))
+    else:
+        operations.append(migrations.RunSQL('INSERT INTO web_studysubjectlist (' +
+                                            'study_id, ' +
+                                            'visible_subject_study_columns_id, ' +
+                                            'visible_subject_columns_id, ' +
+                                            'last_contact_attempt,'
+                                            'visits,'
+                                            'type) ' +
+                                            "SELECT " +
+                                            "1, " +
+                                            "max(web_studycolumns.id), " +
+                                            "max(web_subjectcolumns.id), " +
+                                            "false, " +
+                                            "false, " +
+                                            "'NO_VISIT' FROM web_studycolumns, web_subjectcolumns;"))
diff --git a/smash/web/migrations/0085_auto_20171205_1650.py b/smash/web/migrations/0085_auto_20171205_1650.py
index a579ab20..d0d6190d 100644
--- a/smash/web/migrations/0085_auto_20171205_1650.py
+++ b/smash/web/migrations/0085_auto_20171205_1650.py
@@ -3,6 +3,7 @@
 
 
 from django.db import migrations, models
+from ..migration_functions import is_sqlite_db
 
 # noinspection PyUnusedLocal
 # noinspection PyPep8Naming
@@ -70,8 +71,10 @@ class Migration(migrations.Migration):
             name='type',
             field=models.CharField(blank=True, choices=[(b'GENERIC', b'Generic'), (b'NO_VISIT', b'Subjects without visit'), (b'REQUIRE_CONTACT', b'Subjects required contact')], max_length=50, null=True, verbose_name=b'Type of list'),
         ),
-        migrations.RunPython(create_default_columns_for_VISIT_LIST_EXCEEDED_TIME),
-        migrations.RunSQL('INSERT INTO web_studyvisitlist (' +
+        migrations.RunPython(create_default_columns_for_VISIT_LIST_EXCEEDED_TIME)
+    ]
+    if is_sqlite_db():
+        operations.append(migrations.RunSQL('INSERT INTO web_studyvisitlist (' +
                           'study_id, ' +
                           'visible_visit_columns_id, ' +
                           'visible_subject_columns_id, ' +
@@ -85,8 +88,26 @@ class Migration(migrations.Migration):
                           "max(web_visitcolumns.id), " +
                           "max(web_subjectcolumns.id), " +
                           "max(web_studycolumns.id), " +
-                          "TRUE, " +
-                          "TRUE, " +
-                          "TRUE, " +
-                          "'EXCEEDED_TIME' FROM web_visitcolumns, web_studycolumns, web_subjectcolumns;"),
-    ]
+                          "0, " +
+                          "0, " +
+                          "0, " +
+                          "'EXCEEDED_TIME' FROM web_visitcolumns, web_studycolumns, web_subjectcolumns;"))
+    else:
+        operations.append(migrations.RunSQL('INSERT INTO web_studyvisitlist (' +
+                                            'study_id, ' +
+                                            'visible_visit_columns_id, ' +
+                                            'visible_subject_columns_id, ' +
+                                            'visible_study_subject_columns_id, ' +
+                                            'visible_appointment_types_done,'
+                                            'visible_appointment_types_in_progress,'
+                                            'visible_appointment_types_missing,'
+                                            'type) ' +
+                                            "SELECT " +
+                                            "1, " +
+                                            "max(web_visitcolumns.id), " +
+                                            "max(web_subjectcolumns.id), " +
+                                            "max(web_studycolumns.id), " +
+                                            "TRUE, " +
+                                            "TRUE, " +
+                                            "TRUE, " +
+                                            "'EXCEEDED_TIME' FROM web_visitcolumns, web_studycolumns, web_subjectcolumns;"))
diff --git a/smash/web/migrations/0086_unfinished_visit_list.py b/smash/web/migrations/0086_unfinished_visit_list.py
index 30a4f25b..460b1d99 100644
--- a/smash/web/migrations/0086_unfinished_visit_list.py
+++ b/smash/web/migrations/0086_unfinished_visit_list.py
@@ -3,6 +3,7 @@
 
 
 from django.db import migrations, models
+from ..migration_functions import is_sqlite_db
 
 # noinspection PyUnusedLocal
 # noinspection PyPep8Naming
@@ -65,8 +66,11 @@ class Migration(migrations.Migration):
     ]
 
     operations = [
-        migrations.RunPython(create_default_columns_for_VISIT_LIST_EXCEEDED_TIME),
-        migrations.RunSQL('INSERT INTO web_studyvisitlist (' +
+        migrations.RunPython(create_default_columns_for_VISIT_LIST_EXCEEDED_TIME)
+        ]
+
+    if is_sqlite_db():
+        operations.append(migrations.RunSQL('INSERT INTO web_studyvisitlist (' +
                           'study_id, ' +
                           'visible_visit_columns_id, ' +
                           'visible_subject_columns_id, ' +
@@ -80,8 +84,26 @@ class Migration(migrations.Migration):
                           "max(web_visitcolumns.id), " +
                           "max(web_subjectcolumns.id), " +
                           "max(web_studycolumns.id), " +
-                          "TRUE, " +
-                          "TRUE, " +
-                          "TRUE, " +
-                          "'UNFINISHED' FROM web_visitcolumns, web_studycolumns, web_subjectcolumns;"),
-    ]
+                          "1, " +
+                          "1, " +
+                          "1, " +
+                          "'UNFINISHED' FROM web_visitcolumns, web_studycolumns, web_subjectcolumns;"))
+    else:
+        operations.append(migrations.RunSQL('INSERT INTO web_studyvisitlist (' +
+                                            'study_id, ' +
+                                            'visible_visit_columns_id, ' +
+                                            'visible_subject_columns_id, ' +
+                                            'visible_study_subject_columns_id, ' +
+                                            'visible_appointment_types_done,'
+                                            'visible_appointment_types_in_progress,'
+                                            'visible_appointment_types_missing,'
+                                            'type) ' +
+                                            "SELECT " +
+                                            "1, " +
+                                            "max(web_visitcolumns.id), " +
+                                            "max(web_subjectcolumns.id), " +
+                                            "max(web_studycolumns.id), " +
+                                            "TRUE, " +
+                                            "TRUE, " +
+                                            "TRUE, " +
+                                            "'UNFINISHED' FROM web_visitcolumns, web_studycolumns, web_subjectcolumns;"))
diff --git a/smash/web/migrations/0087_approaching_visit_wihout_appointment_list.py b/smash/web/migrations/0087_approaching_visit_wihout_appointment_list.py
index 5ea8c79f..55d62fd4 100644
--- a/smash/web/migrations/0087_approaching_visit_wihout_appointment_list.py
+++ b/smash/web/migrations/0087_approaching_visit_wihout_appointment_list.py
@@ -4,6 +4,8 @@
 
 from django.db import migrations
 
+from ..migration_functions import is_sqlite_db
+
 
 # noinspection PyUnusedLocal
 # noinspection PyPep8Naming
@@ -66,8 +68,11 @@ class Migration(migrations.Migration):
     ]
 
     operations = [
-        migrations.RunPython(create_default_columns_for_VISIT_LIST_APPROACHING_WITHOUT_APPOINTMENTS),
-        migrations.RunSQL('INSERT INTO web_studyvisitlist (' +
+        migrations.RunPython(create_default_columns_for_VISIT_LIST_APPROACHING_WITHOUT_APPOINTMENTS)
+        ]
+
+    if is_sqlite_db():
+        operations.append(migrations.RunSQL('INSERT INTO web_studyvisitlist (' +
                           'study_id, ' +
                           'visible_visit_columns_id, ' +
                           'visible_subject_columns_id, ' +
@@ -81,8 +86,26 @@ class Migration(migrations.Migration):
                           "max(web_visitcolumns.id), " +
                           "max(web_subjectcolumns.id), " +
                           "max(web_studycolumns.id), " +
-                          "FALSE, " +
-                          "FALSE, " +
-                          "FALSE, " +
-                          "'APPROACHING_WITHOUT_APPOINTMENTS' FROM web_visitcolumns, web_studycolumns, web_subjectcolumns;"),
-    ]
+                          "0, " +
+                          "0, " +
+                          "0, " +
+                          "'APPROACHING_WITHOUT_APPOINTMENTS' FROM web_visitcolumns, web_studycolumns, web_subjectcolumns;"))
+    else:
+        operations.append(migrations.RunSQL('INSERT INTO web_studyvisitlist (' +
+                                            'study_id, ' +
+                                            'visible_visit_columns_id, ' +
+                                            'visible_subject_columns_id, ' +
+                                            'visible_study_subject_columns_id, ' +
+                                            'visible_appointment_types_done,'
+                                            'visible_appointment_types_in_progress,'
+                                            'visible_appointment_types_missing,'
+                                            'type) ' +
+                                            "SELECT " +
+                                            "1, " +
+                                            "max(web_visitcolumns.id), " +
+                                            "max(web_subjectcolumns.id), " +
+                                            "max(web_studycolumns.id), " +
+                                            "FALSE, " +
+                                            "FALSE, " +
+                                            "FALSE, " +
+                                            "'APPROACHING_WITHOUT_APPOINTMENTS' FROM web_visitcolumns, web_studycolumns, web_subjectcolumns;"))
diff --git a/smash/web/migrations/0104_contact_required_list_update.py b/smash/web/migrations/0104_contact_required_list_update.py
index dbfb503e..29da7956 100644
--- a/smash/web/migrations/0104_contact_required_list_update.py
+++ b/smash/web/migrations/0104_contact_required_list_update.py
@@ -2,7 +2,9 @@
 # Generated by Django 1.10.7 on 2018-02-14 10:26
 
 
-from django.db import migrations, models
+from django.db import migrations
+
+from ..migration_functions import is_sqlite_db
 
 
 class Migration(migrations.Migration):
@@ -10,7 +12,13 @@ class Migration(migrations.Migration):
         ('web', '0103_auto_20180214_1026'),
     ]
 
-    operations = [
-        migrations.RunSQL(
-            'UPDATE web_studysubjectlist SET last_contact_attempt = TRUE WHERE type=\'REQUIRE_CONTACT\';'),
-    ]
+    if is_sqlite_db():
+        operations = [
+            migrations.RunSQL(
+                'UPDATE web_studysubjectlist SET last_contact_attempt = 1 WHERE type=\'REQUIRE_CONTACT\';'),
+        ]
+    else:
+        operations = [
+            migrations.RunSQL(
+                'UPDATE web_studysubjectlist SET last_contact_attempt = TRUE WHERE type=\'REQUIRE_CONTACT\';'),
+        ]
diff --git a/smash/web/migrations/0105_unfinished_appointments_list_update.py b/smash/web/migrations/0105_unfinished_appointments_list_update.py
index cfef1c66..c3e926cc 100644
--- a/smash/web/migrations/0105_unfinished_appointments_list_update.py
+++ b/smash/web/migrations/0105_unfinished_appointments_list_update.py
@@ -2,7 +2,9 @@
 # Generated by Django 1.10.7 on 2018-02-14 10:26
 
 
-from django.db import migrations, models
+from django.db import migrations
+
+from ..migration_functions import is_sqlite_db
 
 
 class Migration(migrations.Migration):
@@ -10,8 +12,15 @@ class Migration(migrations.Migration):
         ('web', '0104_contact_required_list_update'),
     ]
 
-    operations = [
-        migrations.RunSQL(
-            'UPDATE web_studycolumns SET nd_number=TRUE, type=TRUE WHERE id IN ' +
-            '(SELECT visible_study_subject_columns_id FROM web_appointmentlist WHERE type = \'UNFINISHED\');'),
-    ]
+    if is_sqlite_db():
+        operations = [
+            migrations.RunSQL(
+                'UPDATE web_studycolumns SET nd_number=1, type=1 WHERE id IN ' +
+                '(SELECT visible_study_subject_columns_id FROM web_appointmentlist WHERE type = \'UNFINISHED\');'),
+        ]
+    else:
+        operations = [
+            migrations.RunSQL(
+                'UPDATE web_studycolumns SET nd_number=TRUE, type=TRUE WHERE id IN ' +
+                '(SELECT visible_study_subject_columns_id FROM web_appointmentlist WHERE type = \'UNFINISHED\');'),
+        ]
diff --git a/smash/web/migrations/0106_approaching_post_mail_list_update.py b/smash/web/migrations/0106_approaching_post_mail_list_update.py
index 80e966cd..93095d3e 100644
--- a/smash/web/migrations/0106_approaching_post_mail_list_update.py
+++ b/smash/web/migrations/0106_approaching_post_mail_list_update.py
@@ -4,6 +4,8 @@
 
 from django.db import migrations, models
 
+from ..migration_functions import is_sqlite_db
+
 def create_default_columns_for_VISIT_LIST_APPROACHING_FOR_MAIL_CONTACT(apps, schema_editor):
     # We can't import the Study model directly as it may be a newer
     # version than this migration expects. We use the historical version.
@@ -63,8 +65,11 @@ class Migration(migrations.Migration):
     ]
 
     operations = [
-        migrations.RunPython(create_default_columns_for_VISIT_LIST_APPROACHING_FOR_MAIL_CONTACT),
-        migrations.RunSQL('INSERT INTO web_studyvisitlist (' +
+        migrations.RunPython(create_default_columns_for_VISIT_LIST_APPROACHING_FOR_MAIL_CONTACT)
+    ]
+
+    if is_sqlite_db():
+        operations.append(migrations.RunSQL('INSERT INTO web_studyvisitlist (' +
                           'study_id, ' +
                           'visible_visit_columns_id, ' +
                           'visible_subject_columns_id, ' +
@@ -78,8 +83,26 @@ class Migration(migrations.Migration):
                           "max(web_visitcolumns.id), " +
                           "max(web_subjectcolumns.id), " +
                           "max(web_studycolumns.id), " +
-                          "FALSE, " +
-                          "FALSE, " +
-                          "FALSE, " +
-                          "'APPROACHING_FOR_MAIL_CONTACT' FROM web_visitcolumns, web_studycolumns, web_subjectcolumns;"),
-    ]
+                          "0, " +
+                          "0, " +
+                          "0, " +
+                          "'APPROACHING_FOR_MAIL_CONTACT' FROM web_visitcolumns, web_studycolumns, web_subjectcolumns;"))
+    else:
+        operations.append(migrations.RunSQL('INSERT INTO web_studyvisitlist (' +
+                                            'study_id, ' +
+                                            'visible_visit_columns_id, ' +
+                                            'visible_subject_columns_id, ' +
+                                            'visible_study_subject_columns_id, ' +
+                                            'visible_appointment_types_done,'
+                                            'visible_appointment_types_in_progress,'
+                                            'visible_appointment_types_missing,'
+                                            'type) ' +
+                                            "SELECT " +
+                                            "1, " +
+                                            "max(web_visitcolumns.id), " +
+                                            "max(web_subjectcolumns.id), " +
+                                            "max(web_studycolumns.id), " +
+                                            "FALSE, " +
+                                            "FALSE, " +
+                                            "FALSE, " +
+                                            "'APPROACHING_FOR_MAIL_CONTACT' FROM web_visitcolumns, web_studycolumns, web_subjectcolumns;"))
diff --git a/smash/web/migrations/0107_exceeded_visit_time_list_update.py b/smash/web/migrations/0107_exceeded_visit_time_list_update.py
index 934b455f..c9f76720 100644
--- a/smash/web/migrations/0107_exceeded_visit_time_list_update.py
+++ b/smash/web/migrations/0107_exceeded_visit_time_list_update.py
@@ -2,7 +2,9 @@
 # Generated by Django 1.10.7 on 2018-02-14 10:26
 
 
-from django.db import migrations, models
+from django.db import migrations
+
+from ..migration_functions import is_sqlite_db
 
 
 class Migration(migrations.Migration):
@@ -10,8 +12,15 @@ class Migration(migrations.Migration):
         ('web', '0106_approaching_post_mail_list_update'),
     ]
 
-    operations = [
-        migrations.RunSQL(
-            'UPDATE web_studycolumns SET nd_number=TRUE WHERE id IN ' +
-            '(SELECT visible_study_subject_columns_id FROM web_studyvisitlist WHERE type = \'EXCEEDED_TIME\');'),
-    ]
+    if is_sqlite_db():
+        operations = [
+            migrations.RunSQL(
+                'UPDATE web_studycolumns SET nd_number=1 WHERE id IN ' +
+                '(SELECT visible_study_subject_columns_id FROM web_studyvisitlist WHERE type = \'EXCEEDED_TIME\');'),
+        ]
+    else:
+        operations = [
+            migrations.RunSQL(
+                'UPDATE web_studycolumns SET nd_number=TRUE WHERE id IN ' +
+                '(SELECT visible_study_subject_columns_id FROM web_studyvisitlist WHERE type = \'EXCEEDED_TIME\');'),
+        ]
diff --git a/smash/web/migrations/0108_unfinished_visit_list_update.py b/smash/web/migrations/0108_unfinished_visit_list_update.py
index b357d3e2..6301b9d6 100644
--- a/smash/web/migrations/0108_unfinished_visit_list_update.py
+++ b/smash/web/migrations/0108_unfinished_visit_list_update.py
@@ -2,7 +2,9 @@
 # Generated by Django 1.10.7 on 2018-02-14 10:26
 
 
-from django.db import migrations, models
+from django.db import migrations
+
+from ..migration_functions import is_sqlite_db
 
 
 class Migration(migrations.Migration):
@@ -10,8 +12,15 @@ class Migration(migrations.Migration):
         ('web', '0107_exceeded_visit_time_list_update'),
     ]
 
-    operations = [
-        migrations.RunSQL(
-            'UPDATE web_studycolumns SET nd_number=TRUE where id IN ' +
-            '(SELECT visible_study_subject_columns_id FROM web_studyvisitlist WHERE type = \'UNFINISHED\');'),
-    ]
+    if is_sqlite_db():
+        operations = [
+            migrations.RunSQL(
+                'UPDATE web_studycolumns SET nd_number=1 where id IN ' +
+                '(SELECT visible_study_subject_columns_id FROM web_studyvisitlist WHERE type = \'UNFINISHED\');'),
+        ]
+    else:
+        operations = [
+            migrations.RunSQL(
+                'UPDATE web_studycolumns SET nd_number=TRUE where id IN ' +
+                '(SELECT visible_study_subject_columns_id FROM web_studyvisitlist WHERE type = \'UNFINISHED\');'),
+        ]
diff --git a/smash/web/migrations/0109_missing_appointment_list_update.py b/smash/web/migrations/0109_missing_appointment_list_update.py
index bbfc2d9d..fad72c2c 100644
--- a/smash/web/migrations/0109_missing_appointment_list_update.py
+++ b/smash/web/migrations/0109_missing_appointment_list_update.py
@@ -2,7 +2,10 @@
 # Generated by Django 1.10.7 on 2018-02-14 10:26
 
 
-from django.db import migrations, models
+from django.db import migrations
+
+from ..migration_functions import is_sqlite_db
+
 
 def create_default_columns_for_VISIT_LIST_MISSING_APPOINTMENT(apps, schema_editor):
     # We can't import the Study model directly as it may be a newer
@@ -64,22 +67,43 @@ class Migration(migrations.Migration):
 
     operations = [
         migrations.RunPython(create_default_columns_for_VISIT_LIST_MISSING_APPOINTMENT),
-        migrations.RunSQL('INSERT INTO web_studyvisitlist (' +
-                          'study_id, ' +
-                          'visible_visit_columns_id, ' +
-                          'visible_subject_columns_id, ' +
-                          'visible_study_subject_columns_id, ' +
-                          'visible_appointment_types_done,'
-                          'visible_appointment_types_in_progress,'
-                          'visible_appointment_types_missing,'
-                          'type) ' +
-                          "SELECT " +
-                          "1, " +
-                          "max(web_visitcolumns.id), " +
-                          "max(web_subjectcolumns.id), " +
-                          "max(web_studycolumns.id), " +
-                          "TRUE, " +
-                          "TRUE, " +
-                          "TRUE, " +
-                          "'MISSING_APPOINTMENTS' FROM web_visitcolumns, web_studycolumns, web_subjectcolumns;"),
     ]
+
+    if is_sqlite_db():
+        operations.append(migrations.RunSQL('INSERT INTO web_studyvisitlist (' +
+                                            'study_id, ' +
+                                            'visible_visit_columns_id, ' +
+                                            'visible_subject_columns_id, ' +
+                                            'visible_study_subject_columns_id, ' +
+                                            'visible_appointment_types_done,'
+                                            'visible_appointment_types_in_progress,'
+                                            'visible_appointment_types_missing,'
+                                            'type) ' +
+                                            "SELECT " +
+                                            "1, " +
+                                            "max(web_visitcolumns.id), " +
+                                            "max(web_subjectcolumns.id), " +
+                                            "max(web_studycolumns.id), " +
+                                            "1, " +
+                                            "1, " +
+                                            "1, " +
+                                            "'MISSING_APPOINTMENTS' FROM web_visitcolumns, web_studycolumns, web_subjectcolumns;"))
+    else:
+        operations.append(migrations.RunSQL('INSERT INTO web_studyvisitlist (' +
+                                            'study_id, ' +
+                                            'visible_visit_columns_id, ' +
+                                            'visible_subject_columns_id, ' +
+                                            'visible_study_subject_columns_id, ' +
+                                            'visible_appointment_types_done,'
+                                            'visible_appointment_types_in_progress,'
+                                            'visible_appointment_types_missing,'
+                                            'type) ' +
+                                            "SELECT " +
+                                            "1, " +
+                                            "max(web_visitcolumns.id), " +
+                                            "max(web_subjectcolumns.id), " +
+                                            "max(web_studycolumns.id), " +
+                                            "TRUE, " +
+                                            "TRUE, " +
+                                            "TRUE, " +
+                                            "'MISSING_APPOINTMENTS' FROM web_visitcolumns, web_studycolumns, web_subjectcolumns;"))
diff --git a/smash/web/migrations/0140_auto_20190528_0953.py b/smash/web/migrations/0140_auto_20190528_0953.py
index e573406c..2c7c1a19 100644
--- a/smash/web/migrations/0140_auto_20190528_0953.py
+++ b/smash/web/migrations/0140_auto_20190528_0953.py
@@ -2,12 +2,13 @@
 # Generated by Django 1.11.5 on 2019-05-28 09:53
 
 
-from django.db import migrations, models
 import django.db.models.deletion
+from django.db import migrations, models
 
+from ..migration_functions import is_sqlite_db
 
-class Migration(migrations.Migration):
 
+class Migration(migrations.Migration):
     dependencies = [
         ('web', '0139_auto_20190321_1400'),
     ]
@@ -23,17 +24,27 @@ class Migration(migrations.Migration):
                 ('mpower_id', models.BooleanField(default=True, verbose_name=b'MPower ID')),
                 ('languages', models.BooleanField(default=True, verbose_name=b'Languages')),
             ],
-        ),
-        migrations.RunSQL('insert into web_studyredcapcolumns (sex,' +
-                          'date_born,' +
-                          'dead,' +
-                          'mpower_id,' +
-                          'languages' +
-                          ') values(true, true, true, true, true)'),
-        migrations.AddField(
-            model_name='study',
-            name='redcap_columns',
-            field=models.OneToOneField(default=True, on_delete=django.db.models.deletion.CASCADE, to='web.StudyRedCapColumns'),
-            preserve_default=False,
-        ),
-    ]
+        )]
+    if is_sqlite_db():
+        operations.append(
+            migrations.RunSQL('insert into web_studyredcapcolumns (sex,' +
+                              'date_born,' +
+                              'dead,' +
+                              'mpower_id,' +
+                              'languages' +
+                              ') values(1, 1, 1, 1, 1)'))
+    else:
+        operations.append(
+            migrations.RunSQL('insert into web_studyredcapcolumns (sex,' +
+                              'date_born,' +
+                              'dead,' +
+                              'mpower_id,' +
+                              'languages' +
+                              ') values(true, true, true, true, true)'))
+    operations.append(migrations.AddField(
+        model_name='study',
+        name='redcap_columns',
+        field=models.OneToOneField(default=True, on_delete=django.db.models.deletion.CASCADE,
+                                   to='web.StudyRedCapColumns'),
+        preserve_default=False,
+    ))
-- 
GitLab