@@ -11,8 +11,22 @@ class ValidationError(jsonschema.ValidationError):
11
11
pass
12
12
13
13
14
- def validate (config , version = (len (SCHEMAS ) - 1 )):
15
- assert version < len (SCHEMAS )
14
+ class VersionError (Exception ):
15
+ pass
16
+
17
+
18
+ class UpgradeError (Exception ):
19
+ pass
20
+
21
+
22
+ def validate (config , version = None ):
23
+ if version is None :
24
+ version = get_version (config )
25
+
26
+ if version >= len (SCHEMAS ):
27
+ raise VersionError (
28
+ "Configuration version %d is newer than latest known configuration version %d" % (version , len (SCHEMAS ) - 1 )
29
+ )
16
30
17
31
try :
18
32
jsonschema .validate (config , SCHEMAS [version ])
@@ -35,16 +49,40 @@ def upgrade(config):
35
49
latest = len (SCHEMAS ) - 1
36
50
37
51
if current > latest :
38
- logger .warning ("Configuration version %d is newer than latest known configuration version %d" , current , latest )
39
- logger .warning ("Is your installation of Assigner up to date?" )
40
- logger .warning ("Attempting to continue anyway..." )
41
52
return config
42
53
43
54
if current != latest :
44
55
logger .info ("Migrating configuration from version %d to version %d." , current , latest )
45
56
57
+ # Determine whether we should look for upgrade-caused
58
+ # validation errors. If the initial config doesn't validate,
59
+ # we can't tell whether upgrading has made things worse, but
60
+ # we'll try anyway.
61
+ try :
62
+ validate (config , current )
63
+ is_valid = True
64
+ except ValidationError :
65
+ is_valid = False
66
+
46
67
for version in range (current , latest ):
47
68
config = UPGRADES [version ](config )
69
+
70
+ # Upgrade validation.
71
+ # Upgrades should be rare, so we can afford to be very particular about them.
48
72
assert get_version (config ) == version + 1
73
+ if is_valid :
74
+ try :
75
+ validate (config , version + 1 )
76
+ except ValidationError as e :
77
+ # pylint: disable=bad-continuation
78
+ raise UpgradeError (
79
+ """
80
+ Upgrading configuration from version %d to %d resulted in an invalid configuration:
81
+ %s
82
+
83
+ This is a bug. Please file an issue at https://github.com/redkyn/assigner/issues with your configuration.
84
+ Your original configuration has been restored.
85
+ """ % (version , version + 1 , e .message )
86
+ )
49
87
50
88
return config
0 commit comments