Skip to content

Commit a964b95

Browse files
committed
updated migration and piccolo app documentation
1 parent 240adbc commit a964b95

File tree

15 files changed

+219
-60
lines changed

15 files changed

+219
-60
lines changed

docs/src/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Welcome to Piccolo's documentation!
99
piccolo/query_types/index
1010
piccolo/query_clauses/index
1111
piccolo/schema/index
12+
piccolo/projects_and_apps/index
1213
piccolo/engines/index
1314
piccolo/migrations/index
1415
piccolo/authentication/index
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.. _DatabaseSupport:
2+
3+
Database Support
4+
================
5+
6+
Postgres is the primary database which Piccolo was designed for.
7+
8+
Limited SQLite support is available, mostly to enable tooling like the
9+
playground. Postgres is the only database we recommend for use in production
10+
with Piccolo.

docs/src/piccolo/getting_started/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Getting Started
55
:caption: Contents:
66

77
./what_is_piccolo
8+
./database_support
89
./installing_piccolo
910
./playground
1011
./installing_postgres
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
What is Piccolo?
22
================
33

4-
Piccolo is a fast, easy to learn ORM.
4+
Piccolo is a fast, easy to learn ORM and query builder.
55

66
Some of it's stand out features are:
77

88
* Support for sync and async - see :ref:`SyncAndAsync`.
99
* A builtin playground, which makes learning a breeze - see :ref:`Playground`.
1010
* Works great with `iPython <https://ipython.org/>`_ and
1111
`VSCode <https://code.visualstudio.com/>`_ - see :ref:`tab_completion`.
12-
* Batteries included - a User model, authentication, an admin, and more.
12+
* Batteries included - a User model, authentication, migrations, an admin,
13+
and more.
Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
Creating 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

3437
At the moment, this migration does nothing when run - we need to populate the
3538
forwards 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

6955
The golden rule
7056
~~~~~~~~~~~~~~~
@@ -92,33 +78,28 @@ someone runs your migrations in the future, they will get different results.
9278
Make your migrations completely independent of other code, so they're
9379
self 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.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. _Migrations:
1+
.. _migrations:
22

33
Migrations
44
==========
@@ -7,3 +7,4 @@ Migrations
77
:maxdepth: 1
88

99
./create
10+
./running
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Running migrations
2+
==================
3+
4+
Forwards
5+
--------
6+
7+
When the migration is run, the forwards function is executed. To do this:
8+
9+
.. code-block:: bash
10+
11+
piccolo migration forwards my_app
12+
13+
Reversing migrations
14+
--------------------
15+
16+
To reverse the migration, run this:
17+
18+
.. code-block:: bash
19+
20+
piccolo migration backwards 2018-09-04T19:44:09
21+
22+
This executes the backwards function.
23+
24+
You can try going forwards and backwards a few times to make sure it works as
25+
expected.
26+
27+
.. warning:: Auto migrations don't currently support going backwards.
28+
29+
Checking migrations
30+
-------------------
31+
32+
You can easily check which migrations have amd haven't ran using the following:
33+
34+
.. code-block:: bash
35+
36+
piccolo migration check
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.. _ProjectsAndApps:
2+
3+
Projects and Apps
4+
=================
5+
6+
.. toctree::
7+
:maxdepth: 1
8+
9+
./piccolo_projects
10+
./piccolo_apps
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
.. _PiccoloApps:
2+
3+
Piccolo Apps
4+
============
5+
6+
By leveraging Piccolo apps you can unlock some useful functionality like auto
7+
migrations.
8+
9+
Creating an app
10+
---------------
11+
12+
Run the following command within your project:
13+
14+
.. code-block:: bash
15+
16+
piccolo app new my_app
17+
18+
19+
This will create a folder like this:
20+
21+
.. code-block:: bash
22+
23+
my_app/
24+
__init__.py
25+
piccolo_app.py
26+
piccolo_migrations/
27+
__init__.py
28+
tables.py
29+
30+
31+
It's important to register this app with your `piccolo_conf.py` app.
32+
33+
.. code-block:: python
34+
35+
# piccolo_conf.py
36+
APP_REGISTRY = AppRegistry(apps=['my_app.piccolo_app'])
37+
38+
39+
Anytime you invoke the `piccolo` command, you will now be able to perform
40+
operations on your app, the most important of which is :ref:`Migrations`.
41+
42+
AppConfig
43+
---------
44+
45+
Inside the `piccolo_app.py` file is an AppConfig instance. This is how you
46+
customise how your app's settings.
47+
48+
.. code-block:: python
49+
50+
import os
51+
52+
from piccolo.conf.apps import AppConfig
53+
from .tables import (
54+
Author,
55+
Post,
56+
Category,
57+
CategoryToPost,
58+
)
59+
60+
61+
CURRENT_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
62+
63+
64+
APP_CONFIG = AppConfig(
65+
app_name='blog',
66+
migrations_folder_path=os.path.join(CURRENT_DIRECTORY, 'piccolo_migrations'),
67+
table_classes=[Author, Post, Category, CategoryToPost],
68+
migration_dependencies=[],
69+
)
70+
71+
table_classes
72+
~~~~~~~~~~~~~
73+
74+
Use this to register your app's tables. This is important for auto migrations (see :ref:`Migrations`).
75+
76+
migration_dependencies
77+
~~~~~~~~~~~~~~~~~~~~~~
78+
79+
Used to specify other Piccolo apps whose migrations need to be run before the
80+
current app's migrations.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.. _PiccoloProjects:
2+
3+
Piccolo Projects
4+
================
5+
6+
A Piccolo project is a collection of apps.
7+
8+
A project requires a `piccolo_conf.py` file. To create this file, use the following command:
9+
10+
.. code-block:: bash
11+
12+
piccolo project new
13+
14+
The piccolo_conf file serves two important purposes:
15+
16+
* Contains your database settings
17+
* Is used for registering :ref:`PiccoloApps`.

0 commit comments

Comments
 (0)