From fce84b2dfff6e344b03045584bb6e5c999488b55 Mon Sep 17 00:00:00 2001 From: Iain <25081046+Iain-S@users.noreply.github.com> Date: Mon, 27 Feb 2023 15:27:38 +0000 Subject: [PATCH 1/3] Add table without PK to test src database --- tests/examples/src.dump | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/examples/src.dump b/tests/examples/src.dump index 571504f7..1229b105 100644 --- a/tests/examples/src.dump +++ b/tests/examples/src.dump @@ -66,9 +66,9 @@ CREATE TABLE public.person ( stored_from timestamp with time zone NOT NULL ); - ALTER TABLE public.person OWNER TO postgres; + -- -- Name: hospital_visit; Type: TABLE; Schema: public; Owner: postgres -- @@ -84,6 +84,16 @@ CREATE TABLE public.hospital_visit ( ALTER TABLE public.hospital_visit OWNER TO postgres; +-- +-- Name: hospital_visit; Type: TABLE; Schema: public; Owner: postgres +-- + +CREATE TABLE public.no_pk_test ( + not_an_id integer NOT NULL +); + +ALTER TABLE public.no_pk_test OWNER TO postgres; + -- -- Data for Name: hospital_visit; Type: TABLE DATA; Schema: public; Owner: postgres -- From 83c192c475f92ea4252020f7a6487b15be649ec6 Mon Sep 17 00:00:00 2001 From: Iain <25081046+Iain-S@users.noreply.github.com> Date: Mon, 27 Feb 2023 16:15:26 +0000 Subject: [PATCH 2/3] Add no-PK caveat to docs --- docs/source/index.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/source/index.rst b/docs/source/index.rst index 87a87f46..b2f4cc88 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -12,6 +12,13 @@ Draft sqlsynthgen's documentation This project will be under active development from Jan - Oct 2023 + +.. note:: + + We do not currently support tables without primary keys. + If you have tables without primary keys, some sqlsynthgen functionality + may work but vocabulary tables will not. + Contents: --------- From 47ecd2afc0fefd0ac5249779e8048f34fa68d123 Mon Sep 17 00:00:00 2001 From: Iain <25081046+Iain-S@users.noreply.github.com> Date: Mon, 27 Feb 2023 15:51:03 +0000 Subject: [PATCH 3/3] Add make-tables warning for tables without a PK --- sqlsynthgen/main.py | 8 ++++++++ tests/test_main.py | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/sqlsynthgen/main.py b/sqlsynthgen/main.py index 60a294b5..c780e7b3 100644 --- a/sqlsynthgen/main.py +++ b/sqlsynthgen/main.py @@ -159,6 +159,14 @@ def make_tables() -> None: print(e.stderr, file=stderr) sys.exit(e.returncode) + # sqlacodegen falls back on Tables() for tables without PKs, + # but we don't explicitly support Tables and behaviour is unpredictable. + if " = Table(" in completed_process.stdout: + print( + "WARNING: Table without PK detected. sqlsynthgen may not be able to continue.", + file=stderr, + ) + print(completed_process.stdout) diff --git a/tests/test_main.py b/tests/test_main.py index 4d75ec9c..98289abb 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -117,6 +117,32 @@ def test_make_tables_handles_errors(self) -> None: [call.write("some-error-output"), call.write("\n")] ) + def test_make_tables_warns_no_pk(self) -> None: + """Test the make-tables sub-command warns about Tables().""" + with patch("sqlsynthgen.main.run") as mock_run, patch( + "sqlsynthgen.main.get_settings" + ) as mock_get_settings, patch("sqlsynthgen.main.stderr") as mock_stderr: + mock_get_settings.return_value = get_test_settings() + mock_run.return_value.stdout = "t_nopk_table = Table(" + + result = runner.invoke( + app, + [ + "make-tables", + ], + catch_exceptions=False, + ) + + self.assertEqual(0, result.exit_code) + mock_stderr.assert_has_calls( + [ + call.write( + "WARNING: Table without PK detected. sqlsynthgen may not be able to continue." + ), + call.write("\n"), + ] + ) + def test_make_generators(self) -> None: """Test the make-generators sub-command.""" with patch("sqlsynthgen.main.make_generators_from_tables") as mock_make: