On Apr 29, 2012, at 10:56 AM, limodou wrote:
> On Sun, Apr 29, 2012 at 10:42 PM, Michael Bayer
> <
mik...@zzzcomputing.com> wrote:
>> You would assemble a multi-database scheme of your choosing in env.py. If you do "alembic init multidb" you'll see an example of one. How env.py is organized depends greatly on the relationship of the databases to each other, that is, to what degree they are mirrors of each other versus storing different schemas.
>>
>
> If I ran the command:
>
> alembic init multidb
>
> It'll create multidb folder and copy files in it. But I saw the
> alembic.ini will be the same one. So if I should change it myself? And
> how to let alembic know different database when executing commands
> like: revision, upgrade, etc. It seems that no database parameter
> existed.
>
> And if I can manage different databases in one directory or in one ini file?
multidb has a different alembic.ini as an example. If you already had an alembic.ini there it wouldn't overwrite it.
if you really wanted two completely independent sets of migration scripts, then you'd run two migration environments.
They can share the same alembic.ini like this:
[my_db_one]
sqlalchemy.url =
[my_db_two]
sqlalchemy.url =
you then run alembic with "alembic -n my_db_one" or "alembic -n my_db_two". The "default" config area is set by -n.
A single env.py script can get multiple database URLs in any way it wants, as it determines how config is accessed. If you look in the multidb/env.py script, you'll see it's pulling multiple database urls from one section using config.get_section(name) - config file:
[alembic]
# path to migration scripts
script_location = ${script_location}
# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s
databases = engine1, engine2
[engine1]
sqlalchemy.url = driver://user:pass@localhost/dbname
[engine2]
sqlalchemy.url = driver://user:pass@localhost/dbname2
usage:
config = context.config
db_names = config.get_main_option('databases')
for name in re.split(r',\s*', db_names):
engines[name] = rec = {}
rec['engine'] = engine_from_config(
config.get_section(name),
prefix='sqlalchemy.',
poolclass=pool.NullPool)
Over here I have both forms of multi db at the same time. There's two migration environments, and one migration environment does two databases that are largely mirrored, so three databases total. All three make use of a common env.py script that's in my application as a library, they then implement an env.py in the migration environment that draws upon the myapp/lib/env.py script for common features.
You can pass instructions to a single env.py that may be controlling multiple databases using --tag:
"alembic --tag my_tag"
"my_tag" is available in env.py as context.get_tag_argument(). You can use that to conditionally run migrations on one database or the other.
This is all DIY. Multi-database migrations can happen in many different ways so you'd need to build the approach that suits your situation best.