summaryrefslogtreecommitdiff
path: root/alembic/script.py
diff options
context:
space:
mode:
Diffstat (limited to 'alembic/script.py')
-rw-r--r--alembic/script.py94
1 files changed, 49 insertions, 45 deletions
diff --git a/alembic/script.py b/alembic/script.py
index ed44f71..a97fc9c 100644
--- a/alembic/script.py
+++ b/alembic/script.py
@@ -12,7 +12,9 @@ _slug_re = re.compile(r'\w+')
_default_file_template = "%(rev)s_%(slug)s"
_relative_destination = re.compile(r'(?:\+|-)\d+')
+
class ScriptDirectory(object):
+
"""Provides operations upon an Alembic script directory.
This object is useful to get information as to current revisions,
@@ -31,9 +33,10 @@ class ScriptDirectory(object):
"""
+
def __init__(self, dir, file_template=_default_file_template,
- truncate_slug_length=40,
- sourceless=False):
+ truncate_slug_length=40,
+ sourceless=False):
self.dir = dir
self.versions = os.path.join(self.dir, 'versions')
self.file_template = file_template
@@ -42,8 +45,8 @@ class ScriptDirectory(object):
if not os.access(dir, os.F_OK):
raise util.CommandError("Path doesn't exist: %r. Please use "
- "the 'init' command to create a new "
- "scripts folder." % dir)
+ "the 'init' command to create a new "
+ "scripts folder." % dir)
@classmethod
def from_config(cls, config):
@@ -62,13 +65,13 @@ class ScriptDirectory(object):
if truncate_slug_length is not None:
truncate_slug_length = int(truncate_slug_length)
return ScriptDirectory(
- util.coerce_resource_to_filename(script_location),
- file_template=config.get_main_option(
- 'file_template',
- _default_file_template),
- truncate_slug_length=truncate_slug_length,
- sourceless=config.get_main_option("sourceless") == "true"
- )
+ util.coerce_resource_to_filename(script_location),
+ file_template=config.get_main_option(
+ 'file_template',
+ _default_file_template),
+ truncate_slug_length=truncate_slug_length,
+ sourceless=config.get_main_option("sourceless") == "true"
+ )
def walk_revisions(self, base="base", head="head"):
"""Iterate through all revisions.
@@ -108,11 +111,11 @@ class ScriptDirectory(object):
raise util.CommandError("No such revision '%s'" % id_)
elif len(revs) > 1:
raise util.CommandError(
- "Multiple revisions start "
- "with '%s', %s..." % (
- id_,
- ", ".join("'%s'" % r for r in revs[0:3])
- ))
+ "Multiple revisions start "
+ "with '%s', %s..." % (
+ id_,
+ ", ".join("'%s'" % r for r in revs[0:3])
+ ))
else:
return self._revision_map[revs[0]]
@@ -148,7 +151,7 @@ class ScriptDirectory(object):
revs = revs[-relative:]
if len(revs) != abs(relative):
raise util.CommandError("Relative revision %s didn't "
- "produce %d migrations" % (upper, abs(relative)))
+ "produce %d migrations" % (upper, abs(relative)))
return iter(revs)
elif lower is not None and _relative_destination.match(lower):
relative = int(lower)
@@ -156,7 +159,7 @@ class ScriptDirectory(object):
revs = revs[0:-relative]
if len(revs) != abs(relative):
raise util.CommandError("Relative revision %s didn't "
- "produce %d migrations" % (lower, abs(relative)))
+ "produce %d migrations" % (lower, abs(relative)))
return iter(revs)
else:
return self._iterate_revisions(upper, lower)
@@ -165,12 +168,12 @@ class ScriptDirectory(object):
lower = self.get_revision(lower)
upper = self.get_revision(upper)
orig = lower.revision if lower else 'base', \
- upper.revision if upper else 'base'
+ upper.revision if upper else 'base'
script = upper
while script != lower:
if script is None and lower is not None:
raise util.CommandError(
- "Revision %s is not an ancestor of %s" % orig)
+ "Revision %s is not an ancestor of %s" % orig)
yield script
downrev = script.down_revision
script = self._revision_map[downrev]
@@ -181,7 +184,7 @@ class ScriptDirectory(object):
(script.module.upgrade, script.down_revision, script.revision,
script.doc)
for script in reversed(list(revs))
- ]
+ ]
def _downgrade_revs(self, destination, current_rev):
revs = self.iterate_revisions(current_rev, destination)
@@ -189,7 +192,7 @@ class ScriptDirectory(object):
(script.module.downgrade, script.revision, script.down_revision,
script.doc)
for script in revs
- ]
+ ]
def run_env(self):
"""Run the script environment.
@@ -216,14 +219,14 @@ class ScriptDirectory(object):
continue
if script.revision in map_:
util.warn("Revision %s is present more than once" %
- script.revision)
+ script.revision)
map_[script.revision] = script
for rev in map_.values():
if rev.down_revision is None:
continue
if rev.down_revision not in map_:
util.warn("Revision %s referenced from %s is not present"
- % (rev.down_revision, rev))
+ % (rev.down_revision, rev))
rev.down_revision = None
else:
map_[rev.down_revision].add_nextrev(rev.revision)
@@ -260,10 +263,10 @@ class ScriptDirectory(object):
current_heads = self.get_heads()
if len(current_heads) > 1:
raise util.CommandError('Only a single head is supported. The '
- 'script directory has multiple heads (due to branching), which '
- 'must be resolved by manually editing the revision files to '
- 'form a linear sequence. Run `alembic branches` to see the '
- 'divergence(s).')
+ 'script directory has multiple heads (due to branching), which '
+ 'must be resolved by manually editing the revision files to '
+ 'form a linear sequence. Run `alembic branches` to see the '
+ 'divergence(s).')
if current_heads:
return current_heads[0]
@@ -303,18 +306,18 @@ class ScriptDirectory(object):
"""
for script in self._revision_map.values():
if script and script.down_revision is None \
- and script.revision in self._revision_map:
+ and script.revision in self._revision_map:
return script.revision
else:
return None
def _generate_template(self, src, dest, **kw):
util.status("Generating %s" % os.path.abspath(dest),
- util.template_to_file,
- src,
- dest,
- **kw
- )
+ util.template_to_file,
+ src,
+ dest,
+ **kw
+ )
def _copy_file(self, src, dest):
util.status("Generating %s" % os.path.abspath(dest),
@@ -357,13 +360,14 @@ class ScriptDirectory(object):
self._revision_map[script.revision] = script
if script.down_revision:
self._revision_map[script.down_revision].\
- add_nextrev(script.revision)
+ add_nextrev(script.revision)
return script
else:
return None
class Script(object):
+
"""Represent a single revision file in a ``versions/`` directory.
The :class:`.Script` instance is returned by methods
@@ -455,11 +459,11 @@ class Script(object):
def __str__(self):
return "%s -> %s%s%s, %s" % (
- self.down_revision,
- self.revision,
- " (head)" if self.is_head else "",
- " (branchpoint)" if self.is_branch_point else "",
- self.doc)
+ self.down_revision,
+ self.revision,
+ " (head)" if self.is_head else "",
+ " (branchpoint)" if self.is_branch_point else "",
+ self.doc)
@classmethod
def _from_path(cls, scriptdir, path):
@@ -502,11 +506,11 @@ class Script(object):
m = _legacy_rev.match(filename)
if not m:
raise util.CommandError(
- "Could not determine revision id from filename %s. "
- "Be sure the 'revision' variable is "
- "declared inside the script (please see 'Upgrading "
- "from Alembic 0.1 to 0.2' in the documentation)."
- % filename)
+ "Could not determine revision id from filename %s. "
+ "Be sure the 'revision' variable is "
+ "declared inside the script (please see 'Upgrading "
+ "from Alembic 0.1 to 0.2' in the documentation)."
+ % filename)
else:
revision = m.group(1)
else: