Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewriter is incompatible with multi-db scheme #505

Closed
sqlalchemy-bot opened this issue Aug 31, 2018 · 1 comment

Comments

@sqlalchemy-bot
Copy link

commented Aug 31, 2018

Migrated issue, originally created by Michael Bayer (@zzzeek)

@writer.rewrites(ops.CreateTableOp)
def create_table(context, revision, op):
    return op


def run_migrations_online():
    """Run migrations in 'online' mode.

    In this scenario we need to create an Engine
    and associate a connection with the context.

    """
    connectable = engine_from_config(
        config.get_section(config.config_ini_section),
        prefix='sqlalchemy.',
        poolclass=pool.NullPool)

    with connectable.connect() as connection:

        for schema_name in ['test_schema', 'test_schema_2']:
            conn = connection
            conn.execute("SET search_path TO %s" % schema_name)
            context.configure(
                connection=conn,
                target_metadata=target_metadata,
                upgrade_token="%s_upgrades" % schema_name,
                downgrade_token="%s_downgrades" % schema_name,
                process_revision_directives=writer,
                x_schema_name=schema_name
            )

            with context.begin_transaction():
                context.run_migrations(schema_name=schema_name)

because the upgradeops is visited multiple times, we get:

#!

File "foo/env.py", line 99, in <module>
    run_migrations_online()
  File "foo/env.py", line 94, in run_migrations_online
    context.run_migrations(schema_name=schema_name)
  File "<string>", line 8, in run_migrations
  File "/home/classic/dev/alembic/alembic/runtime/environment.py", line 836, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/home/classic/dev/alembic/alembic/runtime/migration.py", line 321, in run_migrations
    for step in self._migrations_fn(heads, self):
  File "/home/classic/dev/alembic/alembic/command.py", line 156, in retrieve_migrations
    revision_context.run_autogenerate(rev, context)
  File "/home/classic/dev/alembic/alembic/autogenerate/api.py", line 415, in run_autogenerate
    self._run_environment(rev, migration_context, True)
  File "/home/classic/dev/alembic/alembic/autogenerate/api.py", line 459, in _run_environment
    hook(migration_context, rev, self.generated_revisions)
  File "/home/classic/dev/alembic/alembic/autogenerate/rewriter.py", line 102, in __call__
    self.process_revision_directives(context, revision, directives)
  File "/home/classic/dev/alembic/alembic/autogenerate/rewriter.py", line 150, in process_revision_directives
    self._traverse_list(context, revision, directives)
  File "/home/classic/dev/alembic/alembic/autogenerate/rewriter.py", line 145, in _traverse_list
    dest.extend(self._traverse_for(context, revision, directive))
  File "/home/classic/dev/alembic/alembic/autogenerate/rewriter.py", line 139, in _traverse_for
    traverser(self, context, revision, directive)
  File "/home/classic/dev/alembic/alembic/autogenerate/rewriter.py", line 110, in _traverse_script
    ret = self._traverse_for(context, revision, directive.upgrade_ops)
  File "/home/classic/dev/alembic/alembic/operations/ops.py", line 1999, in upgrade_ops
    "This MigrationScript instance has a multiple-entry "
ValueError: This MigrationScript instance has a multiple-entry list for UpgradeOps; please use the upgrade_ops_list attribute.

not sure how we can fix this because the traversal keeps happening repeatedly.

@sqlalchemy-bot sqlalchemy-bot added this to the fasttrack milestone Nov 27, 2018
@mikeywaites mikeywaites removed this from the fasttrack milestone Jan 11, 2019
@sqla-tester

This comment has been minimized.

Copy link
Collaborator

commented Sep 19, 2019

Mike Bayer has proposed a fix for this issue in the master branch:

Memoize elements in rewriter; use correct iteration https://gerrit.sqlalchemy.org/1468

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.