from alembic import context
migration_context = context.migration_context
+
+ A :class:`.MigrationContext` can be created programmatically
+ for usage outside of the usual Alembic migrations flow,
+ using the :meth:`.MigrationContext.configure` method::
+
+ conn = myengine.connect()
+ ctx = MigrationContext.configure(conn)
+
+ The above context can then be used to produce
+ Alembic migration operations with an :class:`.Operations`
+ instance.
+
"""
def __init__(self, dialect, connection, opts):
connection=None,
url=None,
dialect_name=None,
- opts=None,
+ opts={},
):
"""Create a new :class:`.MigrationContext`.
dir_ = os.path.join(staging_directory, 'scripts')
return create_engine('sqlite:///%s/foo.db' % dir_)
+def capture_db():
+ buf = []
+ def dump(sql, *multiparams, **params):
+ buf.append(str(sql.compile(dialect=engine.dialect)))
+ engine = create_engine("postgresql://", strategy="mock", executor=dump)
+ return engine, buf
+
_engs = {}
def db_for_dialect(name):
if name in _engs:
from alembic import config
-from tests import eq_
+from alembic.migration import MigrationContext
+from alembic.operations import Operations
+from tests import eq_, capture_db
def test_config_no_file_main_option():
cfg = config.Config()
eq_(cfg.get_section_option("foo", "url"), "postgresql://foo/bar")
cfg.set_section_option("foo", "echo", "True")
- eq_(cfg.get_section_option("foo", "echo"), "True")
\ No newline at end of file
+ eq_(cfg.get_section_option("foo", "echo"), "True")
+
+
+def test_standalone_op():
+ eng, buf = capture_db()
+
+ env = MigrationContext.configure(eng)
+ op = Operations(env)
+
+ op.alter_column("t", "c", nullable=True)
+ eq_(buf, ['ALTER TABLE t ALTER COLUMN c DROP NOT NULL'])