From: Gael Le Mignot Date: Mon, 31 May 2021 07:07:31 +0000 (+0200) Subject: First shot at switching to postgresql X-Git-Tag: 2.1~66^2^2~56 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=refs%2Fheads%2Ffeature%2Fpostgres;p=teleforma.git First shot at switching to postgresql --- diff --git a/app/bin/wait.sh b/app/bin/wait.sh index e9331381..0d92f08c 100755 --- a/app/bin/wait.sh +++ b/app/bin/wait.sh @@ -1,3 +1,3 @@ #!/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; diff --git a/app/settings.py b/app/settings.py index bcec29ed..ba1903d6 100644 --- a/app/settings.py +++ b/app/settings.py @@ -29,21 +29,21 @@ MANAGERS = ADMINS 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. @@ -336,3 +336,5 @@ def show_user_as(user): POSTMAN_SHOW_USER_AS = show_user_as #THUMBNAIL_FORCE_OVERWRITE = True + +ALLOWED_HOSTS = [ 'crfpa.dockdev.pilotsystems.net' ] diff --git a/docker-compose.yml b/docker-compose.yml index 82fd7518..b6a664e5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,19 +15,21 @@ services: 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 diff --git a/env/debug.env b/env/debug.env index 4131492b..3634c8f5 100644 --- a/env/debug.env +++ b/env/debug.env @@ -4,4 +4,9 @@ MYSQL_ROOT_PASSWORD=mysecretpassword 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 diff --git a/requirements.txt b/requirements.txt index bec69740..56d7f373 100644 --- a/requirements.txt +++ b/requirements.txt @@ -21,4 +21,5 @@ sorl-thumbnail==12.7.0 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 diff --git a/teleforma/management/commands/teleforma-convert-boolean-fields.py b/teleforma/management/commands/teleforma-convert-boolean-fields.py new file mode 100644 index 00000000..fcbd33de --- /dev/null +++ b/teleforma/management/commands/teleforma-convert-boolean-fields.py @@ -0,0 +1,18 @@ +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;")