Merge lp:~raj-abhilash1/mailman/alembic-fix into lp:mailman

Proposed by Abhilash Raj
Status: Merged
Merged at revision: 7291
Proposed branch: lp:~raj-abhilash1/mailman/alembic-fix
Merge into: lp:mailman
Diff against target: 74 lines (+30/-2)
2 files modified
src/mailman/database/alembic/env.py (+7/-2)
src/mailman/docs/DATABASE.rst (+23/-0)
To merge this branch: bzr merge lp:~raj-abhilash1/mailman/alembic-fix
Reviewer Review Type Date Requested Status
Mailman Coders Pending
Review via email: mp+246026@code.launchpad.net

Description of the change

Fixed the autogeneration of migrations using alembic.

To post a comment you must log in.
Revision history for this message
Barry Warsaw (barry) wrote :

Thanks for working on this. I think my problem is more basic. How do I run this - or maybe, *do* I even need to run this? For example, how would I create a migration for the pre-py3 branch merge to the current trunk, where a few columns changed from LargeBinary to Unicode?

Could you add some documentation on what needs to be done to make the migration work, say in src/mailman/docs/DATABASE.rst? Or is everything so automatic that I don't need to do anything when the database model changes? (I doubt that. :)

Revision history for this message
Abhilash Raj (raj-abhilash1) wrote :

On Monday 12 January 2015 06:11 AM, Barry Warsaw wrote:
> Thanks for working on this. I think my problem is more basic. How do I run this - or maybe, *do* I even need to run this? For example, how would I create a migration for the pre-py3 branch merge to the current trunk, where a few columns changed from LargeBinary to Unicode?

The basic command to generate migrations automatically would be like this:

   `alembic -c src/mailman/config/alembic.cfg revision --autogenerate`

What alembic does is compares the current state of database schema and
models, so in order to automatically generate the migrations you need to
have the database in the older state. Then the migrations would be
applied automatically when the mailman is started next. So when someone
updates the mailman, the database schema would be upgraded in the first
run automatically. In case you don't have the database in the old state
you generate an empty revision( just remove the --autogenerate flag in
the above command) and add the migrations yourself.

> Could you add some documentation on what needs to be done to make the migration work, say in src/mailman/docs/DATABASE.rst? Or is everything so automatic that I don't need to do anything when the database model changes? (I doubt that. :)

I will add what I wrote above in the documentation as well.

--
thanks,
Abhilash Raj

7290. By Abhilash Raj

Add how to migrate in DATABASE.rst

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/mailman/database/alembic/env.py'
--- src/mailman/database/alembic/env.py 2015-01-05 01:22:39 +0000
+++ src/mailman/database/alembic/env.py 2015-01-13 09:40:47 +0000
@@ -25,11 +25,18 @@
2525
26from alembic import context26from alembic import context
27from contextlib import closing27from contextlib import closing
28from mailman.core.initialize import initialize_1
28from mailman.config import config29from mailman.config import config
29from mailman.database.model import Model30from mailman.database.model import Model
30from mailman.utilities.string import expand31from mailman.utilities.string import expand
31from sqlalchemy import create_engine32from sqlalchemy import create_engine
3233
34try:
35 url = expand(config.database.url, config.paths)
36except AttributeError:
37 # Initialize config object for external alembic calls
38 initialize_1()
39 url = expand(config.database.url, config.paths)
3340
3441
3542
36def run_migrations_offline():43def run_migrations_offline():
@@ -42,7 +49,6 @@
42 Calls to context.execute() here emit the given string to the script49 Calls to context.execute() here emit the given string to the script
43 output.50 output.
44 """51 """
45 url = expand(config.database.url, config.paths)
46 context.configure(url=url, target_metadata=Model.metadata)52 context.configure(url=url, target_metadata=Model.metadata)
47 with context.begin_transaction():53 with context.begin_transaction():
48 context.run_migrations()54 context.run_migrations()
@@ -54,7 +60,6 @@
54 In this scenario we need to create an Engine and associate a60 In this scenario we need to create an Engine and associate a
55 connection with the context.61 connection with the context.
56 """62 """
57 url = expand(config.database.url, config.paths)
58 engine = create_engine(url)63 engine = create_engine(url)
5964
60 connection = engine.connect()65 connection = engine.connect()
6166
=== modified file 'src/mailman/docs/DATABASE.rst'
--- src/mailman/docs/DATABASE.rst 2014-11-09 12:52:58 +0000
+++ src/mailman/docs/DATABASE.rst 2015-01-13 09:40:47 +0000
@@ -59,8 +59,31 @@
59My thanks to Stephen A. Goss for his contribution of PostgreSQL support.59My thanks to Stephen A. Goss for his contribution of PostgreSQL support.
6060
6161
62Database Migrations
63===================
64
65Mailman uses `Alembic`_ to manage database migrations. Lets say you change
66something in the models, what steps are needed to reflect that change in
67the database schema? All you need to do is run this command::
68
69 alembic -c src/mailman/config/alembic.cfg revision --autogenerate
70
71This would create a new migration which would automatically be migrated to the
72database on the next run of mailman. Note that the database needs to be in the
73older state so that alembic can track the changes in the schema and autogenerate
74a migration. If you don't have the database in the older state you can remove
75the `--autogenerate` flag in the above command. It would then create a new empty
76revision which you can edit manually to reflect your changes in the database
77schema.
78
79People upgrading Mailman from previous versions need not do anything manually,
80as soon as a new migration is added in the sources, it will be automatically
81reflected in the schema on first-run post-update.
82
83
62.. _SQLAlchemy: http://www.sqlalchemy.org/84.. _SQLAlchemy: http://www.sqlalchemy.org/
63.. _SQLite3: http://docs.python.org/library/sqlite3.html85.. _SQLite3: http://docs.python.org/library/sqlite3.html
64.. _PostgreSQL: http://www.postgresql.org/86.. _PostgreSQL: http://www.postgresql.org/
65.. _MySQL: http://dev.mysql.com/87.. _MySQL: http://dev.mysql.com/
66.. _`Ubuntu article`: https://help.ubuntu.com/community/PostgreSQL88.. _`Ubuntu article`: https://help.ubuntu.com/community/PostgreSQL
89.. _`Alembic`: https://alembic.readthedocs.org/en/latest/