]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
beginning to lay out migration flow
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 28 Apr 2010 21:47:01 +0000 (17:47 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 28 Apr 2010 21:47:01 +0000 (17:47 -0400)
alembic/command.py
alembic/context.py
alembic/script.py
templates/generic/env.py
templates/multidb/env.py
templates/pylons/env.py

index 73e1db860bac2ce1e8538da0f04ce2c9ed2e7e00..57c8072e2481487fec398dd1ed1d87bd6c939f5b 100644 (file)
@@ -1,8 +1,7 @@
 from alembic.script import ScriptDirectory
-from alembic import options, util
+from alembic import util
 import os
-import sys
-import uuid
+import functools
 
 def list_templates(config):
     """List available templates"""
@@ -70,22 +69,22 @@ def upgrade(config):
     """Upgrade to the latest version."""
 
     script = ScriptDirectory.from_config(config)
+    context._migration_fn = script.upgrade_from
+    script.run_env()
     
-    # ...
-    
-def revert(config):
+def revert(config, revision):
     """Revert to a specific previous version."""
     
     script = ScriptDirectory.from_config(config)
-
-    # ...
+    context._migration_fn = functools.partial(script.downgrade_to, revision)
+    script.run_env()
 
 def history(config):
     """List changeset scripts in chronological order."""
 
     script = ScriptDirectory.from_config(config)
     
-def splice(config):
+def splice(config, parent, child):
     """'splice' two branches, creating a new revision file."""
     
     
index a919c07baf8407cc089fa27d451a2871d8976b8b..a89da7432a53a8b9bc27ce3bf646170493acf09c 100644 (file)
@@ -1,5 +1,6 @@
 from alembic.ddl import base
 from alembic import util
+from sqlalchemy import MetaData, Table, Column, String
 
 class ContextMeta(type):
     def __init__(cls, classname, bases, dict_):
@@ -9,14 +10,36 @@ class ContextMeta(type):
         return newtype
 
 _context_impls = {}
-    
+
+_meta = MetaData()
+_version = Table('alembic_version', _meta, 
+                Column('version_num', String(32), nullable=False)
+            )
+
 class DefaultContext(object):
     __metaclass__ = ContextMeta
+    __dialect__ = 'default'
     
-    def __init__(self, options, connection):
-        self.options = options
+    def __init__(self, connection, fn):
         self.connection = connection
+        self._migrations_fn = fn
+        
+    def _current_rev(self):
+        _version.create(self.connection, checkfirst=True)
+        return self.connection.scalar(_version.select())
     
+    def _update_current_rev(self, old, new):
+        if old is None:
+            self.connection.execute(_version.insert(), {'version_num':new})
+        else:
+            self.connection.execute(_version.update(), {'version_num':new})
+            
+    def run_migrations(self, **kw):
+        current_rev = self._current_rev()
+        for change in self._migrations_fn(current_rev):
+            change.execute()            
+        self._update_current_rev(current_rev, change.upgrade)
+        
     def _exec(self, construct):
         pass
         
@@ -37,4 +60,12 @@ class DefaultContext(object):
     def add_constraint(self, const):
         self._exec(schema.AddConstraint(const))
 
-        
\ No newline at end of file
+
+def configure_connection(connection):
+    global _context
+    _context = _context_impls[connection.dialect.name](connection, _migration_fn)
+    
+def run_migrations(**kw):
+    global _context
+    _context.run_migrations(**kw)
+    
\ No newline at end of file
index d35751b73fdf6fe2202e4cc7c4819623d856589f..9c83af84414a5064984d52e636b4c24743acb922 100644 (file)
@@ -22,6 +22,15 @@ class ScriptDirectory(object):
         return ScriptDirectory(
                     options.get_main_option('script_location'))
 
+    def upgrade_from(self, current_rev):
+        return []
+
+    def downgrade_to(self, destination, current_rev):
+        return []
+
+    def run_env(self):
+        pass
+
     @util.memoized_property
     def _revision_map(self):
         map_ = {}
index e11c4a6c61f85c3a8d7f40efa4d04dad49093d29..b6a389f2beca5a0f31491773bb8a9331cf0aecaa 100644 (file)
@@ -10,7 +10,7 @@ connection = engine.connect()
 context.configure_connection(connection)
 trans = connection.begin()
 try:
-    run_migrations()
+    context.run_migrations()
     trans.commit()
 except:
     trans.rollback()
index 22746cb2c31580447a51cac39a06f06c8bd960f3..4e50ddf3cf5db286a15a7500283d7a0c8fc2a07a 100644 (file)
@@ -24,7 +24,7 @@ for name in re.split(r',\s*', db_names):
 try:
     for name, rec in engines.items():
         context.configure_connection(rec['connection'])
-        run_migrations(engine=name)
+        context.run_migrations(engine=name)
 
     if USE_TWOPHASE:
         for rec in engines.values():
index 8afa10872060910014e1d3d87c88d72ab57e1229..a20327b62835e5aac7abb694ff2bfa2e9d4e8a7f 100644 (file)
@@ -20,7 +20,7 @@ connection = meta.engine.connect()
 context.configure_connection(connection)
 trans = connection.begin()
 try:
-    run_migrations()
+    context.run_migrations()
     trans.commit()
 except:
     trans.rollback()