Skip to content

Commit cfa67dc

Browse files
committed
fleshing out migration docs
1 parent 3d0efab commit cfa67dc

File tree

2 files changed

+74
-84
lines changed

2 files changed

+74
-84
lines changed

docs/src/piccolo/migrations/create.rst

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Creating migrations
22
===================
33

4-
Migrations are used to create the tables in the database.
4+
Migrations are used to create and modify tables in the database.
55

66
.. code-block:: bash
77
@@ -36,21 +36,49 @@ forwards and backwards functions.
3636

3737
.. code-block:: python
3838
39-
from ..tables import Band
39+
from piccolo.columns import Varchar
40+
from piccolo.tables import Table
41+
4042
4143
ID = '2018-09-04T19:44:09'
4244
4345
46+
class Band(Table):
47+
name = Varchar(length=100)
48+
49+
4450
async def forwards():
4551
transaction = Band._meta.db.transaction()
4652
4753
transaction.add(
48-
Band.create_without_columns(),
54+
Band.create(),
55+
)
56+
57+
await transaction.run()
58+
59+
60+
async def backwards():
61+
await Band.alter().drop_table().run()
62+
63+
The golden rule
64+
~~~~~~~~~~~~~~~
65+
66+
Never import your tables directly into a migration, and run methods on them.
67+
68+
This is a **bad example**:
69+
70+
.. code-block:: python
71+
72+
from ..tables import Band
73+
74+
ID = '2018-09-04T19:44:09'
75+
4976
50-
Band.alter().add_column(
51-
'name',
52-
Varchar(length=100)
53-
),
77+
async def forwards():
78+
transaction = Band._meta.db.transaction()
79+
80+
transaction.add(
81+
Band.create(),
5482
)
5583
5684
await transaction.run()
@@ -59,6 +87,11 @@ forwards and backwards functions.
5987
async def backwards():
6088
await Band.alter().drop_table().run()
6189
90+
The reason you don't want to do this, is your tables will change over time. If
91+
someone runs your migrations in the future, they will get different results.
92+
Make your migrations completely independent of other code, so they're
93+
self contained and repeatable.
94+
6295
Running migrations
6396
------------------
6497

@@ -83,3 +116,37 @@ This executes the backwards function.
83116

84117
You can try going forwards and backwards a few times to make sure it works as
85118
expected.
119+
120+
Altering tables
121+
---------------
122+
123+
To alter tables, you'll use mostly use alter queries (see :ref:`alter`), and
124+
occassionally raw queries (see :ref:`raw`).
125+
126+
Auto populating migrations
127+
--------------------------
128+
129+
Instead of manually populating your migrations each time, Piccolo has helpers
130+
for common use cases.
131+
132+
Creating tables
133+
~~~~~~~~~~~~~~~
134+
135+
Rather than having to copy in your table definitions manually, you can ask
136+
Piccolo to do it for you using the ``-c`` flag and passing in the import path
137+
for the table. Multiple ``-c`` flags can be used.
138+
139+
.. code-block:: bash
140+
141+
piccolo new -c ..tables.Band -c ..tables.Manager
142+
143+
Piccolo needs to be able to import these files using ``importlib``, so make
144+
sure the paths are correct.
145+
146+
Piccolo will then add the table definitions the migration.
147+
148+
To create all tables in a particular file, you can use a placeholder:
149+
150+
.. code-block:: bash
151+
152+
piccolo new -c ..tables.*

piccolo/migrations/schema.py

Lines changed: 0 additions & 77 deletions
This file was deleted.

0 commit comments

Comments
 (0)