transactional_ddl,
self.output_buffer
)
+ log.info("Context impl %s.", self.impl.__class__.__name__)
+ if self.as_sql:
+ log.info("Generating static SQL")
+ log.info("Will assume %s DDL.",
+ "transactional" if self.impl.transactional_ddl
+ else "non-transactional")
def _current_rev(self):
if self.as_sql:
)
def run_migrations(self, **kw):
- log.info("Context impl %s.", self.impl.__class__.__name__)
- if self.as_sql:
- log.info("Generating static SQL")
- log.info("Will assume %s DDL.",
- "transactional" if self.impl.transactional_ddl
- else "non-transactional")
-
- if self.as_sql and self.impl.transactional_ddl:
- self.impl.static_output("BEGIN;")
current_rev = rev = False
for change, prev_rev, rev in self._migrations_fn(
if self.as_sql and not rev:
_version.drop(self.connection)
- if self.as_sql and self.impl.transactional_ddl:
- self.impl.static_output("COMMIT;")
-
def execute(self, sql):
self.impl._exec(sql)
_context = _script = None
_context_opts = {}
-def requires_connection():
- """Return True if the current migrations environment should have
- an active database connection.
+def is_offline_mode():
+ """Return True if the current migrations environment
+ is running in "offline mode".
- Currently, this is ``True`` or ``False`` depending
+ This is ``True`` or ``False`` depending
on the the ``--sql`` flag passed.
This function does not require that the :class:`.Context`
has been configured.
"""
- return not _context_opts.get('as_sql', False)
+ return _context_opts.get('as_sql', False)
+
+def is_transactional_ddl():
+ """Return True if the context is configured to expect a
+ transactional DDL capable backend.
+
+ This defaults to the type of database in use, and
+ can be overridden by the ``transactional_ddl`` argument
+ to :func:`.configure`
+
+ This function requires that a :class:`.Context` has first been
+ made available via :func:`.configure`.
+
+ """
+ return get_context().impl.transactional_ddl
+
+def requires_connection():
+ return not is_offline_mode()
def get_head_revision():
"""Return the hex identifier of the 'head' revision.
raise Exception("No context has been configured yet.")
return _context
+def get_bind():
+ """Return the current 'bind'.
+
+ In "online" mode, this is the
+ :class:`sqlalchemy.engine.Connection` currently being used
+ to emit SQL to the database.
+
+ This function requires that a :class:`.Context` has first been
+ made available via :func:`.configure`.
+
+ """
+ return get_context().bind
+
def get_impl():
return get_context().impl
\ No newline at end of file
# This line sets up loggers basically.
fileConfig(config.config_file_name)
-
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
-# if we're running in --sql mode, do everything connectionless.
-# We only need a URL, not an Engine, thereby not even requiring
-# the DBAPI be installed, though an actual Engine would of course be fine as well.
-if not context.requires_connection():
+def run_migrations_offline():
+ """Run migrations in 'offline' mode.
+
+ This configures the context with just a URL
+ and not an Engine, though an Engine is acceptable
+ here as well. By skipping the Engine creation
+ we don't even need a DBAPI to be available.
+
+ Calls to context.execute() here emit the given string to the
+ script output.
+
+ """
url = config.get_main_option("sqlalchemy.url")
context.configure(url=url)
+
+ if context.is_transactional_ddl():
+ context.execute("BEGIN")
context.run_migrations()
+ if context.is_transactional_ddl():
+ context.execute("COMMIT")
-# otherwise we need to make a connection.
-else:
+def run_migrations_online():
+ """Run migrations in 'online' mode.
- # Produce a SQLAlchemy engine using the key/values
- # within the "alembic" section of the documentation,
- # other otherwise what config_ini_section points to.
+ In this scenario we need to create an Engine
+ and associate a connection with the context.
+
+ """
engine = engine_from_config(
config.get_section(config.config_ini_section), prefix='sqlalchemy.')
trans.commit()
except:
trans.rollback()
- raise
\ No newline at end of file
+ raise
+
+if context.is_offline_mode():
+ run_migrations_offline()
+else:
+ run_migrations_online()
+
# databases.
db_names = options.get_main_option('databases')
-# set aside if we need engines or just URLs to do this.
-need_engine = context.requires_connection()
-
-# load up SQLAlchemy engines or URLs.
-engines = {}
-for name in re.split(r',\s*', db_names):
- engines[name] = rec = {}
- if need_engine:
- rec['engine'] = engine_from_config(context.config.get_section(name),
- prefix='sqlalchemy.')
- else:
+def run_migrations_offline():
+ """Run migrations in 'offline' mode.
+
+ This configures the context with just a URL
+ and not an Engine, though an Engine is acceptable
+ here as well. By skipping the Engine creation
+ we don't even need a DBAPI to be available.
+
+ Calls to context.execute() here emit the given string to the
+ script output.
+
+ """
+ # for the --sql use case, run migrations for each URL into
+ # individual files.
+
+ engines = {}
+ for name in re.split(r',\s*', db_names):
+ engines[name] = rec = {}
rec['url'] = context.config.get_section_option(name, "sqlalchemy.url")
-# for the --sql use case, run migrations for each URL into
-# individual files.
-if not need_engine:
for name, rec in engines.items():
file_ = "%s.sql" % name
sys.stderr.write("Writing output to %s\n" % file_)
)
context.run_migrations(engine=name)
-# for the direct-to-DB use case, start a transaction on all
-# engines, then run all migrations, then commit all transactions.
-else:
+def run_migrations_online():
+ """Run migrations in 'online' mode.
+
+ In this scenario we need to create an Engine
+ and associate a connection with the context.
+
+ """
+
+ # for the direct-to-DB use case, start a transaction on all
+ # engines, then run all migrations, then commit all transactions.
+
+ engines = {}
+ for name in re.split(r',\s*', db_names):
+ engines[name] = rec = {}
+ rec['engine'] = engine_from_config(context.config.get_section(name),
+ prefix='sqlalchemy.')
+
for name, rec in engines.items():
engine = rec['engine']
rec['connection'] = conn = engine.connect()
except:
for rec in engines.values():
rec['transaction'].rollback()
- raise
\ No newline at end of file
+ raise
+
+if context.is_offline_mode():
+ run_migrations_offline()
+else:
+ run_migrations_online()
# customize this section for non-standard engine configurations.
meta = __import__("%s.model.meta" % config['pylons.package']).model.meta
+def run_migrations_offline():
+ """Run migrations in 'offline' mode.
-if not context.requires_connection():
+ This configures the context with just a URL
+ and not an Engine, though an Engine is acceptable
+ here as well. By skipping the Engine creation
+ we don't even need a DBAPI to be available.
+
+ Calls to context.execute() here emit the given string to the
+ script output.
+
+ """
context.configure(
dialect_name=meta.engine.name)
context.run_migrations()
-else:
+
+def run_migrations_online():
+ """Run migrations in 'online' mode.
+
+ In this scenario we need to create an Engine
+ and associate a connection with the context.
+
+ """
connection = meta.engine.connect()
context.configure_connection(connection)
trans = connection.begin()
trans.commit()
except:
trans.rollback()
- raise
\ No newline at end of file
+ raise
+
+if context.is_offline_mode():
+ run_migrations_offline()
+else:
+ run_migrations_online()
with capture_context_buffer() as buf:
command.stamp(cfg, "head", sql=True)
assert "UPDATE alembic_version SET version_num='%s';" % c in buf.getvalue()
+