Skip to content

Commit 42ac8f7

Browse files
xnuinsidefantix
andauthored
added content into alembic.rst (#672)
* added content into alembic.rst * remove not needed new lines * fix styling and small changes Co-authored-by: Fantix King <[email protected]>
1 parent 3f52ff1 commit 42ac8f7

File tree

2 files changed

+133
-1
lines changed

2 files changed

+133
-1
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Contributors
4040
* Roald Storm <[email protected]>
4141
* Tiago Requeijo <[email protected]>
4242
43+
* Iuliia Volkova <[email protected]>
4344

4445

4546
Special thanks to my wife Daisy and her outsourcing company `DecentFoX Studio`_,

docs/how-to/alembic.rst

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,135 @@
11
Use Alembic
22
===========
33

4-
TODO
4+
Alembic is a lightweight database migration tool for usage with the SQLAlchemy Database
5+
Toolkit for Python. It’s also possible to use with GINO.
6+
7+
To add migrations to project first of all, add alembic as dependency:
8+
9+
.. code-block:: console
10+
11+
$ pip install --user alembic
12+
13+
When you need to set up alembic for your project.
14+
15+
Prepare sample project. We will have a structure:
16+
17+
18+
.. code-block:: console
19+
20+
alembic_sample/
21+
my_app/
22+
models.py
23+
24+
Inside ``models.py`` define simple DB Model with GINO:
25+
26+
.. code-block:: python
27+
28+
from gino import Gino
29+
30+
db = Gino()
31+
32+
class User(db.Model):
33+
__tablename__ = 'users'
34+
35+
id = db.Column(db.Integer(), primary_key=True)
36+
nickname = db.Column(db.Unicode(), default='noname')
37+
38+
39+
Set up Alembic
40+
^^^^^^^^^^^^^^
41+
42+
This will need to be done only once. Go to the main folder of your project
43+
``alembic_sample`` and run:
44+
45+
.. code-block:: console
46+
47+
$ alembic init alembic
48+
49+
50+
Alembic will create a bunch of files and folders in your project directory. One of them
51+
will be ``alembic.ini``. Open ``alembic.ini`` (you can find it in the main project
52+
folder ``alembic_sample``). Now change property ``sqlalchemy.url =`` with your DB
53+
credentials. Like this:
54+
55+
.. code-block:: ini
56+
57+
sqlalchemy.url = postgres://{{username}}:{{password}}@{{address}}/{{db_name}}
58+
59+
60+
Next go to folder ``alembic/`` and open ``env.py`` file. Inside the ``env.py`` file you
61+
need to import the ``db`` object. In our case ``db`` object is ``db`` from ``models``
62+
modules. This is a variable that links to your ``Gino()`` instance.
63+
64+
Inside ``alembic/env.py``::
65+
66+
from main_app.models import db
67+
68+
69+
And change ``target_metadata =`` to::
70+
71+
target_metadata = db
72+
73+
That’s it. We finished setting up Alembic for a project.
74+
75+
.. note::
76+
77+
All ``alembic`` commands must be run always from the folder that contains the
78+
``alembic.ini`` file.
79+
80+
81+
Create first migration revision
82+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
83+
84+
Same commands you must run each time when you make some changes in DB Models and want to
85+
apply these changes to your DB Schema.
86+
87+
.. code-block:: console
88+
89+
$ alembic revision -m "first migration" --autogenerate --head head
90+
91+
If you have any problems relative to package imports similar to this example:
92+
93+
.. code-block:: console
94+
95+
File "alembic/env.py", line 7, in <module>
96+
from main_app.models import db
97+
ModuleNotFoundError: No module named 'main_app'
98+
99+
Either install your project locally with ``pip install -e .``, ``poetry install`` or
100+
``python setup.py develop``, or add you package to PYTHONPATH, like this:
101+
102+
.. code-block:: console
103+
104+
$ export PYTHONPATH=$PYTHONPATH:/full_path/to/alembic_sample
105+
106+
After the successful run of ``alembic revision`` in folder ``alembic/versions`` you will
107+
see a file with new migration.
108+
109+
110+
Apply migration on DB
111+
^^^^^^^^^^^^^^^^^^^^^
112+
113+
Now time to apply migration to DB. It will create tables based on you DB Models.
114+
115+
.. code-block:: console
116+
117+
$ alembic upgrade head
118+
119+
Great. Now you apply your first migration. Congratulations!
120+
121+
Next time, when you will make any changes in DB models just do:
122+
123+
.. code-block:: console
124+
125+
$ alembic revision -m "your migration description" --autogenerate --head head
126+
127+
And
128+
129+
.. code-block:: console
130+
131+
alembic upgrade head
132+
133+
134+
Full documentation about how to work with Alembic migrations, downgrades and other
135+
things - you can find in official docs https://alembic.sqlalchemy.org

0 commit comments

Comments
 (0)