From b6d8dcd46f6712d5f49b6892a5cd0c6d3346cf53 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Fri, 13 Aug 2021 11:46:44 +0200
Subject: [PATCH] misc: drop sqlit support - postgresql only (#56148)

---
 .gitignore                                    |  1 -
 README                                        |  2 +-
 .../0052_event_date_range_constraint.py       |  2 --
 .../0053_event_date_range_constraint.py       |  2 --
 chrono/settings.py                            |  3 +--
 chrono/utils/db.py                            | 20 +++++++++----------
 debian/control                                |  4 ++--
 tests/settings.py                             |  2 +-
 tests/test_ensure_jsonbfields.py              |  1 -
 tests/test_misc.py                            |  9 ---------
 tox.ini                                       |  3 +--
 11 files changed, 15 insertions(+), 34 deletions(-)

diff --git a/.gitignore b/.gitignore
index 83e8f8f..c6dd879 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,7 +1,6 @@
 *.pyc
 *.mo
 local_settings.py
-/db.sqlite3
 /dist
 /chrono.egg-info
 /chrono/manager/static/css/style.css
diff --git a/README b/README
index fd8295e..084be01 100644
--- a/README
+++ b/README
@@ -13,7 +13,7 @@ Dependencies can be installed with pip,
  $ pip install -r requirements.txt
 
 It's then required to get the database configured (./manage.py migrate); by
-default it will create a db.sqlite3 file.
+default it will create a postgresqsl DB.
 
 You can then run the Django test server for a quick try (you should refer to
 the Django documentation for production deployments).
diff --git a/chrono/agendas/migrations/0052_event_date_range_constraint.py b/chrono/agendas/migrations/0052_event_date_range_constraint.py
index b806f67..33c9cfb 100644
--- a/chrono/agendas/migrations/0052_event_date_range_constraint.py
+++ b/chrono/agendas/migrations/0052_event_date_range_constraint.py
@@ -102,8 +102,6 @@ class Migration(migrations.Migration):
     operations = [migrations.RunSQL(sql=sql_forwards, reverse_sql=sql_backwards)]
 
     def _check_db(self, project_state, schema_editor):
-        if schema_editor.connection.vendor != 'postgresql':
-            return project_state
         try:
             with transaction.atomic():
                 try:
diff --git a/chrono/agendas/migrations/0053_event_date_range_constraint.py b/chrono/agendas/migrations/0053_event_date_range_constraint.py
index 5516dd8..e187ba2 100644
--- a/chrono/agendas/migrations/0053_event_date_range_constraint.py
+++ b/chrono/agendas/migrations/0053_event_date_range_constraint.py
@@ -22,8 +22,6 @@ class Migration(migrations.Migration):
     operations = [migrations.RunSQL(sql=sql_forwards, reverse_sql=sql_backwards)]
 
     def _check_db(self, project_state, schema_editor):
-        if schema_editor.connection.vendor != 'postgresql':
-            return project_state
         try:
             with transaction.atomic():
                 try:
diff --git a/chrono/settings.py b/chrono/settings.py
index 8e7740a..a6ce48b 100644
--- a/chrono/settings.py
+++ b/chrono/settings.py
@@ -82,8 +82,7 @@ WSGI_APPLICATION = 'chrono.wsgi.application'
 
 DATABASES = {
     'default': {
-        'ENGINE': 'django.db.backends.sqlite3',
-        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
+        'ENGINE': 'django.db.backends.postgresql_psycopg2',
     }
 }
 
diff --git a/chrono/utils/db.py b/chrono/utils/db.py
index 2d59785..d8dae1f 100644
--- a/chrono/utils/db.py
+++ b/chrono/utils/db.py
@@ -14,7 +14,6 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from django.db import connection
 from django.db.migrations.operations.base import Operation
 
 
@@ -30,17 +29,16 @@ class EnsureJsonbType(Operation):
         pass
 
     def database_forwards(self, app_label, schema_editor, from_state, to_state):
-        if connection.vendor == 'postgresql':
-            model = from_state.apps.get_model(app_label, self.model_name)
-            table_name = model._meta.db_table
-            field = model._meta.get_field(self.field_name)
-            _, column_name = field.get_attname_column()
-            with schema_editor.connection.cursor() as cursor:
-                cursor.execute(
-                    'ALTER TABLE {table} ALTER COLUMN {col} TYPE jsonb USING {col}::jsonb;'.format(
-                        table=table_name, col=column_name
-                    )
+        model = from_state.apps.get_model(app_label, self.model_name)
+        table_name = model._meta.db_table
+        field = model._meta.get_field(self.field_name)
+        _, column_name = field.get_attname_column()
+        with schema_editor.connection.cursor() as cursor:
+            cursor.execute(
+                'ALTER TABLE {table} ALTER COLUMN {col} TYPE jsonb USING {col}::jsonb;'.format(
+                    table=table_name, col=column_name
                 )
+            )
 
     def database_backwards(self, app_label, schema_editor, from_state, to_state):
         pass
diff --git a/debian/control b/debian/control
index 56578f6..2679c94 100644
--- a/debian/control
+++ b/debian/control
@@ -26,10 +26,10 @@ Depends: ${misc:Depends},
     python3-django-mellon,
     python3-dateutil,
     uwsgi,
-    uwsgi-plugin-python3
+    uwsgi-plugin-python3,
+    postgresql
 Recommends: nginx,
     python3-workalendar
-Suggests: postgresql
 Breaks: python-chrono (<<0.84.post6)
 Replaces: python-chrono (<<0.84.post6)
 Description: Agendas System
diff --git a/tests/settings.py b/tests/settings.py
index dfd0840..1e527f0 100644
--- a/tests/settings.py
+++ b/tests/settings.py
@@ -10,7 +10,7 @@ REST_FRAMEWORK = {
 
 DATABASES = {
     'default': {
-        'ENGINE': os.environ.get('DB_ENGINE', 'django.db.backends.sqlite3'),
+        'ENGINE': 'django.db.backends.postgresql_psycopg2',
         'TEST': {
             'NAME': 'chrono-test-%s' % os.environ.get("BRANCH_NAME", "").replace('/', '-')[:63],
         },
diff --git a/tests/test_ensure_jsonbfields.py b/tests/test_ensure_jsonbfields.py
index 24b4828..2a4515a 100644
--- a/tests/test_ensure_jsonbfields.py
+++ b/tests/test_ensure_jsonbfields.py
@@ -5,7 +5,6 @@ from django.db import connection
 pytestmark = pytest.mark.django_db
 
 
-@pytest.mark.skipif(connection.vendor != 'postgresql', reason='only postgresql is supported')
 def test_ensure_jsonb_fields():
     json_fields = (
         'extra_data',
diff --git a/tests/test_misc.py b/tests/test_misc.py
index 4fa37de..678f545 100644
--- a/tests/test_misc.py
+++ b/tests/test_misc.py
@@ -31,9 +31,6 @@ def check_end_datetime(event, value):
 
 
 def test_event_ignore_reason():
-    if connection.vendor != 'postgresql':
-        pytest.skip('postgresql required')
-
     agenda = Agenda.objects.create(label='Meetings', kind='meetings')
     meeting_type = MeetingType.objects.create(agenda=agenda, label='Foo', duration=60)
     desk = Desk.objects.create(agenda=agenda, label='Desk')
@@ -62,9 +59,6 @@ def test_event_ignore_reason():
 
 
 def test_event_end_datetime():
-    if connection.vendor != 'postgresql':
-        pytest.skip('postgresql required')
-
     agenda = Agenda.objects.create(label='Meetings', kind='meetings')
     meeting_type1 = MeetingType.objects.create(agenda=agenda, label='Foo', duration=60)
     meeting_type2 = MeetingType.objects.create(agenda=agenda, label='Foo', duration=45)
@@ -101,9 +95,6 @@ def test_event_end_datetime():
 
 
 def test_meeting_event_exclusion_constraint():
-    if connection.vendor != 'postgresql':
-        pytest.skip('postgresql required')
-
     agenda = Agenda.objects.create(label='Meetings', kind='meetings')
     meeting_type1 = MeetingType.objects.create(agenda=agenda, label='Foo 1', duration=60)
     meeting_type2 = MeetingType.objects.create(agenda=agenda, label='Foo 2', duration=30)
diff --git a/tox.ini b/tox.ini
index 603a17a..3bc82e7 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,5 +1,5 @@
 [tox]
-envlist = py3-django22-pg-codestyle-coverage-pylint
+envlist = py3-django22-codestyle-coverage-pylint
 toxworkdir = {env:TMPDIR:/tmp}/tox-{env:USER}/chrono/{env:BRANCH_NAME:}
 
 [testenv]
@@ -12,7 +12,6 @@ setenv =
   BRANCH_NAME={env:BRANCH_NAME:}
   SETUPTOOLS_USE_DISTUTILS=stdlib
   coverage: COVERAGE=--junitxml=junit-{envname}.xml --cov-report xml --cov-report html --cov=chrono/
-  pg: DB_ENGINE=django.db.backends.postgresql_psycopg2
 deps =
   pytest-cov
   pytest-django
-- 
2.30.2

