Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
---------

Expand Down
8 changes: 8 additions & 0 deletions sqlsynthgen/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
12 changes: 11 additions & 1 deletion tests/examples/src.dump
Original file line number Diff line number Diff line change
Expand Up @@ -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
--
Expand All @@ -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
--
Expand Down
26 changes: 26 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down