]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
- add test coverage for standalone MigrationContext / Operations
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 30 Jan 2012 17:31:08 +0000 (12:31 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 30 Jan 2012 17:31:08 +0000 (12:31 -0500)
- ensure MigrationContext.configure can be used with a single connection
argument

alembic/migration.py
tests/__init__.py
tests/test_config.py

index 2e817e6412a57b88e52e12ec77c57ed2af5d9266..544942cd3f091e9a5b2e595df2ca4f285c7c2435 100644 (file)
@@ -28,6 +28,18 @@ class MigrationContext(object):
     
         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):
@@ -69,7 +81,7 @@ class MigrationContext(object):
                 connection=None,
                 url=None,
                 dialect_name=None,
-                opts=None,
+                opts={},
     ):
         """Create a new :class:`.MigrationContext`.
         
index bb06ae7f0c7a49f249b8ae5b58cc14842c35f848..e8baba875c7e6ed2116592e968640cd449856743 100644 (file)
@@ -35,6 +35,13 @@ def sqlite_db():
     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:
index fb54e8aa97bec770b66c24b6a92166857a9d33e9..f0bd16743035a98a000d5a606aa868cb379258df 100644 (file)
@@ -1,5 +1,7 @@
 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()
@@ -15,4 +17,14 @@ def test_config_no_file_section_option():
     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'])