11Creating migrations
22===================
33
4- Migrations are used to create and modify tables in the database.
4+ Migrations are Python files which are used to modify the database schema in a
5+ controlled way. Each migration belongs to a Piccolo app (see :ref: `PiccoloApps `).
56
6- .. code-block :: bash
7+ You can either manually populate migrations, or allow Piccolo to do it for you
8+ automatically. To create an empty migration:
79
8- piccolo migration new
10+ .. code-block :: bash
911
10- This creates a migrations folder, along with a migration file.
12+ piccolo migration new my_app
1113
12- The migration filename is a timestamp, which also serves as the migration ID.
14+ This creates a new migration file in the migrations folder of the app. The
15+ migration filename is a timestamp, which also serves as the migration ID.
1316
1417.. code-block :: bash
1518
16- migrations /
19+ piccolo_migrations /
1720 2018-09-04T19:44:09.py
1821
19- The contents of the migration file look like this:
22+ The contents of an empty migration file looks like this:
2023
2124.. code-block :: python
2225
@@ -28,43 +31,26 @@ The contents of the migration file look like this:
2831 async def backwards ():
2932 pass
3033
31- Populating the migration
32- ------------------------
34+ Populating an empty migration
35+ -----------------------------
3336
3437At the moment, this migration does nothing when run - we need to populate the
3538forwards and backwards functions.
3639
3740.. code-block :: python
3841
39- from piccolo.columns import Varchar
40- from piccolo.tables import Table
41-
42-
4342 ID = ' 2018-09-04T19:44:09'
4443
4544
46- class Band (Table ):
47- name = Varchar(length = 100 )
48-
49-
5045 async def forwards ():
51- await Band.create_table().run ()
46+ await run_some_sql ()
5247
5348
5449 async def backwards ():
55- await Band.alter().drop_table().run ()
50+ await run_some_other_sql ()
5651
5752 If making multiple changes to the database in a single migration, be sure to
58- wrap it in a transaction.
59-
60- .. code-block :: python
61-
62- async def forwards ():
63- async with Band._meta.db.transaction:
64- await Band.create_table().run()
65- await Manager.create_table().run()
66-
67- See :ref: `Transactions `.
53+ wrap it in a transaction. See :ref: `Transactions `.
6854
6955The golden rule
7056~~~~~~~~~~~~~~~
@@ -92,33 +78,28 @@ someone runs your migrations in the future, they will get different results.
9278Make your migrations completely independent of other code, so they're
9379self contained and repeatable.
9480
95- Running migrations
96- ------------------
97-
98- When the migration is run, the forwards function is executed. To do this:
99-
100- .. code-block :: bash
101-
102- piccolo migration forwards
81+ Auto migrations
82+ ---------------
10383
104- Inspect your database, and a ``band `` table should now exist.
84+ Manually writing your migrations gives you a good level of control, but Piccolo
85+ supports `auto migrations ` which can save a great deal of time.
10586
106- Reversing migrations
107- --------------------
87+ Piccolo will work out what tables to add by comparing previous auto migrations,
88+ and your current tables. In order for this to work, you have to register
89+ your app's tables with the `AppConfig ` in the piccolo_app.py file at the root
90+ of your app (see :ref: `PiccoloApps `).
10891
109- To reverse the migration, run this :
92+ Creating an auto migration:
11093
11194.. code-block :: bash
11295
113- piccolo migration backwards 2018-09-04T19:44:09
114-
115- This executes the backwards function.
96+ piccolo migration new my_app --auto
11697
117- You can try going forwards and backwards a few times to make sure it works as
118- expected.
119-
120- Altering tables
121- ---------------
98+ .. hint :: Auto migrations are the preferred way to create migrations with
99+ Piccolo. We recommend using `empty migrations ` for special circumstances which
100+ aren't support by auto migrations, or to modify the data held in tables, as
101+ opposed to changing the tables themselves.
122102
123- To alter tables, you'll use mostly use alter queries (see :ref: `alter `), and
124- occasionally raw queries (see :ref: `raw `).
103+ .. warning :: Auto migrations aren't supported in SQLite, because of SQLite's
104+ extremely limited support for SQL Alter statements. This might change in
105+ the future.
0 commit comments