Skip to content

Commit d1d6300

Browse files
authored
initial prototype for auto schema generation (piccolo-orm#157)
* initial prototype for auto schema generation * make sure the correct column type is being instantiated * use black to nicely format the string * add Timestamptz * detect if column is nullable * introspect Varchar length * slight refactor, and output required imports * added initial tests * fix tests * remove some kwargs from output * fix bug with duplicate id columns * add extra imports if foreign keys are present * add docs * fixing a bug with primary key detection * add test for unique constraints * log the db version instead of printing to avoid adding noise to output Otherwise if someone pipes `piccolo schema generate` straight to a file, it won't be valid Python as it'll have something like 'Running Postgres version 9.6' at the top. * increase code coverage * add a link from the schema docs to the `piccolo schema generate` docs
1 parent 3218432 commit d1d6300

File tree

14 files changed

+575
-2
lines changed

14 files changed

+575
-2
lines changed

docs/src/piccolo/projects_and_apps/included_apps.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,23 @@ Lets you create a new ``piccolo_conf.py`` file. See :ref:`PiccoloProjects`.
6060
6161
piccolo project new
6262
63+
.. _SchemaApp:
64+
65+
schema
66+
~~~~~~
67+
68+
Lets you auto generate Piccolo ``Table`` classes from an existing database.
69+
Make sure the credentials in ``piccolo_conf.py`` are for the database you're
70+
interested in, then run the following:
71+
72+
.. code-block:: bash
73+
74+
piccolo schema generate > tables.py
75+
76+
.. warning:: This feature is still a work in progress. However, even in it's
77+
current form it will save you a lot of time. Make sure you check the
78+
generated code to make sure it's correct.
79+
6380
shell
6481
~~~~~
6582

docs/src/piccolo/schema/defining.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ columns. Here's a very simple schema:
2121
2222
For a full list of columns, see :ref:`ColumnTypes`.
2323

24+
.. hint:: If you're using an existing database, see Piccolo's
25+
:ref:`auto schema generation command<SchemaApp>`, which will save you some
26+
time.
27+
2428
-------------------------------------------------------------------------------
2529

2630
Primary Key

piccolo/apps/schema/__init__.py

Whitespace-only changes.

piccolo/apps/schema/commands/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)