Browse Source

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
changes/00/296200/2
dineshbhor 6 years ago
parent
commit
2a32681036
  1. 4
      migrate/exceptions.py
  2. 4
      migrate/tests/versioning/test_version.py
  3. 17
      migrate/versioning/version.py

4
migrate/exceptions.py

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

4
migrate/tests/versioning/test_version.py

@ -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)

17
migrate/versioning/version.py

@ -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):

Loading…
Cancel
Save