Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
Raise VersionNotFoundError instead of KeyError
Browse files Browse the repository at this point in the history
Currently migrate.versioning.api.upgrade() raises KeyError
instead of sqlalchemy-migrate specific exception if migration
script file is not present in migration repository.

Raised migrate.exception.VersionNotFoundError if the specified
migration script does not exist in the repository. Made
VersionNotFoundError exception class as a subclass of KeyError
in order to avoid breaking existing users looking for KeyError.

Related-Bug: #1546441
Change-Id: I0210d56a6e85f03c44cea027f50863faaf050c1d
  • Loading branch information
dineshbhor committed May 27, 2016
1 parent fe3e08a commit 2a32681
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
4 changes: 4 additions & 0 deletions migrate/exceptions.py
Expand Up @@ -27,6 +27,10 @@ class InvalidVersionError(ControlledSchemaError):
"""Invalid version number."""


class VersionNotFoundError(KeyError):
"""Specified version is not present."""


class DatabaseNotControlledError(ControlledSchemaError):
"""Database should be under version control, but it's not."""

Expand Down
4 changes: 4 additions & 0 deletions migrate/tests/versioning/test_version.py
Expand Up @@ -103,6 +103,10 @@ def test_collection(self):
self.assertEqual(coll.latest, 4)
self.assertEqual(len(coll.versions), 4)
self.assertEqual(coll.version(4), coll.version(coll.latest))
# Check for non-existing version
self.assertRaises(VersionNotFoundError, coll.version, 5)
# Check for the current version
self.assertEqual('4', coll.version(4).version)

coll2 = Collection(self.temp_usable_dir)
self.assertEqual(coll.versions, coll2.versions)
Expand Down
17 changes: 14 additions & 3 deletions migrate/versioning/version.py
Expand Up @@ -156,11 +156,22 @@ def create_new_sql_version(self, database, description, **k):
self.versions[ver].add_script(filepath)

def version(self, vernum=None):
"""Returns latest Version if vernum is not given.
Otherwise, returns wanted version"""
"""Returns required version.
If vernum is not given latest version will be returned otherwise
required version will be returned.
:raises: : exceptions.VersionNotFoundError if respective migration
script file of version is not present in the migration repository.
"""
if vernum is None:
vernum = self.latest
return self.versions[VerNum(vernum)]

try:
return self.versions[VerNum(vernum)]
except KeyError:
raise exceptions.VersionNotFoundError(
("Database schema file with version %(args)s doesn't "
"exist.") % {'args': VerNum(vernum)})

@classmethod
def clear(cls):
Expand Down

0 comments on commit 2a32681

Please sign in to comment.