peewee-migrate 2.2.0__tar.gz → 2.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- peewee_migrate-2.4.0/PKG-INFO +294 -0
- peewee_migrate-2.4.0/README.md +267 -0
- {peewee_migrate-2.2.0 → peewee_migrate-2.4.0}/peewee_migrate/auto.py +60 -41
- {peewee_migrate-2.2.0 → peewee_migrate-2.4.0}/peewee_migrate/cli.py +40 -33
- {peewee_migrate-2.2.0 → peewee_migrate-2.4.0}/peewee_migrate/migrator.py +114 -63
- {peewee_migrate-2.2.0 → peewee_migrate-2.4.0}/peewee_migrate/models.py +4 -2
- {peewee_migrate-2.2.0 → peewee_migrate-2.4.0}/peewee_migrate/router.py +150 -82
- {peewee_migrate-2.2.0 → peewee_migrate-2.4.0}/peewee_migrate/template.py +3 -1
- peewee_migrate-2.4.0/peewee_migrate/types.py +21 -0
- peewee_migrate-2.4.0/peewee_migrate/utils.py +26 -0
- {peewee_migrate-2.2.0 → peewee_migrate-2.4.0}/pyproject.toml +8 -4
- {peewee_migrate-2.2.0 → peewee_migrate-2.4.0}/pyproject.toml.orig +15 -5
- peewee_migrate-2.4.0/tests/__init__.py +0 -0
- peewee_migrate-2.4.0/tests/conftest.py +71 -0
- peewee_migrate-2.4.0/tests/migrations/001_test.py +41 -0
- peewee_migrate-2.4.0/tests/migrations/002_test.py +33 -0
- peewee_migrate-2.4.0/tests/migrations/003_tespy.py +34 -0
- peewee_migrate-2.4.0/tests/migrations/conf.py +3 -0
- peewee_migrate-2.4.0/tests/mocks/__init__.py +0 -0
- peewee_migrate-2.4.0/tests/mocks/postgres.py +28 -0
- peewee_migrate-2.4.0/tests/models/__init__.py +18 -0
- peewee_migrate-2.4.0/tests/test_auto.py +547 -0
- peewee_migrate-2.4.0/tests/test_autodiscover/__init__.py +0 -0
- peewee_migrate-2.4.0/tests/test_autodiscover/models1/__init__.py +0 -0
- peewee_migrate-2.4.0/tests/test_autodiscover/models1/another_models.py +9 -0
- peewee_migrate-2.4.0/tests/test_autodiscover/models1/one_models.py +5 -0
- peewee_migrate-2.4.0/tests/test_autodiscover/models2/__init__.py +0 -0
- peewee_migrate-2.4.0/tests/test_autodiscover/models2/another_model.py +11 -0
- peewee_migrate-2.4.0/tests/test_autodiscover/models2/base_model.py +6 -0
- peewee_migrate-2.4.0/tests/test_autodiscover/models2/one_model.py +7 -0
- peewee_migrate-2.4.0/tests/test_autodiscover/models3/__init__.py +0 -0
- peewee_migrate-2.4.0/tests/test_autodiscover/models3/base_model_s.py +7 -0
- peewee_migrate-2.4.0/tests/test_autodiscover/models3/nested_referenced_model_s.py +5 -0
- peewee_migrate-2.4.0/tests/test_autodiscover/models3/referenced_model_s.py +7 -0
- peewee_migrate-2.4.0/tests/test_autodiscover/test_autodiscover.py +21 -0
- peewee_migrate-2.4.0/tests/test_cli.py +206 -0
- peewee_migrate-2.4.0/tests/test_databases.py +103 -0
- peewee_migrate-2.4.0/tests/test_migrator.py +557 -0
- peewee_migrate-2.4.0/tests/test_pg.py +14 -0
- peewee_migrate-2.4.0/tests/test_router.py +461 -0
- peewee_migrate-2.2.0/PKG-INFO +0 -211
- peewee_migrate-2.2.0/README.rst +0 -185
- peewee_migrate-2.2.0/peewee_migrate/types.py +0 -12
- peewee_migrate-2.2.0/peewee_migrate/utils.py +0 -17
- {peewee_migrate-2.2.0 → peewee_migrate-2.4.0}/peewee_migrate/__init__.py +0 -0
- {peewee_migrate-2.2.0 → peewee_migrate-2.4.0}/peewee_migrate/__main__.py +0 -0
- {peewee_migrate-2.2.0 → peewee_migrate-2.4.0}/peewee_migrate/logs.py +0 -0
- {peewee_migrate-2.2.0 → peewee_migrate-2.4.0}/peewee_migrate/py.typed +0 -0
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: peewee-migrate
|
|
3
|
+
Version: 2.4.0
|
|
4
|
+
Summary: Support for migrations in Peewee ORM
|
|
5
|
+
Keywords: peewee,migrations,orm
|
|
6
|
+
Author: Kirill Klenov
|
|
7
|
+
Author-email: Kirill Klenov <horneds@gmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
Classifier: Topic :: Utilities
|
|
21
|
+
Requires-Dist: peewee>=4.4
|
|
22
|
+
Requires-Dist: click
|
|
23
|
+
Requires-Python: >=3.11, <4
|
|
24
|
+
Project-URL: Homepage, https://github.com/klen/peewee_migrate
|
|
25
|
+
Project-URL: Repository, https://github.com/klen/peewee_migrate
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# Peewee Migrate
|
|
29
|
+
|
|
30
|
+
A simple migration engine for [Peewee](http://docs.peewee-orm.com/en/latest) ORM.
|
|
31
|
+
|
|
32
|
+
[](https://github.com/klen/peewee_migrate/actions/workflows/tests.yml)
|
|
33
|
+
[](https://github.com/klen/peewee_migrate/actions/workflows/release.yml)
|
|
34
|
+
[](https://pypi.org/project/peewee-migrate/)
|
|
35
|
+
[](https://pypi.org/project/peewee-migrate/)
|
|
36
|
+
|
|
37
|
+
## Requirements
|
|
38
|
+
|
|
39
|
+
Peewee 4.4 or newer, Python 3.11 or newer.
|
|
40
|
+
|
|
41
|
+
### Dependency note
|
|
42
|
+
|
|
43
|
+
- For `Peewee<3.0` use `Peewee-Migrate==0.14.0`
|
|
44
|
+
- For `Python 3.0-3.6` use `Peewee-Migrate==1.1.6`
|
|
45
|
+
- For `Python 3.7` use `Peewee-Migrate==1.6.6`
|
|
46
|
+
- For `Python 3.8, 3.9` use `Peewee-Migrate>=1.13.0`
|
|
47
|
+
- For `Python 3.11+` use `Peewee-Migrate>=1.14+`
|
|
48
|
+
|
|
49
|
+
## Installation
|
|
50
|
+
|
|
51
|
+
```console
|
|
52
|
+
pip install peewee-migrate
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Command line
|
|
56
|
+
|
|
57
|
+
A migration is a plain Python file with a `migrate()` function. The router keeps
|
|
58
|
+
track of applied migrations in a database table and runs each file in its own
|
|
59
|
+
transaction. The command line is the usual way to drive it.
|
|
60
|
+
|
|
61
|
+
Installation puts `pw_migrate` on the path, with `pw-migrate` as an alias.
|
|
62
|
+
`python -m peewee_migrate` runs the same commands.
|
|
63
|
+
|
|
64
|
+
```console
|
|
65
|
+
$ pw_migrate --help
|
|
66
|
+
Usage: pw_migrate [OPTIONS] COMMAND [ARGS]...
|
|
67
|
+
|
|
68
|
+
Migrate database with Peewee ORM.
|
|
69
|
+
|
|
70
|
+
Options:
|
|
71
|
+
--help Show this message and exit.
|
|
72
|
+
|
|
73
|
+
Commands:
|
|
74
|
+
create Create a migration.
|
|
75
|
+
list List migrations.
|
|
76
|
+
merge Merge migrations into one.
|
|
77
|
+
migrate Migrate database.
|
|
78
|
+
rollback Rollback migrations.
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Every command takes the same connection options:
|
|
82
|
+
|
|
83
|
+
| Option | Default | Meaning |
|
|
84
|
+
| --- | --- | --- |
|
|
85
|
+
| `--database URL` | none | Database to migrate, e.g. `postgresql://user:pass@host/db`, `sqlite:///app.db`, `mysql://user:pass@host/db`. Required unless `conf.py` provides `DATABASE`. |
|
|
86
|
+
| `--directory PATH` | `migrations` | Where migration files live and are looked up. |
|
|
87
|
+
| `--migratetable NAME` | `migratehistory` | Table that records applied migrations. |
|
|
88
|
+
| `-v` | warnings only | Increase log verbosity: `-v` info, `-vv` debug, `-vvv` everything. |
|
|
89
|
+
|
|
90
|
+
A `conf.py` inside the migrations directory can set `DATABASE`, `SCHEMA`,
|
|
91
|
+
`MIGRATE_TABLE`, `IGNORE` and `LOGGING_LEVEL`; those values win over the flags.
|
|
92
|
+
See [The migrations directory](#the-migrations-directory).
|
|
93
|
+
|
|
94
|
+
Run `pw_migrate COMMAND --help` for the exact options of one command.
|
|
95
|
+
|
|
96
|
+
### Creating a migration
|
|
97
|
+
|
|
98
|
+
```console
|
|
99
|
+
$ pw_migrate create --database=sqlite:///app.db add_user
|
|
100
|
+
$ ls migrations
|
|
101
|
+
001_add_user.py
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
The new file has an empty `migrate()` and `rollback()` for you to fill in. Add
|
|
105
|
+
`--auto` to generate the operations from your models instead:
|
|
106
|
+
|
|
107
|
+
```console
|
|
108
|
+
$ pw_migrate create --database=sqlite:///app.db --auto add_user
|
|
109
|
+
$ pw_migrate create --database=sqlite:///app.db --auto --auto-source=myapp.models add_user
|
|
110
|
+
$ pw_migrate create --database=sqlite:///app.db --auto --ignore=cache_entry add_user
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
`--auto` replays the migrations applied so far, diffs that schema against the
|
|
114
|
+
models it can import, and writes the resulting operations. With no
|
|
115
|
+
`--auto-source`, the current directory is scanned recursively for packages;
|
|
116
|
+
`--auto-source` points at one module path. Tables listed in `IGNORE` are skipped,
|
|
117
|
+
and `--ignore` adds a name to that list; repeat it for several models. When there
|
|
118
|
+
is nothing to change, no file is written and the command says `No changes found.`
|
|
119
|
+
|
|
120
|
+
### Applying migrations
|
|
121
|
+
|
|
122
|
+
```console
|
|
123
|
+
$ pw_migrate migrate --database=postgresql://user:pass@localhost/app
|
|
124
|
+
Migrating app
|
|
125
|
+
- [x] 001_add_user
|
|
126
|
+
- [x] 002_add_email
|
|
127
|
+
OK
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
`--name NAME` stops after that migration, which has to be pending:
|
|
131
|
+
|
|
132
|
+
```console
|
|
133
|
+
$ pw_migrate migrate --database=postgresql://user:pass@localhost/app --name=001_add_user
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
`--fake` writes the history rows without executing the migration SQL. Use it for
|
|
137
|
+
a schema that already exists, so the files are not applied twice.
|
|
138
|
+
|
|
139
|
+
### Rolling back
|
|
140
|
+
|
|
141
|
+
```console
|
|
142
|
+
$ pw_migrate rollback --database=postgresql://user:pass@localhost/app
|
|
143
|
+
$ pw_migrate rollback --database=postgresql://user:pass@localhost/app --count=3
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
The first undoes the last migration, the second the last three.
|
|
147
|
+
|
|
148
|
+
Only `rollback()` from the migration file is executed, so a file without one is
|
|
149
|
+
undone by dropping its history row alone. Asking for more migrations than are
|
|
150
|
+
applied is an error, not a partial rollback.
|
|
151
|
+
|
|
152
|
+
### Listing migrations
|
|
153
|
+
|
|
154
|
+
```console
|
|
155
|
+
$ pw_migrate list --database=postgresql://user:pass@localhost/app
|
|
156
|
+
List of migrations:
|
|
157
|
+
|
|
158
|
+
- [x] 001_add_user
|
|
159
|
+
- [ ] 002_add_email
|
|
160
|
+
|
|
161
|
+
Done: 1, Pending: 1
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
`[x]` is recorded in the history table, `[ ]` is still waiting on disk.
|
|
165
|
+
|
|
166
|
+
### Merging migrations
|
|
167
|
+
|
|
168
|
+
```console
|
|
169
|
+
$ pw_migrate merge --database=postgresql://user:pass@localhost/app
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Squashes the whole history into one `001_initial.py` built from the current
|
|
173
|
+
schema and drops the old files. The history table has to match the files, so run
|
|
174
|
+
`migrate` first.
|
|
175
|
+
|
|
176
|
+
Do you want Flask integration? Look at [Flask-PW](https://github.com/klen/flask-pw).
|
|
177
|
+
|
|
178
|
+
## From Python
|
|
179
|
+
|
|
180
|
+
```python
|
|
181
|
+
from peewee import SqliteDatabase
|
|
182
|
+
|
|
183
|
+
from peewee_migrate import Router
|
|
184
|
+
|
|
185
|
+
router = Router(SqliteDatabase("test.db"))
|
|
186
|
+
|
|
187
|
+
router.create("add_user") # empty 001_add_user.py
|
|
188
|
+
router.create(auto="myapp.models") # generated from your model classes
|
|
189
|
+
router.run() # apply everything pending
|
|
190
|
+
router.run("002_add_user") # apply up to the given migration
|
|
191
|
+
router.rollback() # undo the last one
|
|
192
|
+
router.merge() # squash into 001_initial.py
|
|
193
|
+
|
|
194
|
+
router.todo # names found on disk
|
|
195
|
+
router.done # names recorded in the database
|
|
196
|
+
router.diff # names still pending
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
`Router` also takes `migrate_dir`, `migrate_table`, `ignore` (table names to skip
|
|
200
|
+
when diffing models) and `schema`. `ModuleRouter` does the same for migrations
|
|
201
|
+
kept in a Python module instead of a directory. Anything the CLI does is these
|
|
202
|
+
calls, so scripts and tests can drive the same flow without a shell.
|
|
203
|
+
|
|
204
|
+
## The migrations directory
|
|
205
|
+
|
|
206
|
+
The directory defaults to `migrations` in the current working directory and is
|
|
207
|
+
created on first use. Point the CLI at another one with `--directory`, or pass
|
|
208
|
+
`migrate_dir` to the router.
|
|
209
|
+
|
|
210
|
+
Each file is named `NNN_name.py`. Only the number matters: files are applied in
|
|
211
|
+
numeric order, and the numbers do not have to be contiguous, so you can leave a
|
|
212
|
+
gap and slot a migration between two existing ones later.
|
|
213
|
+
|
|
214
|
+
A `conf.py` in that directory configures the router:
|
|
215
|
+
|
|
216
|
+
```python
|
|
217
|
+
DATABASE = "postgresql://user:pass@localhost/app"
|
|
218
|
+
SCHEMA = "public"
|
|
219
|
+
MIGRATE_TABLE = "migratehistory"
|
|
220
|
+
IGNORE = ["cache_entry"]
|
|
221
|
+
LOGGING_LEVEL = "INFO"
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Values from `conf.py` win over the command line flags. Errors raised inside it
|
|
225
|
+
are not swallowed.
|
|
226
|
+
|
|
227
|
+
## How it works
|
|
228
|
+
|
|
229
|
+
Applied migrations are recorded in the `migratehistory` table (rename it with
|
|
230
|
+
`--migratetable`). The name is unique there. Each migration runs inside a
|
|
231
|
+
transaction and its history row is written in the same transaction, so a
|
|
232
|
+
migration that fails leaves neither schema changes nor a history row behind. On
|
|
233
|
+
MySQL the DDL part is not covered, see Limitations.
|
|
234
|
+
|
|
235
|
+
Several processes may start migrating at once. On Postgres the router takes a
|
|
236
|
+
`pg_advisory_lock`, on MySQL a `GET_LOCK`, both keyed by the history table, and
|
|
237
|
+
the second process waits up to 60 seconds for the first one to finish before it
|
|
238
|
+
gives up with an error.
|
|
239
|
+
|
|
240
|
+
## Migration files
|
|
241
|
+
|
|
242
|
+
Each file must define `migrate()` and may define `rollback()`:
|
|
243
|
+
|
|
244
|
+
```python
|
|
245
|
+
def migrate(migrator, database, fake=False, **kwargs):
|
|
246
|
+
"""Write your migrations here."""
|
|
247
|
+
...
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def rollback(migrator, database, fake=False, **kwargs):
|
|
251
|
+
"""Write your rollback migrations here."""
|
|
252
|
+
...
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
The `migrator` argument carries the operations: `create_model`, `remove_model`,
|
|
256
|
+
`add_fields`, `change_fields`, `remove_fields`, `rename_field`, `rename_table`,
|
|
257
|
+
`add_index`, `drop_index`, `add_not_null`, `drop_not_null`, `add_default`,
|
|
258
|
+
`add_constraint`, `drop_constraints`, plus `sql()` for raw SQL and `run()` for
|
|
259
|
+
arbitrary Python. A generated file lists them in its docstring.
|
|
260
|
+
|
|
261
|
+
`migrator.orm["table_name"]` returns the model as it exists at that point in the
|
|
262
|
+
history, which is what data migrations usually need.
|
|
263
|
+
|
|
264
|
+
A migration without `rollback()` counts as an empty rollback: the command
|
|
265
|
+
removes its history row and leaves the schema alone. If you want a real
|
|
266
|
+
undoing, write the inverse operations.
|
|
267
|
+
|
|
268
|
+
A model created with `migrator.create_model()` can only be changed in a later
|
|
269
|
+
migration. Create it in one file and alter it in the next one; the same file
|
|
270
|
+
would try to add a column that the new table already has.
|
|
271
|
+
|
|
272
|
+
## Limitations
|
|
273
|
+
|
|
274
|
+
- Sqlite has no advisory lock, so concurrent processes are not serialised
|
|
275
|
+
there. The unique history name turns a double apply into an error instead of a
|
|
276
|
+
silent duplicate. The constraint is part of newly created history tables; an
|
|
277
|
+
existing `migratehistory` table keeps its old definition.
|
|
278
|
+
- MySQL commits DDL implicitly, so a migration that fails halfway can leave
|
|
279
|
+
partial schema changes behind.
|
|
280
|
+
- Applied migrations are not checksummed. Editing a file that already ran
|
|
281
|
+
changes nothing in the database and is not reported.
|
|
282
|
+
- On Sqlite, changing a column rebuilds the table, which is slow on large
|
|
283
|
+
tables.
|
|
284
|
+
- One router serves one database. Two databases in one process means two
|
|
285
|
+
routers, which is supported; sharing a single router between them is not.
|
|
286
|
+
|
|
287
|
+
## Bug tracker
|
|
288
|
+
|
|
289
|
+
If you have any suggestions, bug reports or annoyances, please report them at
|
|
290
|
+
https://github.com/klen/peewee_migrate/issues.
|
|
291
|
+
|
|
292
|
+
## Contributing
|
|
293
|
+
|
|
294
|
+
Development happens at GitHub: https://github.com/klen/peewee_migrate
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
# Peewee Migrate
|
|
2
|
+
|
|
3
|
+
A simple migration engine for [Peewee](http://docs.peewee-orm.com/en/latest) ORM.
|
|
4
|
+
|
|
5
|
+
[](https://github.com/klen/peewee_migrate/actions/workflows/tests.yml)
|
|
6
|
+
[](https://github.com/klen/peewee_migrate/actions/workflows/release.yml)
|
|
7
|
+
[](https://pypi.org/project/peewee-migrate/)
|
|
8
|
+
[](https://pypi.org/project/peewee-migrate/)
|
|
9
|
+
|
|
10
|
+
## Requirements
|
|
11
|
+
|
|
12
|
+
Peewee 4.4 or newer, Python 3.11 or newer.
|
|
13
|
+
|
|
14
|
+
### Dependency note
|
|
15
|
+
|
|
16
|
+
- For `Peewee<3.0` use `Peewee-Migrate==0.14.0`
|
|
17
|
+
- For `Python 3.0-3.6` use `Peewee-Migrate==1.1.6`
|
|
18
|
+
- For `Python 3.7` use `Peewee-Migrate==1.6.6`
|
|
19
|
+
- For `Python 3.8, 3.9` use `Peewee-Migrate>=1.13.0`
|
|
20
|
+
- For `Python 3.11+` use `Peewee-Migrate>=1.14+`
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```console
|
|
25
|
+
pip install peewee-migrate
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Command line
|
|
29
|
+
|
|
30
|
+
A migration is a plain Python file with a `migrate()` function. The router keeps
|
|
31
|
+
track of applied migrations in a database table and runs each file in its own
|
|
32
|
+
transaction. The command line is the usual way to drive it.
|
|
33
|
+
|
|
34
|
+
Installation puts `pw_migrate` on the path, with `pw-migrate` as an alias.
|
|
35
|
+
`python -m peewee_migrate` runs the same commands.
|
|
36
|
+
|
|
37
|
+
```console
|
|
38
|
+
$ pw_migrate --help
|
|
39
|
+
Usage: pw_migrate [OPTIONS] COMMAND [ARGS]...
|
|
40
|
+
|
|
41
|
+
Migrate database with Peewee ORM.
|
|
42
|
+
|
|
43
|
+
Options:
|
|
44
|
+
--help Show this message and exit.
|
|
45
|
+
|
|
46
|
+
Commands:
|
|
47
|
+
create Create a migration.
|
|
48
|
+
list List migrations.
|
|
49
|
+
merge Merge migrations into one.
|
|
50
|
+
migrate Migrate database.
|
|
51
|
+
rollback Rollback migrations.
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Every command takes the same connection options:
|
|
55
|
+
|
|
56
|
+
| Option | Default | Meaning |
|
|
57
|
+
| --- | --- | --- |
|
|
58
|
+
| `--database URL` | none | Database to migrate, e.g. `postgresql://user:pass@host/db`, `sqlite:///app.db`, `mysql://user:pass@host/db`. Required unless `conf.py` provides `DATABASE`. |
|
|
59
|
+
| `--directory PATH` | `migrations` | Where migration files live and are looked up. |
|
|
60
|
+
| `--migratetable NAME` | `migratehistory` | Table that records applied migrations. |
|
|
61
|
+
| `-v` | warnings only | Increase log verbosity: `-v` info, `-vv` debug, `-vvv` everything. |
|
|
62
|
+
|
|
63
|
+
A `conf.py` inside the migrations directory can set `DATABASE`, `SCHEMA`,
|
|
64
|
+
`MIGRATE_TABLE`, `IGNORE` and `LOGGING_LEVEL`; those values win over the flags.
|
|
65
|
+
See [The migrations directory](#the-migrations-directory).
|
|
66
|
+
|
|
67
|
+
Run `pw_migrate COMMAND --help` for the exact options of one command.
|
|
68
|
+
|
|
69
|
+
### Creating a migration
|
|
70
|
+
|
|
71
|
+
```console
|
|
72
|
+
$ pw_migrate create --database=sqlite:///app.db add_user
|
|
73
|
+
$ ls migrations
|
|
74
|
+
001_add_user.py
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
The new file has an empty `migrate()` and `rollback()` for you to fill in. Add
|
|
78
|
+
`--auto` to generate the operations from your models instead:
|
|
79
|
+
|
|
80
|
+
```console
|
|
81
|
+
$ pw_migrate create --database=sqlite:///app.db --auto add_user
|
|
82
|
+
$ pw_migrate create --database=sqlite:///app.db --auto --auto-source=myapp.models add_user
|
|
83
|
+
$ pw_migrate create --database=sqlite:///app.db --auto --ignore=cache_entry add_user
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
`--auto` replays the migrations applied so far, diffs that schema against the
|
|
87
|
+
models it can import, and writes the resulting operations. With no
|
|
88
|
+
`--auto-source`, the current directory is scanned recursively for packages;
|
|
89
|
+
`--auto-source` points at one module path. Tables listed in `IGNORE` are skipped,
|
|
90
|
+
and `--ignore` adds a name to that list; repeat it for several models. When there
|
|
91
|
+
is nothing to change, no file is written and the command says `No changes found.`
|
|
92
|
+
|
|
93
|
+
### Applying migrations
|
|
94
|
+
|
|
95
|
+
```console
|
|
96
|
+
$ pw_migrate migrate --database=postgresql://user:pass@localhost/app
|
|
97
|
+
Migrating app
|
|
98
|
+
- [x] 001_add_user
|
|
99
|
+
- [x] 002_add_email
|
|
100
|
+
OK
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
`--name NAME` stops after that migration, which has to be pending:
|
|
104
|
+
|
|
105
|
+
```console
|
|
106
|
+
$ pw_migrate migrate --database=postgresql://user:pass@localhost/app --name=001_add_user
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
`--fake` writes the history rows without executing the migration SQL. Use it for
|
|
110
|
+
a schema that already exists, so the files are not applied twice.
|
|
111
|
+
|
|
112
|
+
### Rolling back
|
|
113
|
+
|
|
114
|
+
```console
|
|
115
|
+
$ pw_migrate rollback --database=postgresql://user:pass@localhost/app
|
|
116
|
+
$ pw_migrate rollback --database=postgresql://user:pass@localhost/app --count=3
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
The first undoes the last migration, the second the last three.
|
|
120
|
+
|
|
121
|
+
Only `rollback()` from the migration file is executed, so a file without one is
|
|
122
|
+
undone by dropping its history row alone. Asking for more migrations than are
|
|
123
|
+
applied is an error, not a partial rollback.
|
|
124
|
+
|
|
125
|
+
### Listing migrations
|
|
126
|
+
|
|
127
|
+
```console
|
|
128
|
+
$ pw_migrate list --database=postgresql://user:pass@localhost/app
|
|
129
|
+
List of migrations:
|
|
130
|
+
|
|
131
|
+
- [x] 001_add_user
|
|
132
|
+
- [ ] 002_add_email
|
|
133
|
+
|
|
134
|
+
Done: 1, Pending: 1
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
`[x]` is recorded in the history table, `[ ]` is still waiting on disk.
|
|
138
|
+
|
|
139
|
+
### Merging migrations
|
|
140
|
+
|
|
141
|
+
```console
|
|
142
|
+
$ pw_migrate merge --database=postgresql://user:pass@localhost/app
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Squashes the whole history into one `001_initial.py` built from the current
|
|
146
|
+
schema and drops the old files. The history table has to match the files, so run
|
|
147
|
+
`migrate` first.
|
|
148
|
+
|
|
149
|
+
Do you want Flask integration? Look at [Flask-PW](https://github.com/klen/flask-pw).
|
|
150
|
+
|
|
151
|
+
## From Python
|
|
152
|
+
|
|
153
|
+
```python
|
|
154
|
+
from peewee import SqliteDatabase
|
|
155
|
+
|
|
156
|
+
from peewee_migrate import Router
|
|
157
|
+
|
|
158
|
+
router = Router(SqliteDatabase("test.db"))
|
|
159
|
+
|
|
160
|
+
router.create("add_user") # empty 001_add_user.py
|
|
161
|
+
router.create(auto="myapp.models") # generated from your model classes
|
|
162
|
+
router.run() # apply everything pending
|
|
163
|
+
router.run("002_add_user") # apply up to the given migration
|
|
164
|
+
router.rollback() # undo the last one
|
|
165
|
+
router.merge() # squash into 001_initial.py
|
|
166
|
+
|
|
167
|
+
router.todo # names found on disk
|
|
168
|
+
router.done # names recorded in the database
|
|
169
|
+
router.diff # names still pending
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
`Router` also takes `migrate_dir`, `migrate_table`, `ignore` (table names to skip
|
|
173
|
+
when diffing models) and `schema`. `ModuleRouter` does the same for migrations
|
|
174
|
+
kept in a Python module instead of a directory. Anything the CLI does is these
|
|
175
|
+
calls, so scripts and tests can drive the same flow without a shell.
|
|
176
|
+
|
|
177
|
+
## The migrations directory
|
|
178
|
+
|
|
179
|
+
The directory defaults to `migrations` in the current working directory and is
|
|
180
|
+
created on first use. Point the CLI at another one with `--directory`, or pass
|
|
181
|
+
`migrate_dir` to the router.
|
|
182
|
+
|
|
183
|
+
Each file is named `NNN_name.py`. Only the number matters: files are applied in
|
|
184
|
+
numeric order, and the numbers do not have to be contiguous, so you can leave a
|
|
185
|
+
gap and slot a migration between two existing ones later.
|
|
186
|
+
|
|
187
|
+
A `conf.py` in that directory configures the router:
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
DATABASE = "postgresql://user:pass@localhost/app"
|
|
191
|
+
SCHEMA = "public"
|
|
192
|
+
MIGRATE_TABLE = "migratehistory"
|
|
193
|
+
IGNORE = ["cache_entry"]
|
|
194
|
+
LOGGING_LEVEL = "INFO"
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
Values from `conf.py` win over the command line flags. Errors raised inside it
|
|
198
|
+
are not swallowed.
|
|
199
|
+
|
|
200
|
+
## How it works
|
|
201
|
+
|
|
202
|
+
Applied migrations are recorded in the `migratehistory` table (rename it with
|
|
203
|
+
`--migratetable`). The name is unique there. Each migration runs inside a
|
|
204
|
+
transaction and its history row is written in the same transaction, so a
|
|
205
|
+
migration that fails leaves neither schema changes nor a history row behind. On
|
|
206
|
+
MySQL the DDL part is not covered, see Limitations.
|
|
207
|
+
|
|
208
|
+
Several processes may start migrating at once. On Postgres the router takes a
|
|
209
|
+
`pg_advisory_lock`, on MySQL a `GET_LOCK`, both keyed by the history table, and
|
|
210
|
+
the second process waits up to 60 seconds for the first one to finish before it
|
|
211
|
+
gives up with an error.
|
|
212
|
+
|
|
213
|
+
## Migration files
|
|
214
|
+
|
|
215
|
+
Each file must define `migrate()` and may define `rollback()`:
|
|
216
|
+
|
|
217
|
+
```python
|
|
218
|
+
def migrate(migrator, database, fake=False, **kwargs):
|
|
219
|
+
"""Write your migrations here."""
|
|
220
|
+
...
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def rollback(migrator, database, fake=False, **kwargs):
|
|
224
|
+
"""Write your rollback migrations here."""
|
|
225
|
+
...
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
The `migrator` argument carries the operations: `create_model`, `remove_model`,
|
|
229
|
+
`add_fields`, `change_fields`, `remove_fields`, `rename_field`, `rename_table`,
|
|
230
|
+
`add_index`, `drop_index`, `add_not_null`, `drop_not_null`, `add_default`,
|
|
231
|
+
`add_constraint`, `drop_constraints`, plus `sql()` for raw SQL and `run()` for
|
|
232
|
+
arbitrary Python. A generated file lists them in its docstring.
|
|
233
|
+
|
|
234
|
+
`migrator.orm["table_name"]` returns the model as it exists at that point in the
|
|
235
|
+
history, which is what data migrations usually need.
|
|
236
|
+
|
|
237
|
+
A migration without `rollback()` counts as an empty rollback: the command
|
|
238
|
+
removes its history row and leaves the schema alone. If you want a real
|
|
239
|
+
undoing, write the inverse operations.
|
|
240
|
+
|
|
241
|
+
A model created with `migrator.create_model()` can only be changed in a later
|
|
242
|
+
migration. Create it in one file and alter it in the next one; the same file
|
|
243
|
+
would try to add a column that the new table already has.
|
|
244
|
+
|
|
245
|
+
## Limitations
|
|
246
|
+
|
|
247
|
+
- Sqlite has no advisory lock, so concurrent processes are not serialised
|
|
248
|
+
there. The unique history name turns a double apply into an error instead of a
|
|
249
|
+
silent duplicate. The constraint is part of newly created history tables; an
|
|
250
|
+
existing `migratehistory` table keeps its old definition.
|
|
251
|
+
- MySQL commits DDL implicitly, so a migration that fails halfway can leave
|
|
252
|
+
partial schema changes behind.
|
|
253
|
+
- Applied migrations are not checksummed. Editing a file that already ran
|
|
254
|
+
changes nothing in the database and is not reported.
|
|
255
|
+
- On Sqlite, changing a column rebuilds the table, which is slow on large
|
|
256
|
+
tables.
|
|
257
|
+
- One router serves one database. Two databases in one process means two
|
|
258
|
+
routers, which is supported; sharing a single router between them is not.
|
|
259
|
+
|
|
260
|
+
## Bug tracker
|
|
261
|
+
|
|
262
|
+
If you have any suggestions, bug reports or annoyances, please report them at
|
|
263
|
+
https://github.com/klen/peewee_migrate/issues.
|
|
264
|
+
|
|
265
|
+
## Contributing
|
|
266
|
+
|
|
267
|
+
Development happens at GitHub: https://github.com/klen/peewee_migrate
|