Sfoglia il codice sorgente

Open the door to handling various db backends

jherve 1 anno fa
parent
commit
9d1b010d7b
2 ha cambiato i file con 13 aggiunte e 2 eliminazioni
  1. 1 1
      settings.toml
  2. 12 1
      src/de_quoi_parle_le_monde/storage.py

+ 1 - 1
settings.toml

@@ -1 +1 @@
-database_url="test.db"
+database_url="sqlite:///test.db"

+ 12 - 1
src/de_quoi_parle_le_monde/storage.py

@@ -4,6 +4,7 @@ import asyncio
 from datetime import datetime
 import numpy as np
 from attrs import frozen
+from yarl import URL
 
 from config import settings
 from de_quoi_parle_le_monde.article import (
@@ -311,7 +312,17 @@ class Storage:
     ]
 
     def __init__(self):
-        self.conn = DbConnection(settings.database_url)
+        # We try to reproduce the scheme used by SQLAlchemy for Database-URLs
+        # https://docs.sqlalchemy.org/en/20/core/engines.html#database-urls
+        conn_url = URL(settings.database_url)
+
+        if conn_url.scheme == "sqlite":
+            if conn_url.path.startswith("//"):
+                raise ValueError("Absolute URLs not supported for sqlite")
+            elif conn_url.path.startswith("/"):
+                self.conn = DbConnection(conn_url.path[1:])
+        else:
+            raise ValueError("Only the SQLite backend is supported")
 
     @staticmethod
     async def create():