Skip to content

Commit 4c04739

Browse files
authored
Merge pull request #7585 from RasaHQ/fix-login-db
correctly replace database name in query
2 parents 746f723 + 3ecdbb5 commit 4c04739

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

changelog/7585.bugfix.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Fixed an error when using the [SQLTrackerStore](tracker-stores.mdx#sqltrackerstore)
2+
with a Postgres database and the parameter `login_db` specified.
3+
4+
The error was:
5+
6+
```bash
7+
psycopg2.errors.SyntaxError: syntax error at end of input
8+
rasa-production_1 | LINE 1: SELECT 1 FROM pg_catalog.pg_database WHERE datname = ?
9+
```

rasa/core/tracker_store.py

+23-12
Original file line numberDiff line numberDiff line change
@@ -917,33 +917,44 @@ def get_db_url(
917917
)
918918

919919
def _create_database_and_update_engine(self, db: Text, engine_url: "URL"):
920-
"""Create databse `db` and update engine to reflect the updated `engine_url`."""
921-
920+
"""Creates database `db` and updates engine accordingly."""
922921
from sqlalchemy import create_engine
923922

923+
if not self.engine.dialect.name == "postgresql":
924+
rasa.shared.utils.io.raise_warning(
925+
"The parameter 'login_db' can only be used with a postgres database.",
926+
)
927+
return
928+
924929
self._create_database(self.engine, db)
925930
engine_url.database = db
926931
self.engine = create_engine(engine_url)
927932

928933
@staticmethod
929-
def _create_database(engine: "Engine", db: Text):
934+
def _create_database(engine: "Engine", database_name: Text) -> None:
930935
"""Create database `db` on `engine` if it does not exist."""
931-
932936
import psycopg2
933937

934938
conn = engine.connect()
935939

936-
cursor = conn.connection.cursor()
937-
cursor.execute("COMMIT")
938-
cursor.execute("SELECT 1 FROM pg_catalog.pg_database WHERE datname = ?", (db,))
939-
exists = cursor.fetchone()
940-
if not exists:
940+
matching_rows = (
941+
conn.execution_options(isolation_level="AUTOCOMMIT")
942+
.execute(
943+
sa.text(
944+
"SELECT 1 FROM pg_catalog.pg_database "
945+
"WHERE datname = :database_name"
946+
),
947+
database_name=database_name,
948+
)
949+
.rowcount
950+
)
951+
952+
if not matching_rows:
941953
try:
942-
cursor.execute(f"CREATE DATABASE {db}")
954+
conn.execute(f"CREATE DATABASE {database_name}")
943955
except psycopg2.IntegrityError as e:
944-
logger.error(f"Could not create database '{db}': {e}")
956+
logger.error(f"Could not create database '{database_name}': {e}")
945957

946-
cursor.close()
947958
conn.close()
948959

949960
@contextlib.contextmanager

tests/core/test_tracker_stores.py

+5
Original file line numberDiff line numberDiff line change
@@ -891,3 +891,8 @@ def test_current_state_without_events(default_domain: Domain):
891891

892892
# `events` key should not be in there
893893
assert state and "events" not in state
894+
895+
896+
def test_login_db_with_no_postgresql(tmp_path: Path):
897+
with pytest.warns(UserWarning):
898+
SQLTrackerStore(db=str(tmp_path / "rasa.db"), login_db="other")

0 commit comments

Comments
 (0)