|
1 | 1 | .. _cli: |
2 | 2 |
|
3 | 3 | =========== |
4 | | -TortoiseCLI |
| 4 | +Tortoise CLI |
5 | 5 | =========== |
6 | 6 |
|
7 | | -This document describes how to use `tortoise-cli`, a cli tool for tortoise-orm, build on top of click and ptpython. |
| 7 | +This page documents the built-in CLI for schema migrations and interactive use. |
8 | 8 |
|
9 | | -You can see `https://github.com/tortoise/tortoise-cli <https://github.com/tortoise/tortoise-cli>`_ for more details. |
| 9 | +Overview |
| 10 | +======== |
10 | 11 |
|
| 12 | +The CLI resolves configuration from ``-c/--config``, ``--config-file``, or |
| 13 | +``[tool.tortoise]`` in ``pyproject.toml``. Migration commands mirror the |
| 14 | +runtime API while adding plan/history output. |
11 | 15 |
|
12 | | -Quick Start |
| 16 | +Basic usage |
13 | 17 | =========== |
14 | 18 |
|
15 | 19 | .. code-block:: shell |
16 | 20 |
|
17 | | - > tortoise-cli -h 23:59:38 |
18 | | - Usage: tortoise-cli [OPTIONS] COMMAND [ARGS]... |
| 21 | + tortoise -h |
| 22 | + tortoise -c settings.TORTOISE_ORM init |
| 23 | + tortoise makemigrations |
| 24 | + tortoise migrate |
19 | 25 |
|
20 | | - Options: |
21 | | - -V, --version Show the version and exit. |
22 | | - -c, --config TEXT TortoiseORM config dictionary path, like settings.TORTOISE_ORM |
23 | | - -h, --help Show this message and exit. |
| 26 | +Configuration resolution |
| 27 | +======================== |
24 | 28 |
|
25 | | - Commands: |
26 | | - shell Start an interactive shell. |
| 29 | +You can supply configuration in one of these ways: |
27 | 30 |
|
28 | | -Usage |
29 | | -===== |
| 31 | +- ``-c/--config`` with a dotted path to a config object |
| 32 | + (for example ``settings.TORTOISE_ORM``). |
| 33 | +- ``--config-file`` with a JSON/YAML config file path. |
| 34 | +- ``pyproject.toml`` with ``[tool.tortoise]`` and a ``tortoise_orm`` key. |
30 | 35 |
|
31 | | -First, you need make a TortoiseORM config object, assuming that in `settings.py`. |
| 36 | +Commands |
| 37 | +======== |
| 38 | + |
| 39 | +init |
| 40 | +---- |
| 41 | + |
| 42 | +Create migrations packages for configured apps. This ensures each app has a |
| 43 | +``migrations`` module and the package is importable. |
| 44 | + |
| 45 | +.. code-block:: shell |
| 46 | +
|
| 47 | + tortoise init |
| 48 | + tortoise init users billing |
| 49 | +
|
| 50 | +makemigrations |
| 51 | +-------------- |
| 52 | + |
| 53 | +Autodetect model changes and create new migration files. |
32 | 54 |
|
33 | 55 | .. code-block:: shell |
34 | 56 |
|
35 | | - TORTOISE_ORM = { |
36 | | - "connections": { |
37 | | - "default": "sqlite://:memory:", |
38 | | - }, |
39 | | - "apps": { |
40 | | - "models": {"models": ["examples.models"], "default_connection": "default"}, |
41 | | - }, |
42 | | - } |
| 57 | + tortoise makemigrations |
| 58 | + tortoise makemigrations --name add_posts_table |
| 59 | + tortoise makemigrations users |
| 60 | + tortoise makemigrations --empty users |
| 61 | +
|
| 62 | +migrate / upgrade |
| 63 | +----------------- |
| 64 | + |
| 65 | +Apply migrations. ``migrate`` can move forward or backward depending on the |
| 66 | +target. ``upgrade`` is forward-only and will refuse to roll back. |
43 | 67 |
|
| 68 | +.. code-block:: shell |
| 69 | +
|
| 70 | + tortoise migrate |
| 71 | + tortoise migrate users |
| 72 | + tortoise migrate users 0002_add_field |
| 73 | + tortoise migrate users.0002_add_field |
44 | 74 |
|
45 | | -Interactive shell |
46 | | -================= |
| 75 | +downgrade |
| 76 | +--------- |
47 | 77 |
|
48 | | -Then you can start an interactive shell for TortoiseORM. |
| 78 | +Unapply migrations for a specific app. ``downgrade`` is backward-only and will |
| 79 | +refuse to apply forward migrations. If no migration name is provided, it |
| 80 | +targets the first migration for that app. |
49 | 81 |
|
50 | 82 | .. code-block:: shell |
51 | 83 |
|
52 | | - tortoise-cli -c settings.TORTOISE_ORM shell |
| 84 | + tortoise downgrade users |
| 85 | + tortoise downgrade users 0001_initial |
53 | 86 |
|
| 87 | +history |
| 88 | +------- |
54 | 89 |
|
55 | | -Or you can set config by set environment variable. |
| 90 | +List applied migrations from the database. |
56 | 91 |
|
57 | 92 | .. code-block:: shell |
58 | 93 |
|
59 | | - export TORTOISE_ORM=settings.TORTOISE_ORM |
| 94 | + tortoise history |
| 95 | + tortoise history users |
| 96 | +
|
| 97 | +heads |
| 98 | +----- |
| 99 | + |
| 100 | +List migration heads on disk. |
| 101 | + |
| 102 | +.. code-block:: shell |
| 103 | +
|
| 104 | + tortoise heads |
| 105 | + tortoise heads users |
| 106 | +
|
| 107 | +shell |
| 108 | +----- |
| 109 | + |
| 110 | +Start an interactive shell with Tortoise initialized. |
| 111 | + |
| 112 | +.. code-block:: shell |
| 113 | +
|
| 114 | + tortoise shell |
| 115 | +
|
| 116 | +Target shorthand |
| 117 | +================ |
| 118 | + |
| 119 | +The migration commands accept Django-style targets: |
60 | 120 |
|
61 | | -Then just run: |
| 121 | +- ``APP_LABEL`` means "latest" for that app. |
| 122 | +- ``APP_LABEL MIGRATION`` targets a specific migration name. |
| 123 | +- ``APP_LABEL.MIGRATION`` is equivalent to ``APP_LABEL MIGRATION`` and is |
| 124 | + convenient for copy/paste from history output. |
62 | 125 |
|
63 | 126 | .. code-block:: shell |
64 | 127 |
|
65 | | - tortoise-cli shell |
| 128 | + tortoise migrate users.__latest__ |
| 129 | + tortoise migrate users 0003_add_index |
| 130 | + tortoise downgrade users.__first__ |
0 commit comments