##// END OF EJS Templates
docs: document how to use Alembic for database migrations
Søren Løvborg -
r5986:9fd64dd2 default
parent child Browse files
Show More
@@ -0,0 +1,74 b''
1 =======================
2 Database schema changes
3 =======================
4
5 Kallithea uses Alembic for :ref:`database migrations <upgrade_db>`
6 (upgrades and downgrades).
7
8 If you are developing a Kallithea feature that requires database schema
9 changes, you should make a matching Alembic database migration script:
10
11 1. :ref:`Create a Kallithea configuration and database <setup>` for testing
12 the migration script, or use existing ``development.ini`` setup.
13
14 Ensure that this database is up to date with the latest database
15 schema *before* the changes you're currently developing. (Do not
16 create the database while your new schema changes are applied.)
17
18 2. Create a separate throwaway configuration for iterating on the actual
19 database changes::
20
21 paster make-config Kallithea temp.ini
22
23 Edit the file to change database settings. SQLite is typically fine,
24 but make sure to change the path to e.g. ``temp.db``, to avoid
25 clobbering any existing database file.
26
27 3. Make your code changes (including database schema changes in ``db.py``).
28
29 4. After every database schema change, recreate the throwaway database
30 to test the changes::
31
32 rm temp.db
33 paster setup-db temp.ini --repos=/var/repos --user=doe --email doe@example.com --password=123456 --no-public-access --force-yes
34 paster repo-scan temp.ini
35
36 5. Once satisfied with the schema changes, auto-generate a draft Alembic
37 script using the development database that has *not* been upgraded.
38 (The generated script will upgrade the database to match the code.)
39
40 ::
41
42 alembic -c development.ini revision -m "area: add cool feature" --autogenerate
43
44 6. Edit the script to clean it up and fix any problems.
45
46 Note that for changes that simply add columns, it may be appropriate
47 to not remove them in the downgrade script (and instead do nothing),
48 to avoid the loss of data. Unknown columns will simply be ignored by
49 Kallithea versions predating your changes.
50
51 7. Run ``alembic -c development.ini upgrade head`` to apply changes to
52 the (non-throwaway) database, and test the upgrade script. Also test
53 downgrades.
54
55 The included ``development.ini`` has full SQL logging enabled. If
56 you're using another configuration file, you may want to enable it
57 by setting ``level = DEBUG`` in section ``[handler_console_sql]``.
58
59 The Alembic migration script should be committed in the same revision as
60 the database schema (``db.py``) changes.
61
62 See the `Alembic documentation`__ for more information, in particular
63 the tutorial and the section about auto-generating migration scripts.
64
65 .. __: http://alembic.zzzcomputing.com/en/latest/
66
67
68 Troubleshooting
69 ---------------
70
71 * If ``alembic --autogenerate`` responds "Target database is not up to
72 date", you need to either first use Alembic to upgrade the database
73 to the most recent version (before your changes), or recreate the
74 database from scratch (without your schema changes applied).
@@ -54,6 +54,7 b' Kallithea Documentation'
54
54
55 contributing
55 contributing
56 dev/translation
56 dev/translation
57 dev/dbmigrations
57 changelog
58 changelog
58
59
59 **API**
60 **API**
@@ -103,11 +103,64 b' that you check the contents after the me'
103 Please always make sure your ``.ini`` files are up to date. Errors
103 Please always make sure your ``.ini`` files are up to date. Errors
104 can often be caused by missing parameters added in new versions.
104 can often be caused by missing parameters added in new versions.
105
105
106 .. _upgrade_db:
107
106
108
107 6. Upgrade your database
109 6. Upgrade your database
108 ------------------------
110 ------------------------
109
111
110 Not required.
112 .. note::
113 If you are *downgrading* Kallithea, you should perform the database
114 migration step *before* installing the older version. (That is,
115 always perform migrations using the most recent of the two versions
116 you're migrating between.)
117
118 First, run the following command to see your current database version::
119
120 alembic -c my.ini current
121
122 Typical output will be something like "9358dc3d6828 (head)", which is
123 the current Alembic database "revision ID". Write down the entire output
124 for troubleshooting purposes.
125
126 The output will be empty if you're upgrading from Kallithea 0.3.x or
127 older. That's expected. If you get an error that the config file was not
128 found or has no ``[alembic]`` section, see the next section.
129
130 Next, if you are performing an *upgrade*: Run the following command to
131 upgrade your database to the current Kallithea version::
132
133 alembic -c my.ini upgrade head
134
135 If you are performing a *downgrade*: Run the following command to
136 downgrade your database to the given version::
137
138 alembic -c my.ini downgrade 0.4
139
140 Alembic will show the necessary migrations (if any) as it executes them.
141 If no "ERROR" is displayed, the command was successful.
142
143 Should an error occur, the database may be "stranded" half-way
144 through the migration, and you should restore it from backup.
145
146 Enabling old Kallithea config files for Alembic use
147 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
148
149 Kallithea configuration files created before the introduction of Alembic
150 (i.e. predating Kallithea 0.4) need to be updated for use with Alembic.
151 Without this, Alembic will fail with an error like this::
152
153 FAILED: No config file 'my.ini' found, or file has no '[alembic]' section
154
155 If Alembic complains specifically about a missing ``alembic.ini``, it is
156 likely because you did not specify a config file using the ``-c`` option.
157 On the other hand, if the mentioned config file actually exists, you
158 need to append the following lines to it::
159
160 [alembic]
161 script_location = kallithea:alembic
162
163 Your config file should now work with Alembic.
111
164
112
165
113 7. Rebuild the Whoosh full-text index
166 7. Rebuild the Whoosh full-text index
@@ -5,4 +5,7 b' class UpgradeDb(Command):'
5 summary = '(removed)'
5 summary = '(removed)'
6
6
7 def run(self, args):
7 def run(self, args):
8 raise SystemExit('The "paster upgrade-db" command has been removed.')
8 raise SystemExit(
9 'The "paster upgrade-db" command has been removed; please see the docs:\n'
10 ' https://kallithea.readthedocs.io/en/default/upgrade.html'
11 )
General Comments 0
You need to be logged in to leave comments. Login now