summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--alembic/script.py25
-rw-r--r--alembic/util.py5
-rw-r--r--templates/generic/script.py.mako7
-rw-r--r--templates/multidb/script.py.mako15
-rw-r--r--templates/pylons/script.py.mako7
-rw-r--r--tests/test_revisions.py7
6 files changed, 32 insertions, 34 deletions
diff --git a/alembic/script.py b/alembic/script.py
index 27e84df..b2fded6 100644
--- a/alembic/script.py
+++ b/alembic/script.py
@@ -4,7 +4,7 @@ import shutil
import re
import inspect
-_rev_file = re.compile(r'[a-z0-9]+\.py$')
+_rev_file = re.compile(r'([a-z0-9]+)\.py$')
_mod_def_re = re.compile(r'(upgrade|downgrade)_([a-z0-9]+)')
class ScriptDirectory(object):
@@ -101,31 +101,20 @@ class ScriptDirectory(object):
class Script(object):
nextrev = None
- def __init__(self, module):
+ def __init__(self, module, rev_id):
self.module = module
- self.upgrade = self.downgrade = None
- for name in dir(module):
- m = _mod_def_re.match(name)
- if not m:
- continue
- fn = getattr(module, name)
- if not inspect.isfunction(fn):
- continue
- if m.group(1) == 'upgrade':
- self.upgrade = m.group(2)
- elif m.group(1) == 'downgrade':
- self.downgrade = m.group(2)
- if not self.downgrade and not self.upgrade:
- raise Exception("Script %s has no upgrade or downgrade path" % module)
+ self.upgrade = rev_id
+ self.downgrade = getattr(module, 'down_revision', None)
def __str__(self):
return "revision %s" % self.upgrade
@classmethod
def from_path(cls, dir_, filename):
- if not _rev_file.match(filename):
+ m = _rev_file.match(filename)
+ if not m:
return None
module = util.load_python_file(dir_, filename)
- return Script(module)
+ return Script(module, m.group(1))
\ No newline at end of file
diff --git a/alembic/util.py b/alembic/util.py
index d23d722..79b1e3b 100644
--- a/alembic/util.py
+++ b/alembic/util.py
@@ -8,6 +8,7 @@ import warnings
import re
import time
import random
+import uuid
NO_VALUE = util.symbol("NO_VALUE")
@@ -58,9 +59,7 @@ def load_python_file(dir_, filename):
return module
def rev_id():
- v1 = int(time.time()) * 10000
- v2 = random.randint(0, 9999)
- val = v1 + v2
+ val = int(uuid.uuid4()) % 100000000000000
return hex(val)[2:-1]
class memoized_property(object):
diff --git a/templates/generic/script.py.mako b/templates/generic/script.py.mako
index 5091946..9e89fa6 100644
--- a/templates/generic/script.py.mako
+++ b/templates/generic/script.py.mako
@@ -1,12 +1,15 @@
"""${message}"""
+# downgrade revision identifier, used by Alembic.
+down_revision = ${repr(down_revision)}
+
from alembic.op import *
-def upgrade_${up_revision}():
+def upgrade():
pass
% if down_revision:
-def downgrade_${down_revision}():
+def downgrade():
pass
% else:
# this is the origin node, no downgrade !
diff --git a/templates/multidb/script.py.mako b/templates/multidb/script.py.mako
index 2aebeb6..d485a58 100644
--- a/templates/multidb/script.py.mako
+++ b/templates/multidb/script.py.mako
@@ -1,22 +1,25 @@
"""${message}"""
+# downgrade revision identifier, used by Alembic.
+down_revision = ${repr(down_revision)}
+
from alembic.op import *
-def upgrade_${up_revision}(engine):
- eval("upgrade_%s_${up_revision}" % engine.name)()
+def upgrade(engine):
+ eval("upgrade_%s" % engine.name)()
% if down_revision:
-def downgrade_${down_revision}(engine):
- eval("upgrade_%s_${down_revision}" % engine.name)()
+def downgrade(engine):
+ eval("upgrade_%s" % engine.name)()
% else:
# this is the origin node, no downgrade !
% endif
% for engine in ["engine1", "engine2"]:
- def upgrade_${engine}_${up_revision}():
+ def upgrade_${engine}():
pass
- def downgrade_${engine}_${down_revision}():
+ def downgrade_${engine}():
pass
% endfor \ No newline at end of file
diff --git a/templates/pylons/script.py.mako b/templates/pylons/script.py.mako
index 5091946..9e89fa6 100644
--- a/templates/pylons/script.py.mako
+++ b/templates/pylons/script.py.mako
@@ -1,12 +1,15 @@
"""${message}"""
+# downgrade revision identifier, used by Alembic.
+down_revision = ${repr(down_revision)}
+
from alembic.op import *
-def upgrade_${up_revision}():
+def upgrade():
pass
% if down_revision:
-def downgrade_${down_revision}():
+def downgrade():
pass
% else:
# this is the origin node, no downgrade !
diff --git a/tests/test_revisions.py b/tests/test_revisions.py
index 5ee1368..30fc9c6 100644
--- a/tests/test_revisions.py
+++ b/tests/test_revisions.py
@@ -24,7 +24,7 @@ def test_004_rev():
eq_(script.upgrade, abc)
eq_(script.downgrade, None)
assert os.access(os.path.join(env.dir, 'versions', '%s.py' % abc), os.F_OK)
- assert callable(getattr(script.module, 'upgrade_%s' % abc))
+ assert callable(script.module.upgrade)
eq_(env._get_heads(), [abc])
def test_005_nextrev():
@@ -32,8 +32,9 @@ def test_005_nextrev():
eq_(script.upgrade, def_)
eq_(script.downgrade, abc)
eq_(env._revision_map[abc].nextrev, def_)
- assert callable(getattr(script.module, 'upgrade_%s' % def_))
- assert callable(getattr(script.module, 'downgrade_%s' % abc))
+ assert script.module.down_revision == abc
+ assert callable(script.module.upgrade)
+ assert callable(script.module.downgrade)
eq_(env._get_heads(), [def_])
def test_006_from_clean_env():