#!/bin/sh
 
-/srv/bin/misc/wait-for-it/wait-for-it.sh -h localhost -p $DB_PORT;
+/srv/bin/misc/wait-for-it/wait-for-it.sh -h $DB_HOST -p $DB_PORT;
 
 DATABASES = {
     'default': {
         # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
-        'ENGINE': 'django.db.backends.mysql',
+        'ENGINE': 'django.db.backends.postgresql_psycopg2',
         # Or path to database file if using sqlite3.
-        'NAME': os.environ.get('MYSQL_DATABASE'),
+        'NAME': os.environ.get('POSTGRES_DATABASE'),
         # Not used with sqlite3.
-        'USER': os.environ.get('MYSQL_USER'),
+        'USER': os.environ.get('POSTGRES_USER'),
         # Not used with sqlite3.
-        'PASSWORD': os.environ.get('MYSQL_PASSWORD'),
+        'PASSWORD': os.environ.get('POSTGRES_PASSWORD'),
         # Set to empty string for localhost. Not used with sqlite3.
-        'HOST': 'db',
+        'HOST': 'postgres',
         # Set to empty string for default. Not used with sqlite3.
         'PORT': '',
-        'OPTIONS': {'init_command': 'SET storage_engine=InnoDB', },
     }
 }
 
+
 # Local time zone for this installation. Choices can be found here:
 # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
 # although not all choices may be available on all operating systems.
 POSTMAN_SHOW_USER_AS = show_user_as
 
 #THUMBNAIL_FORCE_OVERWRITE = True
+
+ALLOWED_HOSTS = [ 'crfpa.dockdev.pilotsystems.net' ]
 
     ports:
       - "8000:8000"
     links:
-      - db
+      - postgres
     env_file:
       - env/debug.env
 
-  db:
-    image: mariadb
+  postgres:
+    image: postgres
     env_file:
       - env/debug.env
+    environment:
+      PGDATA: /var/lib/postgresql/data/pgdata
     volumes:
+      - ./var/lib/postgresql:/var/lib/postgresql/data:rw
       - ./bin:/srv/bin
-      - ./var/lib/mysql/:/var/lib/mysql
-      - ./var/log/mysql/:/var/log/mysql
       - ./var/backup/:/srv/backup
+      - /etc/localtime:/etc/localtime:ro
 
   memcached:
       image: memcached:alpine
 
 MYSQL_DATABASE=teleforma
 MYSQL_USER=teleforma
 MYSQL_PASSWORD=mysecretpassword
-DB_PORT=3306
+DB_PORT=5432
+DB_HOST=postgres
+
+POSTGRES_PASSWORD=mysecretpassword
+POSTGRES_DATABASE=teleforma
+POSTGRES_USER=teleforma
 
 unidecode==1.2.0
 xhtml2pdf==0.2.5
 xlrd==2.0.1
-xlwt==1.3.0
\ No newline at end of file
+xlwt==1.3.0
+psycopg2==2.8.6 
 
--- /dev/null
+from django.conf import settings
+from django.apps import apps
+from django.core.management.base import BaseCommand, CommandError
+import django.db.models as models
+
+class Command(BaseCommand):
+    help = "Issue SQL commands to fix boolean fields after MySQL migration"
+    admin_email = 'webmaster@parisson.com'
+
+    def handle(self, *args, **options):
+        for app, _ in apps.all_models.items():
+            app_models = apps.get_app_config(app).get_models()
+            for model in app_models:
+                table = model._meta.db_table
+                for field in model._meta.fields:
+                    if isinstance(field, models.BooleanField):
+                        field_name = field.column
+                        print(f"alter table {table} alter {field_name} type boolean using case when {field_name}=0 then false else true end;")