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

SQLite offline batch migration fails despite copy_from #289

Closed
sqlalchemy-bot opened this issue Mar 27, 2015 · 3 comments
Closed

SQLite offline batch migration fails despite copy_from #289

sqlalchemy-bot opened this issue Mar 27, 2015 · 3 comments
Labels
batch migrations bug Something isn't working

Comments

@sqlalchemy-bot
Copy link

Migrated issue, originally created by Thomas Tanner (@ttanner)

continuing https://bitbucket.org/zzzeek/alembic/issue/287/sqlite-batch-migrations-missing-operations with https://bitbucket.org/zzzeek/alembic/commits/2af6e438c955 applied

I have modified the script to define the table and pass it using copy_from
according to the documentation

...
def upgrade_example():
   op.create_table('example',
    sa.Column('uuid', sa.String(length=36), nullable=False),
    sa.PrimaryKeyConstraint('uuid'),
  ...
  )
   with op.batch_alter_table('example', copy_from=Example.__table__) as batch_op:
        batch_op.create_index(batch_op.f('ix_example_uuid'), ['uuid'], unique=True)

"alembic upgrade head --sql" fails with:

#!

INFO  [alembic.env] Migrating database example
INFO  [alembic.env] Writing output to example.sql
INFO  [alembic.migration] Context impl SQLiteImpl.
INFO  [alembic.migration] Generating static SQL
INFO  [alembic.migration] Will assume non-transactional DDL.
INFO  [alembic.migration] Running upgrade  -> initial, empty message
Traceback (most recent call last):
  File "<path>/bin/alembic", line 9, in <module>
    load_entry_point('alembic==0.7.5.post2.dev0', 'console_scripts', 'alembic')()
  File "<path>/lib/python/site-packages/alembic/config.py", line 439, in main
    CommandLine(prog=prog).main(argv=argv)
  File "<path>/lib/python/site-packages/alembic/config.py", line 433, in main
    self.run_cmd(cfg, options)
  File "<path>/lib/python/site-packages/alembic/config.py", line 416, in run_cmd
    **dict((k, getattr(options, k)) for k in kwarg)
  File "<path>/lib/python/site-packages/alembic/command.py", line 165, in upgrade
    script.run_env()
  File "<path>/lib/python/site-packages/alembic/script.py", line 390, in run_env
    util.load_python_file(self.dir, 'env.py')
  File "<path>/lib/python/site-packages/alembic/util.py", line 243, in load_python_file
    module = load_module_py(module_id, path)
  File "<path>/lib/python/site-packages/alembic/compat.py", line 79, in load_module_py
    mod = imp.load_source(module_id, path, fp)
  File "alembic/env.py", line 171, in <module>
    run_migrations_offline()
  File "alembic/env.py", line 112, in run_migrations_offline
    context.run_migrations(engine_name=name)
  File "<string>", line 7, in run_migrations
  File "<path>/lib/python/site-packages/alembic/environment.py", line 742, in run_migrations
    self.get_context().run_migrations(**kw)
  File "<path>/lib/python/site-packages/alembic/migration.py", line 309, in run_migrations
    step.migration_fn(**kw)
  File "<proj>/alembic/versions/initial.py", line 23, in upgrade
    globals()["upgrade_%s" % engine_name]()
  File "<proj>/alembic/versions/initial.py", line 56, in upgrade_example
    batch_op.create_index(batch_op.f('ix_example_uuid'), ['uuid'], unique=True)
  File "<path>/lib/python2.7/contextlib.py", line 24, in __exit__
    self.gen.next()
  File "<path>/lib/python/site-packages/alembic/operations.py", line 320, in batch_alter_table
    impl.flush()
  File "<path>/lib/python/site-packages/alembic/batch.py", line 66, in flush
    *self.reflect_args, **self.reflect_kwargs)
  File "<path>/lib/python/site-packages/sqlalchemy/sql/schema.py", line 406, in __new__
    table._init(name, metadata, *args, **kw)
  File "<path>/lib/python/site-packages/sqlalchemy/sql/schema.py", line 479, in _init
    self._autoload(metadata, autoload_with, include_columns)
  File "<path>/lib/python/site-packages/sqlalchemy/sql/schema.py", line 489, in _autoload
    autoload_with.run_callable(
AttributeError: 'MockConnection' object has no attribute 'run_callable'

sqlalchemy 0.9.9, alembic 0.7.6 (master), Python 2.7.9

In the alembic source I noticed BatchOperationsImpl.copy_from is not used anywhere.

@sqlalchemy-bot
Copy link
Author

Michael Bayer (@zzzeek) wrote:

  • Fully implemented the
    :paramref:~.Operations.batch_alter_table.copy_from parameter for
    batch mode, which previously was not functioning. This allows
    "batch mode" to be usable in conjunction with --sql.
    fixes SQLite offline batch migration fails despite copy_from #289
  • sqlite dialect checks for "create_index" and "drop_index" as exceptions
    for "recreate" in batch mode, the same way as "add_column", so that
    unnecessary table recreates don't emit for index-only operations

f02f211

@sqlalchemy-bot
Copy link
Author

Changes by Michael Bayer (@zzzeek):

  • changed status to closed

@sqlalchemy-bot
Copy link
Author

Thomas Tanner (@ttanner) wrote:

Thank you for your excellent work! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
batch migrations bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants