From 732a3f957620d17d0bb8ba936c15b94c93624708 Mon Sep 17 00:00:00 2001
From: Erwan Rouchet <rouchet@teklia.com>
Date: Mon, 5 Sep 2022 12:49:10 +0000
Subject: [PATCH] Handle Decimal values when saving rows to SQLite databases

---
 .gitlab-ci.yml                        |  2 +-
 arkindex/documents/export/__init__.py | 10 ++++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 6b89fb1da8..51bdc21217 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -51,7 +51,7 @@ backend-tests:
   stage: test
 
   services:
-    - name: postgis/postgis:12-3.1
+    - name: postgis/postgis:14-3.2
       alias: postgres
 
   artifacts:
diff --git a/arkindex/documents/export/__init__.py b/arkindex/documents/export/__init__.py
index 78c8fbfbda..02ac0cf715 100644
--- a/arkindex/documents/export/__init__.py
+++ b/arkindex/documents/export/__init__.py
@@ -5,6 +5,7 @@ import sqlite3
 import tempfile
 import uuid
 from datetime import datetime, timezone
+from decimal import Decimal
 from pathlib import Path
 
 from django.conf import settings
@@ -70,6 +71,15 @@ def save_sqlite(rows, table, cursor):
         if isinstance(value, (list, dict)):
             return json.dumps(value)
 
+        # Serialize Decimal numbers as regular floats
+        # We don't care about any loss of precision because SQLite will loose that precision anyway
+        if isinstance(value, Decimal):
+            return float(value)
+
+        # Show very explicit error messages if we stumble upon an unexpected type
+        # https://docs.python.org/3.8/library/sqlite3.html#sqlite-and-python-types
+        assert value is None or isinstance(value, (int, float, str, bytes)), f'Type {type(value)} is not supported by sqlite3'
+
         return value
 
     rows = [
-- 
GitLab