浏览代码

Upgrade backend to support postgresql

jherve 1 年之前
父节点
当前提交
d681c8a42c
共有 5 个文件被更改,包括 54 次插入0 次删除
  1. 1 0
      pyproject.toml
  2. 4 0
      requirements-dev.lock
  3. 4 0
      requirements.lock
  4. 42 0
      src/de_quoi_parle_le_monde/db/postgres.py
  5. 3 0
      src/de_quoi_parle_le_monde/storage.py

+ 1 - 0
pyproject.toml

@@ -26,6 +26,7 @@ dependencies = [
     "protobuf>=5.26.1",
     "dynaconf>=3.2.5",
     "packaging>=24.0",
+    "asyncpg>=0.29.0",
 ]
 readme = "README.md"
 requires-python = ">= 3.11"

+ 4 - 0
requirements-dev.lock

@@ -35,6 +35,10 @@ anyio==4.3.0
     # via httpx
     # via starlette
     # via watchfiles
+async-timeout==4.0.3
+    # via asyncpg
+asyncpg==0.29.0
+    # via de-quoi-parle-le-monde
 attrs==23.2.0
     # via aiohttp
     # via aiohttp-client-cache

+ 4 - 0
requirements.lock

@@ -35,6 +35,10 @@ anyio==4.3.0
     # via httpx
     # via starlette
     # via watchfiles
+async-timeout==4.0.3
+    # via asyncpg
+asyncpg==0.29.0
+    # via de-quoi-parle-le-monde
 attrs==23.2.0
     # via aiohttp
     # via aiohttp-client-cache

+ 42 - 0
src/de_quoi_parle_le_monde/db/postgres.py

@@ -0,0 +1,42 @@
+import asyncpg
+import traceback
+
+from .connection import DbConnection
+
+
+class DbConnectionPostgres(DbConnection):
+    def __init__(self, conn_str):
+        self.connection_string = conn_str
+        self.conn = None
+
+    async def __aenter__(self):
+        self.conn = await asyncpg.connect(self.connection_string)
+        return self
+
+    async def __aexit__(self, exc_type, exc, tb):
+        await self.conn.close()
+        self.conn = None
+
+    async def execute(self, *args, **kwargs):
+        return await self.conn.execute(*args, **kwargs)
+
+    async def execute_fetchall(self, *args, **kwargs):
+        try:
+            res = await self.conn.fetch(*args, **kwargs)
+            return res
+        except Exception as e:
+            print("exception on exec of : ", args)
+            traceback.print_exception(e)
+            raise e
+
+    async def execute_insert(self, *args, **kwargs):
+        try:
+            ret = await self.conn.execute(*args, **kwargs)
+            return ret
+        except Exception as e:
+            print("exception on exec of : ", args)
+            traceback.print_exception(e)
+            raise e
+
+    async def commit(self):
+        return

+ 3 - 0
src/de_quoi_parle_le_monde/storage.py

@@ -11,6 +11,7 @@ from de_quoi_parle_le_monde.article import (
     FeaturedArticle,
 )
 from de_quoi_parle_le_monde.db.sqlite import DbConnectionSQLite
+from de_quoi_parle_le_monde.db.postgres import DbConnectionPostgres
 from de_quoi_parle_le_monde.internet_archive import InternetArchiveSnapshotId
 
 
@@ -291,6 +292,8 @@ class Storage:
                 raise ValueError("Absolute URLs not supported for sqlite")
             elif conn_url.path.startswith("/"):
                 self.conn = DbConnectionSQLite(conn_url.path[1:])
+        elif conn_url.scheme == "postgresql":
+            self.conn = DbConnectionPostgres(settings.database_url)
         else:
             raise ValueError("Only the SQLite backend is supported")