Skip to content

Commit 2d62dea

Browse files
committed
Fix destination schema creation
1 parent 362452a commit 2d62dea

File tree

3 files changed

+47
-18
lines changed

3 files changed

+47
-18
lines changed

sqlsynthgen/create.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def create_db_tables(metadata: MetaData) -> None:
2626
if settings.dst_schema:
2727
schema_name = settings.dst_schema
2828
with engine.connect() as connection:
29-
if not engine.dialect.has_schema(connection, schema_name=schema_name):
30-
connection.execute(CreateSchema(schema_name, if_not_exists=True))
29+
connection.execute(CreateSchema(schema_name, if_not_exists=True))
30+
connection.commit()
3131

3232
# Recreate the engine, this time with a schema specified
3333
engine = get_sync_engine(create_db_engine(dst_dsn, schema_name=schema_name))

tests/examples/dst.dump

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,22 @@ SET default_table_access_method = heap;
5252
-- rather have to handle the ignore at a later stage.
5353

5454
--
55-
-- Name: unignorable_table; Type: TABLE; Schema: public; Owner: postgres
55+
-- Name: unignorable_table; Type: TABLE; Schema: dstschema; Owner: postgres
5656
--
5757

58-
CREATE TABLE public.unignorable_table (
58+
create schema dstschema;
59+
60+
CREATE TABLE dstschema.unignorable_table (
5961
id integer NOT NULL
6062
);
6163

62-
ALTER TABLE public.unignorable_table OWNER TO postgres;
64+
ALTER TABLE dstschema.unignorable_table OWNER TO postgres;
6365

6466
--
65-
-- Name: unignorable_table_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
67+
-- Name: unignorable_table_id_seq; Type: SEQUENCE; Schema: dstschema; Owner: postgres
6668
--
6769

68-
CREATE SEQUENCE public.unignorable_table_id_seq
70+
CREATE SEQUENCE dstschema.unignorable_table_id_seq
6971
AS integer
7072
START WITH 1
7173
INCREMENT BY 1
@@ -74,35 +76,35 @@ CREATE SEQUENCE public.unignorable_table_id_seq
7476
CACHE 1;
7577

7678

77-
ALTER TABLE public.unignorable_table_id_seq OWNER TO postgres;
79+
ALTER TABLE dstschema.unignorable_table_id_seq OWNER TO postgres;
7880

7981
--
80-
-- Name: unignorable_table_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
82+
-- Name: unignorable_table_id_seq; Type: SEQUENCE OWNED BY; Schema: dstschema; Owner: postgres
8183
--
8284

83-
ALTER SEQUENCE public.unignorable_table_id_seq OWNED BY public.unignorable_table.id;
85+
ALTER SEQUENCE dstschema.unignorable_table_id_seq OWNED BY dstschema.unignorable_table.id;
8486

8587
--
86-
-- Name: unignorable_table unignorable_table_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
88+
-- Name: unignorable_table unignorable_table_pkey; Type: CONSTRAINT; Schema: dstschema; Owner: postgres
8789
--
8890

89-
ALTER TABLE ONLY public.unignorable_table
91+
ALTER TABLE ONLY dstschema.unignorable_table
9092
ADD CONSTRAINT unignorable_table_pkey PRIMARY KEY (id);
9193

9294
--
93-
-- Name: unignorable_table unignorable_table_id; Type: DEFAULT; Schema: public; Owner: postgres
95+
-- Name: unignorable_table unignorable_table_id; Type: DEFAULT; Schema: dstschema; Owner: postgres
9496
--
9597

96-
ALTER TABLE ONLY public.unignorable_table ALTER COLUMN id SET DEFAULT nextval('public.unignorable_table_id_seq'::regclass);
98+
ALTER TABLE ONLY dstschema.unignorable_table ALTER COLUMN id SET DEFAULT nextval('dstschema.unignorable_table_id_seq'::regclass);
9799

98100
--
99-
-- Data for Name: unignorable_table; Type: TABLE DATA; Schema: public; Owner: postgres
101+
-- Data for Name: unignorable_table; Type: TABLE DATA; Schema: dstschema; Owner: postgres
100102
--
101103

102-
INSERT INTO public.unignorable_table VALUES (1);
104+
INSERT INTO dstschema.unignorable_table VALUES (1);
103105

104106
--
105-
-- Name: unignorable_table_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
107+
-- Name: unignorable_table_id_seq; Type: SEQUENCE SET; Schema: dstschema; Owner: postgres
106108
--
107109

108-
SELECT pg_catalog.setval('public.unignorable_table_id_seq', 2, false);
110+
SELECT pg_catalog.setval('dstschema.unignorable_table_id_seq', 2, false);

tests/test_functional.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
from pathlib import Path
55
from subprocess import run
66

7+
import sqlalchemy
8+
from sqlalchemy import inspect
9+
710
from tests.utils import RequiresDBTestCase, run_psql
811

912
# pylint: disable=subprocess-run-check
@@ -55,6 +58,7 @@ class DBFunctionalTestCase(RequiresDBTestCase):
5558
**env,
5659
"src_dsn": "postgresql://postgres:password@localhost/src",
5760
"dst_dsn": "postgresql://postgres:password@localhost/dst",
61+
"dst_schema": "dstschema",
5862
}
5963

6064
def setUp(self) -> None:
@@ -586,3 +590,26 @@ def test_unique_constraint_fail(self) -> None:
586590
)
587591
self.assertIn(expected_error, completed_process.stderr.decode("utf-8"))
588592
self.assertFailure(completed_process)
593+
594+
def test_create_schema(self) -> None:
595+
"""Check that we create a destination schema if it doesn't exist."""
596+
env = self.env.copy()
597+
env["dst_schema"] = "doesntexistyetschema"
598+
599+
engine = sqlalchemy.create_engine(env["dst_dsn"])
600+
inspector = inspect(engine)
601+
self.assertFalse(inspector.has_schema(env["dst_schema"]))
602+
603+
run(
604+
[
605+
"sqlsynthgen",
606+
"create-tables",
607+
f"--orm-file={self.alt_orm_file_path}",
608+
],
609+
capture_output=True,
610+
env=env,
611+
)
612+
613+
engine = sqlalchemy.create_engine(env["dst_dsn"])
614+
inspector = inspect(engine)
615+
self.assertTrue(inspector.has_schema(env["dst_schema"]))

0 commit comments

Comments
 (0)