Raise VersionNotFoundError instead of KeyError

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
This commit is contained in:
dineshbhor 2016-03-14 07:08:28 +00:00
parent fe3e08ae0b
commit 2a32681036
3 changed files with 22 additions and 3 deletions

View File

@ -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."""

View File

@ -103,6 +103,10 @@ class TestVersion(fixture.Pathed):
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)

View File

@ -156,11 +156,22 @@ class Collection(pathed.Pathed):
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):