Skip to content

Commit 3201a61

Browse files
Studio: pin search_path so empty_database CREATE EXTENSION works
1 parent cc35bbc commit 3201a61

2 files changed

Lines changed: 33 additions & 8 deletions

File tree

studio/provsql_studio/db.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,14 @@ def empty_database(pool: ConnectionPool) -> list[str]:
693693
# Pre-PG15 default grants on public, so unqualified CREATE TABLE
694694
# keeps working for every role that could before.
695695
cur.execute("GRANT USAGE, CREATE ON SCHEMA public TO PUBLIC")
696+
# CREATE EXTENSION installs into the first existing schema on the
697+
# session search_path. That search_path is inherited from the
698+
# database default, which may point at a user schema we just
699+
# dropped above (e.g. the regression fixture sets the DB-level
700+
# search_path to provsql_test) -- leaving no valid creation
701+
# target and a "no schema has been selected for creation" error.
702+
# Pin it to the public schema we just recreated.
703+
cur.execute("SET search_path TO public")
696704
cur.execute("CREATE EXTENSION IF NOT EXISTS provsql CASCADE")
697705
return schemas
698706

studio/tests/e2e/test_notebook_ui.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -901,17 +901,34 @@ def test_empty_db_button_confirms_and_wipes(
901901
import psycopg
902902
from pathlib import Path
903903
repo = Path(__file__).resolve().parents[3]
904+
# The fixture script expects a virgin database (plain CREATE EXTENSION
905+
# and CREATE SCHEMA provsql_test); the wipe reinstalled provsql and
906+
# leaves provsql_test (the active search_path schema) in place, so shed
907+
# both first. The database-level search_path that the original setup.sql
908+
# established (ALTER DATABASE ... SET search_path = provsql_test) must
909+
# also be reset: once provsql_test is dropped it points at a missing
910+
# schema, so the fresh sessions below would have no valid creation
911+
# target and setup.sql's CREATE EXTENSION would fail with "no schema
912+
# selected". setup.sql re-establishes the search_path itself.
904913
with psycopg.connect(test_dsn, autocommit=True) as conn:
905-
# The fixture script expects a virgin database (plain
906-
# CREATE EXTENSION); the wipe reinstalled provsql, so shed
907-
# it first.
908914
conn.execute("DROP EXTENSION IF EXISTS provsql CASCADE")
909915
conn.execute("DROP SCHEMA IF EXISTS provsql CASCADE")
910-
for fname in ("setup.sql", "add_provenance.sql"):
911-
sql_text = "\n".join(
912-
line for line in
913-
(repo / "test" / "sql" / fname).read_text().splitlines()
914-
if not line.startswith("\\"))
916+
conn.execute("DROP SCHEMA IF EXISTS provsql_test CASCADE")
917+
conn.execute(
918+
"DO $$ BEGIN EXECUTE format("
919+
"'ALTER DATABASE %I RESET search_path', current_database());"
920+
" END $$")
921+
# Each file gets its own connection: setup.sql sets the search_path at
922+
# the database level (ALTER DATABASE), which only takes effect for
923+
# sessions opened afterwards, so add_provenance.sql must run in a fresh
924+
# one to resolve the provsql schema -- the same reason conftest seeds
925+
# per file.
926+
for fname in ("setup.sql", "add_provenance.sql"):
927+
sql_text = "\n".join(
928+
line for line in
929+
(repo / "test" / "sql" / fname).read_text().splitlines()
930+
if not line.startswith("\\"))
931+
with psycopg.connect(test_dsn, autocommit=True) as conn:
915932
conn.execute(sql_text)
916933
# The restore re-created the extension behind the server's back;
917934
# bounce its pool (same-database connection switch) so later

0 commit comments

Comments
 (0)