From beddcdf1659d8427d0f045a4e4b000001a73c2e5 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 29 Apr 2010 20:23:08 -0400 Subject: [PATCH] ui --- alembic/command.py | 28 +++++++++++----------------- alembic/context.py | 13 +++++++++++-- alembic/ddl/__init__.py | 2 +- alembic/ddl/postgresql.py | 2 +- alembic/script.py | 22 ++++++++++++++++++++++ 5 files changed, 46 insertions(+), 21 deletions(-) diff --git a/alembic/command.py b/alembic/command.py index a2253185..050111a5 100644 --- a/alembic/command.py +++ b/alembic/command.py @@ -85,22 +85,18 @@ def history(config): """List changeset scripts in chronological order.""" script = ScriptDirectory.from_config(config) - heads = set(script._get_heads()) - base = script._get_rev("base") - while heads: - todo = set(heads) - heads = set() - for head in todo: - print - if head in heads: - break - for sc in script._revs(head, base): - if sc.is_branch_point and sc.revision not in todo: - heads.add(sc.revision) - break - else: - print sc + for sc in script.walk_revisions(): + if sc.is_head: + print + print sc +def branches(config): + """Show current un-spliced branch points""" + script = ScriptDirectory.from_config(config) + for sc in script.walk_revisions(): + if sc.is_branch_point: + print sc + def current(config): """Display the current revision for each database.""" @@ -120,5 +116,3 @@ def splice(config, parent, child): """'splice' two branches, creating a new revision file.""" -def branches(config): - """Show current un-spliced branch points""" \ No newline at end of file diff --git a/alembic/context.py b/alembic/context.py index cc1a6325..96941902 100644 --- a/alembic/context.py +++ b/alembic/context.py @@ -23,6 +23,8 @@ class DefaultContext(object): __metaclass__ = ContextMeta __dialect__ = 'default' + transactional_ddl = False + def __init__(self, connection, fn): self.connection = connection self._migrations_fn = fn @@ -43,13 +45,20 @@ class DefaultContext(object): self.connection.execute(_version.update(), {'version_num':new}) def run_migrations(self, **kw): + log.info("Context class %s.", self.__class__.__name__) + log.info("Will assume %s DDL.", + "transactional" if self.transactional_ddl + else "non-transactional") current_rev = prev_rev = rev = self._current_rev() for change, rev in self._migrations_fn(current_rev): log.info("Running %s %s -> %s", change.__name__, prev_rev, rev) change(**kw) + if not self.transactional_ddl: + self._update_current_rev(prev_rev, rev) prev_rev = rev - - self._update_current_rev(current_rev, rev) + + if self.transactional_ddl: + self._update_current_rev(current_rev, rev) def _exec(self, construct): self.connection.execute(construct) diff --git a/alembic/ddl/__init__.py b/alembic/ddl/__init__.py index 232d538b..20bb6d77 100644 --- a/alembic/ddl/__init__.py +++ b/alembic/ddl/__init__.py @@ -1 +1 @@ -import base, postgresql \ No newline at end of file +import base, postgresql, mysql, sqlite \ No newline at end of file diff --git a/alembic/ddl/postgresql.py b/alembic/ddl/postgresql.py index 894a3026..ebd2f00c 100644 --- a/alembic/ddl/postgresql.py +++ b/alembic/ddl/postgresql.py @@ -2,5 +2,5 @@ from alembic.context import DefaultContext class PostgresqlContext(DefaultContext): __dialect__ = 'postgresql' - + transactional_ddl = True \ No newline at end of file diff --git a/alembic/script.py b/alembic/script.py index f5766a82..d0de004c 100644 --- a/alembic/script.py +++ b/alembic/script.py @@ -23,6 +23,28 @@ class ScriptDirectory(object): return ScriptDirectory( config.get_main_option('script_location')) + def walk_revisions(self): + """Iterate through all revisions. + + This is actually a breadth-first tree traversal, + with leaf nodes being heads. + + """ + heads = set(self._get_heads()) + base = self._get_rev("base") + while heads: + todo = set(heads) + heads = set() + for head in todo: + if head in heads: + break + for sc in self._revs(head, base): + if sc.is_branch_point and sc.revision not in todo: + heads.add(sc.revision) + break + else: + yield sc + def _get_rev(self, id_): if id_ == 'head': id_ = self._current_head() -- 2.47.2