From: Mike Bayer Date: Wed, 28 Apr 2010 02:57:54 +0000 (-0400) Subject: - get the test environment to use the command line correctly X-Git-Tag: rel_0_1_0~103 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=682c2bb5cad903db3f5dd3d34dd4b78844c3795c;p=thirdparty%2Fsqlalchemy%2Falembic.git - get the test environment to use the command line correctly - use time-based slightly random hex ids. - probably going to remove the id from the upgrade/downgrade name, not much point to it. --- diff --git a/alembic/command.py b/alembic/command.py index cd4508ae..50452b0a 100644 --- a/alembic/command.py +++ b/alembic/command.py @@ -65,7 +65,7 @@ def revision(opts): """Create a new revision file.""" script = ScriptDirectory.from_options(opts) - script.generate_rev(str(uuid.uuid4().hex), opts.cmd_line_options.message) + script.generate_rev(util.rev_id(), opts.cmd_line_options.message) def upgrade(opts): """Upgrade to the latest version.""" diff --git a/alembic/options.py b/alembic/options.py index 3b020cb7..f54a1b48 100644 --- a/alembic/options.py +++ b/alembic/options.py @@ -49,10 +49,14 @@ class Options(object): self.cmd_line_args = parser.parse_args(argv[1:]) if len(self.cmd_line_args) < 1: self.err("no command specified") - self.config_file_name = self.cmd_line_options.config - self.file_config = ConfigParser.ConfigParser() - self.file_config.read([self.config_file_name]) + @util.memoized_property + def file_config(self): + self.config_file_name = self.cmd_line_options.config + file_config = ConfigParser.ConfigParser() + file_config.read([self.config_file_name]) + return file_config + def get_command(self): return self.cmd_line_args[0] diff --git a/alembic/util.py b/alembic/util.py index b065ef92..d23d7221 100644 --- a/alembic/util.py +++ b/alembic/util.py @@ -6,6 +6,8 @@ from sqlalchemy import util import imp import warnings import re +import time +import random NO_VALUE = util.symbol("NO_VALUE") @@ -55,6 +57,12 @@ def load_python_file(dir_, filename): del sys.modules[module_id] return module +def rev_id(): + v1 = int(time.time()) * 10000 + v2 = random.randint(0, 9999) + val = v1 + v2 + return hex(val)[2:-1] + class memoized_property(object): """A read-only @property that is only evaluated once.""" def __init__(self, fget, doc=None): diff --git a/tests/__init__.py b/tests/__init__.py index ee5eb28c..85f79d2a 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,4 +1,4 @@ -from sqlalchemy.test.testing import eq_ +from sqlalchemy.test.testing import eq_, ne_ from sqlalchemy.util import defaultdict from sqlalchemy.engine import url, default import shutil @@ -27,10 +27,13 @@ def _testing_options(**kw): 'config', os.path.join(staging_directory, 'test_alembic.ini') ) + return Options( get_option_parser(), - list(itertools.chain(*[["--%s" % k, "%r" % v] for k, v in kw.items()]) ) - + [os.path.join(staging_directory, 'scripts')] + ["./scripts/alembic"] + \ + list(itertools.chain(*[["--%s" % k, "%s" % v] for k, v in kw.items()])) + \ + ["init"] +\ + [os.path.join(staging_directory, 'scripts')] ) def staging_env(create=True): diff --git a/tests/test_revisions.py b/tests/test_revisions.py index f704cde0..5ee1368b 100644 --- a/tests/test_revisions.py +++ b/tests/test_revisions.py @@ -1,4 +1,5 @@ -from tests import clear_staging_env, staging_env, eq_ +from tests import clear_staging_env, staging_env, eq_, ne_ +from alembic import util import os def test_001_environment(): @@ -8,38 +9,44 @@ def test_001_environment(): assert_set ) -def test_002_heads(): +def test_002_rev_ids(): + global abc, def_ + abc = util.rev_id() + def_ = util.rev_id() + ne_(abc, def_) + +def test_003_heads(): eq_(env._get_heads(), []) -def test_003_rev(): - script = env.generate_rev("abc", "this is a message") +def test_004_rev(): + script = env.generate_rev(abc, "this is a message") eq_(script.module.__doc__,"this is a message") - eq_(script.upgrade, "abc") + eq_(script.upgrade, abc) eq_(script.downgrade, None) - assert os.access(os.path.join(env.dir, 'versions', 'abc.py'), os.F_OK) - assert callable(script.module.upgrade_abc) - eq_(env._get_heads(), ["abc"]) + assert os.access(os.path.join(env.dir, 'versions', '%s.py' % abc), os.F_OK) + assert callable(getattr(script.module, 'upgrade_%s' % abc)) + eq_(env._get_heads(), [abc]) -def test_004_nextrev(): - script = env.generate_rev("def", "this is the next rev") - eq_(script.upgrade, "def") - eq_(script.downgrade, "abc") - eq_(env._revision_map["abc"].nextrev, "def") - assert callable(script.module.upgrade_def) - assert callable(script.module.downgrade_abc) - eq_(env._get_heads(), ["def"]) +def test_005_nextrev(): + script = env.generate_rev(def_, "this is the next rev") + 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)) + eq_(env._get_heads(), [def_]) -def test_005_from_clean_env(): +def test_006_from_clean_env(): # test the environment so far with a # new ScriptDirectory instance. env = staging_env(create=False) - abc = env._revision_map["abc"] - def_ = env._revision_map["def"] - eq_(abc.nextrev, "def") - eq_(abc.upgrade, "abc") - eq_(def_.downgrade, "abc") - eq_(env._get_heads(), ["def"]) + abc_rev = env._revision_map[abc] + def_rev = env._revision_map[def_] + eq_(abc_rev.nextrev, def_) + eq_(abc_rev.upgrade, abc) + eq_(def_rev.downgrade, abc) + eq_(env._get_heads(), [def_]) def setup(): global env