from alembic.script import ScriptDirectory
-from alembic import util
+from alembic import util, ddl, context
import os
import functools
script = ScriptDirectory.from_config(config)
context._migration_fn = script.upgrade_from
+ context.config = config
script.run_env()
def revert(config, revision):
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)
+ for change, rev in self._migrations_fn(current_rev):
+ change.execute(**kw)
+ self._update_current_rev(current_rev, rev)
def _exec(self, construct):
pass
+
+ def execute(self, sql):
+ self._exec(sql)
def alter_column(self, table_name, column_name,
nullable=util.NO_VALUE,
def configure_connection(connection):
global _context
- _context = _context_impls[connection.dialect.name](connection, _migration_fn)
+ _context = _context_impls.get(connection.dialect.name, DefaultContext)(connection, _migration_fn)
def run_migrations(**kw):
- global _context
_context.run_migrations(**kw)
\ No newline at end of file
context.add_constraint(
_unique_constraint(name, source, local_cols)
)
+
+def execute(sql):
+ context.execute(sql)
\ No newline at end of file
options.get_main_option('script_location'))
def upgrade_from(self, current_rev):
- return []
+ script = self._revision_map[current_rev]
+ while script:
+ yield script.module.upgrade, script.upgrade
+ script = script.nextrev
def downgrade_to(self, destination, current_rev):
return []
-
+
def run_env(self):
- pass
+ util.load_python_file(self.dir, 'env.py')
@util.memoized_property
def _revision_map(self):
-from alembic import options, context
+from alembic import context
from sqlalchemy import engine_from_config
-import logging
+from logging.config import fileConfig
+config = context.config
-logging.fileConfig(options.config_file)
+fileConfig(config.config_file_name)
-engine = engine_from_config(options.get_section('alembic'), prefix='sqlalchemy.')
+engine = engine_from_config(config.get_section('alembic'), prefix='sqlalchemy.')
connection = engine.connect()
context.configure_connection(connection)