diff --git a/py/config.py b/py/config.py index c6a4ca9..8adc7d6 100644 --- a/py/config.py +++ b/py/config.py @@ -26,7 +26,7 @@ class _Key(): UPPER_THRESHOLD = "upper_threshold" # Removed - OWNERS = "owners" + _OWNERS = "owners" _SCHEMA = Schema( @@ -54,7 +54,7 @@ _SCHEMA = Schema( def _migrate_to_v1(d: dict): # Owners moved to History in v1 # Note: owners will be required to be re-added to the state.json - owners = d.pop(_Key.OWNERS) + owners = d.pop(_Key._OWNERS) logger.warn( "Migration removed owners from config, these must be re-added to the state.json") logger.warn("Owners: %s", owners) diff --git a/py/state.py b/py/state.py index f75be41..453a76e 100644 --- a/py/state.py +++ b/py/state.py @@ -13,18 +13,48 @@ logger.setLevel(logging.INFO) # Warning: Changing any of the below needs proper thought to ensure backwards compatibility -_VERSION = 1 +_VERSION = 2 def _migrate_to_v1(d: dict): - logger.info("Renaming %s to %s", _Key.MATCHEES, _Key.USERS) - d[_Key.USERS] = d[_Key.MATCHEES] - del d[_Key.MATCHEES] + """v1 simply renamed matchees to users""" + logger.info("Renaming %s to %s", _Key._MATCHEES, _Key.USERS) + d[_Key.USERS] = d[_Key._MATCHEES] + del d[_Key._MATCHEES] + + +def _migrate_to_v2(d: dict): + """v2 swapped the date over to a less silly format""" + logger.info("Fixing up date format from %s to %s", + _TIME_FORMAT_OLD, _TIME_FORMAT) + + def old_to_new_ts(ts: str) -> str: + return datetime.strftime(datetime.strptime(ts, _TIME_FORMAT_OLD), _TIME_FORMAT) + + # Adjust all the history keys + d[_Key.HISTORY] = { + old_to_new_ts(ts): entry + for ts, entry in d[_Key.HISTORY].items() + } + # Adjust all the user parts + for user in d[_Key.USERS].values(): + # Update the match dates + matches = user.get(_Key.MATCHES, {}) + for id, ts in matches.items(): + matches[id] = old_to_new_ts(ts) + + # Update any reactivation dates + channels = user.get(_Key.CHANNELS, {}) + for id, channel in channels.items(): + old_ts = channel.get(_Key.REACTIVATE, None) + if old_ts: + channel[_Key.REACTIVATE] = old_to_new_ts(old_ts) # Set of migration functions to apply _MIGRATIONS = [ - _migrate_to_v1 + _migrate_to_v1, + _migrate_to_v2 ] @@ -48,10 +78,11 @@ class _Key(str): VERSION = "version" # Unused - MATCHEES = "matchees" + _MATCHEES = "matchees" -_TIME_FORMAT = "%a %b %d %H:%M:%S %Y" +_TIME_FORMAT = "%Y-%m-%d %H:%M:%S.%f" +_TIME_FORMAT_OLD = "%a %b %d %H:%M:%S %Y" _SCHEMA = Schema(