Skip to content

Commit

Permalink
Use Psycopg3 instead of Psycopg2 (#21)
Browse files Browse the repository at this point in the history
* adjust dependency psycopg2->psycopg[binary]

* require postgresql+psycopg:// in dsn

* replace old dsn prefix to keep .toml backwards compatibility

* update docs
  • Loading branch information
meksor committed Sep 15, 2023
1 parent 1eb6d51 commit 0e7ea6e
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 37 deletions.
2 changes: 1 addition & 1 deletion doc/source/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ By default, an SQLite database will be created. If you want to add an existing d

.. code-block::
ixmp4 platforms add <database-name> --dsn postgresql://user:pw@host/db
ixmp4 platforms add <database-name> --dsn postgresql+psycopg://user:pw@host/db
From a Python environment, you can then access this **ixmp4 database** using the
following code:
Expand Down
2 changes: 1 addition & 1 deletion ixmp4/cli/platforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def validate_name(name: str):
def validate_dsn(dsn: str | None):
if dsn is None:
return None
match = re.match(r"^(sqlite|postgresql|https|http)(\:\/\/)", dsn)
match = re.match(r"^(sqlite|postgresql\+psycopg|https|http)(\:\/\/)", dsn)
if match is None:
raise typer.BadParameter(
"Platform dsn must be a valid URl or database connection string."
Expand Down
5 changes: 5 additions & 0 deletions ixmp4/data/backend/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ def __init__(self, info: PlatformInfo) -> None:
self.make_repositories()

def make_engine(self, dsn: str):
if dsn.startswith("postgresql://"):
logger.debug(
"Replacing the platform dsn prefix to use the new `psycopg` driver."
)
dsn = dsn.replace("postgresql://", "postgresql+psycopg://")
self.engine = cached_create_engine(dsn)
self.session = self.Session(bind=self.engine)

Expand Down
Loading

0 comments on commit 0e7ea6e

Please sign in to comment.