Skip to content

Commit 4284d13

Browse files
authored
tutorial for migrating an existing project to Piccolo (#511)
1 parent 2fde648 commit 4284d13

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

docs/src/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ batteries included.
2323
piccolo/playground/index
2424
piccolo/deployment/index
2525
piccolo/ecosystem/index
26+
piccolo/tutorials/index
2627
piccolo/contributing/index
2728
piccolo/changes/index
2829
piccolo/help/index

docs/src/piccolo/tutorials/index.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Tutorials
2+
=========
3+
4+
These tutorials bring together information from across the documentation, to
5+
help you solve common problems:
6+
7+
.. toctree::
8+
:maxdepth: 1
9+
10+
./migrate_existing_project
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
Migrate an existing project to Piccolo
2+
======================================
3+
4+
Introduction
5+
------------
6+
7+
If you have an existing project and Postgres database, and you want to use
8+
Piccolo with it, these are the steps you need to take.
9+
10+
Option 1 - ``piccolo asgi new``
11+
-------------------------------
12+
13+
This is the recommended way of creating brand new projects. If this is your
14+
first experience with Piccolo, then it's a good idea to create a test project:
15+
16+
.. code-block:: bash
17+
18+
mkdir test_project
19+
cd test_project
20+
piccolo asgi new
21+
22+
You'll learn a lot about how Piccolo works by looking at the generated code.
23+
You can then copy over the relevant files to your existing project if you like.
24+
25+
Alternatively, doing it from scratch, you'll need to do the following:
26+
27+
Option 2 - from scratch
28+
-----------------------
29+
30+
Create a Piccolo project file
31+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32+
33+
Create a new ``piccolo_conf.py`` file in the root of your project:
34+
35+
.. code-block:: bash
36+
37+
piccolo project new
38+
39+
This contains your database details, and is used to register Piccolo apps.
40+
41+
Create a new Piccolo app
42+
~~~~~~~~~~~~~~~~~~~~~~~~
43+
44+
The app contains your ``Table`` classes and migrations. Run this command at the
45+
root of your project:
46+
47+
.. code-block:: bash
48+
49+
# Replace 'my_app' with whatever you want to call your app
50+
piccolo app new my_app
51+
52+
Register the new Piccolo app
53+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
54+
55+
Register this new app in ``piccolo_conf.py``. For example:
56+
57+
.. code-block:: python
58+
59+
APP_REGISTRY = AppRegistry(
60+
apps=[
61+
"my_app.piccolo_app",
62+
]
63+
)
64+
65+
While you're at it, make sure the database credentials are correct in
66+
``piccolo_conf.py``.
67+
68+
Make ``Table`` classes for your current database
69+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70+
71+
Now, if you run:
72+
73+
.. code-block:: bash
74+
75+
piccolo schema generate
76+
77+
It will output Piccolo ``Table`` classes for your current database. Copy the
78+
output into ``my_app/tables.py``. Double check that everything looks correct.
79+
80+
In ``my_app/piccolo_app.py`` make sure it's tracking these tables for
81+
migration purposes.
82+
83+
.. code-block:: python
84+
85+
from piccolo.conf.apps import AppConfig, table_finder
86+
87+
APP_CONFIG = AppConfig(
88+
table_classes=table_finder(["my_app.tables"], exclude_imported=True),
89+
...
90+
)
91+
92+
Create an initial migration
93+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
94+
95+
This will create a new file in ``my_app/piccolo_migrations``:
96+
97+
.. code-block:: bash
98+
99+
piccolo migrations new my_app --auto
100+
101+
These tables already exist in the database, as it's an existing project, so
102+
you need to fake apply this initial migration:
103+
104+
.. code-block:: bash
105+
106+
piccolo migrations forwards my_app --fake
107+
108+
Making queries
109+
~~~~~~~~~~~~~~
110+
111+
Now you're basically setup - to make database queries:
112+
113+
.. code-block:: python
114+
115+
from my_app.tables import MyTable
116+
117+
async def my_endpoint():
118+
data = await MyTable.select()
119+
return data
120+
121+
Making new migrations
122+
~~~~~~~~~~~~~~~~~~~~~
123+
124+
Just modify the files in ``tables.py``, and then run:
125+
126+
.. code-block:: bash
127+
128+
piccolo migrations new my_app --auto
129+
piccolo migrations forwards my_app

0 commit comments

Comments
 (0)