Re: autogenerate revision fails in a multi-db project generated with: `alembic init --template multidb alembic`

117 views
Skip to first unread message

Mike Bayer

unread,
Aug 10, 2018, 11:16:28 AM8/10/18
to sqlalchem...@googlegroups.com
On Fri, Aug 10, 2018 at 6:47 AM, <rdeb...@gmail.com> wrote:
> Hello
>
> I'm trying to use alembic to generate a migration file in a multi-db
> environment. I first created the project using `alembic init --template
> multidb alembic`
> and then hack the `env.py` script (didn't touch to `run_migration_online`)
> to suit my needs.
>
> Here is the error I got after invoking `alembic --raiseerr revision
> --autogenerate -m 'First revision`:

have you also received the alembic.ini from that run, or was there a
previous alembic.ini present that prevented the new one from being
genreated? the alembic.ini for multidb requires a "databases"
configuration:

databases = engine1, engine2

[engine1]
sqlalchemy.url = driver://user:pass@localhost/dbname

[engine2]
sqlalchemy.url = driver://user:pass@localhost/dbname2


I can reproduce your error if that section is missing. Restore it,
and also put valid URLs in there, and then it works. If this is not
how you are configuring, then you need to alter the env.py to locate
your database URLs somehow.





>
> [...]
> Generating
> /home/rdebroiz/Quantivly/services/alembic/versions/201808-10-T12-30-48-first_revision-cb8c6da27574.py
> ... FAILED
> Traceback (most recent call last):
> File
> "/home/rdebroiz/.virtualenvs/quantivly/lib/python3.6/site-packages/alembic/util/pyfiles.py",
> line 15, in template_to_file
> output = template.render_unicode(**kw).encode(output_encoding)
> File
> "/home/rdebroiz/.virtualenvs/quantivly/lib/python3.6/site-packages/mako/template.py",
> line 471, in render_unicode
> as_unicode=True)
> File
> "/home/rdebroiz/.virtualenvs/quantivly/lib/python3.6/site-packages/mako/runtime.py",
> line 838, in _render
> **_kwargs_for_callable(callable_, data))
> File
> "/home/rdebroiz/.virtualenvs/quantivly/lib/python3.6/site-packages/mako/runtime.py",
> line 873, in _render_context
> _exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
> File
> "/home/rdebroiz/.virtualenvs/quantivly/lib/python3.6/site-packages/mako/runtime.py",
> line 899, in _exec_template
> callable_(context, *args, **kwargs)
> File "alembic_script_py_mako", line 62, in render_body
> File "/home/rdebroiz/.virtualenvs/quantivly/lib/python3.6/re.py", line 212,
> in split
> return _compile(pattern, flags).split(string, maxsplit)
> TypeError: expected string or bytes-like object
>
> During handling of the above exception, another exception occurred:
>
> Traceback (most recent call last):
> File "/home/rdebroiz/.virtualenvs/quantivly/bin/alembic", line 11, in
> <module>
> sys.exit(main())
> File
> "/home/rdebroiz/.virtualenvs/quantivly/lib/python3.6/site-packages/alembic/config.py",
> line 486, in main
> CommandLine(prog=prog).main(argv=argv)
> File
> "/home/rdebroiz/.virtualenvs/quantivly/lib/python3.6/site-packages/alembic/config.py",
> line 480, in main
> self.run_cmd(cfg, options)
> File
> "/home/rdebroiz/.virtualenvs/quantivly/lib/python3.6/site-packages/alembic/config.py",
> line 463, in run_cmd
> **dict((k, getattr(options, k, None)) for k in kwarg)
> File
> "/home/rdebroiz/.virtualenvs/quantivly/lib/python3.6/site-packages/alembic/command.py",
> line 180, in revision
> revision_context.generate_scripts()
> File
> "/home/rdebroiz/.virtualenvs/quantivly/lib/python3.6/site-packages/alembic/command.py",
> line 179, in <listcomp>
> script for script in
> File
> "/home/rdebroiz/.virtualenvs/quantivly/lib/python3.6/site-packages/alembic/autogenerate/api.py",
> line 480, in generate_scripts
> yield self._to_script(generated_revision)
> File
> "/home/rdebroiz/.virtualenvs/quantivly/lib/python3.6/site-packages/alembic/autogenerate/api.py",
> line 412, in _to_script
> **template_args)
> File
> "/home/rdebroiz/.virtualenvs/quantivly/lib/python3.6/site-packages/alembic/script/base.py",
> line 574, in generate_revision
> **kw
> File
> "/home/rdebroiz/.virtualenvs/quantivly/lib/python3.6/site-packages/alembic/script/base.py",
> line 439, in _generate_template
> **kw
> File
> "/home/rdebroiz/.virtualenvs/quantivly/lib/python3.6/site-packages/alembic/util/messaging.py",
> line 47, in status
> ret = fn(*arg, **kw)
> File
> "/home/rdebroiz/.virtualenvs/quantivly/lib/python3.6/site-packages/alembic/util/pyfiles.py",
> line 24, in template_to_file
> "template-oriented traceback." % fname)
> alembic.util.exc.CommandError: Template rendering failed; see
> /tmp/tmph4dvwm5j.txt for a template-oriented traceback.
>
>
> I kept the original `script.py.mako` template:
>
> <%!
> import re
>
> %>"""${message}
>
> Revision ID: ${up_revision}
> Revises: ${down_revision | comma,n}
> Create Date: ${create_date}
>
> """
> from alembic import op
> import sqlalchemy as sa
> ${imports if imports else ""}
>
> # revision identifiers, used by Alembic.
> revision = ${repr(up_revision)}
> down_revision = ${repr(down_revision)}
> branch_labels = ${repr(branch_labels)}
> depends_on = ${repr(depends_on)}
>
>
> def upgrade(engine_name):
> globals()["upgrade_%s" % engine_name]()
>
>
> def downgrade(engine_name):
> globals()["downgrade_%s" % engine_name]()
>
> <%
> db_names = config.get_main_option("databases")
> %>
>
> ## generate an "upgrade_<xyz>() / downgrade_<xyz>()" function
> ## for each database name in the ini file.
>
> % for db_name in re.split(r',\s*', db_names):
>
> def upgrade_${db_name}():
> ${context.get("%s_upgrades" % db_name, "pass")}
>
>
> def downgrade_${db_name}():
> ${context.get("%s_downgrades" % db_name, "pass")}
>
> % endfor
>
>
>
> I tested it with alembic provided by PyPi and the bitbucket master.
> Any help would be greatly appreciate.
>
> Thanks!
> RP.
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy-alembic" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy-alem...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

René-paul Debroize

unread,
Aug 13, 2018, 9:12:20 AM8/13/18
to sqlalchem...@googlegroups.com
Hello

Thank you for your answer.

The problem was that the entry db_name was missing from the alembic.ini file. It is not how I configure the engine. However add something like:
config.set_main_option("databases", db_names)

rather than just fill the db_name var in the env.py file fixed the problem.

Thanks again!





> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "sqlalchemy-alembic" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy-alembic+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages