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

Implement tracking event format versions #4403

Closed
wants to merge 9 commits into from
13 changes: 13 additions & 0 deletions synapse/api/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ class RoomVersions(object):
RoomVersions.STATE_V2_TEST,
}


class EventFormatVersions(object):
"""This is an internal enum for tracking the version of the event format,
independently from the room version.
"""
V1 = 1


KNOWN_EVENT_FORMAT_VERSIONS = {
EventFormatVersions.V1,
}


ServerNoticeMsgType = "m.server_notice"
ServerNoticeLimitReached = "m.server_notice.usage_limit_reached"

Expand Down
19 changes: 18 additions & 1 deletion synapse/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

import six

from synapse.api.constants import KNOWN_ROOM_VERSIONS
from synapse.api.constants import (
KNOWN_EVENT_FORMAT_VERSIONS,
KNOWN_ROOM_VERSIONS,
EventFormatVersions,
)
from synapse.util.caches import intern_dict
from synapse.util.frozenutils import freeze

Expand Down Expand Up @@ -180,6 +184,8 @@ def auth_event_ids(self):


class FrozenEvent(EventBase):
format_version = EventFormatVersions.V1 # All events of this type are V1

def __init__(self, event_dict, internal_metadata_dict={}, rejected_reason=None):
event_dict = dict(event_dict)

Expand Down Expand Up @@ -244,3 +250,14 @@ def event_type_from_room_version(room_version):
"No event format defined for version %r" % (room_version,)
)
return FrozenEvent


def event_type_from_format_version(format_version):
"""Returns the python type to use to construct an Event object for the
given event format version.
"""
if format_version not in KNOWN_EVENT_FORMAT_VERSIONS:
raise Exception(
"No event format %r" % (format_version,)
)
return FrozenEvent
1 change: 1 addition & 0 deletions synapse/storage/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,7 @@ def event_dict(event):
event.internal_metadata.get_dict()
),
"json": encode_json(event_dict(event)),
"format_version": event.format_version,
}
for event, _ in events_and_contexts
],
Expand Down
14 changes: 11 additions & 3 deletions synapse/storage/events_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@

from twisted.internet import defer

from synapse.api.constants import EventFormatVersions
from synapse.api.errors import NotFoundError
from synapse.events import event_type_from_room_version
from synapse.events import event_type_from_format_version
# these are only included to make the type annotations work
from synapse.events.snapshot import EventContext # noqa: F401
from synapse.events.utils import prune_event
Expand Down Expand Up @@ -352,6 +353,7 @@ def _enqueue_events(self, events, check_redacted=True, allow_rejected=False):
self._get_event_from_row,
row["internal_metadata"], row["json"], row["redacts"],
rejected_reason=row["rejects"],
format_version=row["format_version"],
)
for row in rows
],
Expand All @@ -376,6 +378,7 @@ def _fetch_event_rows(self, txn, events):
" e.event_id as event_id, "
" e.internal_metadata,"
" e.json,"
" e.format_version, "
" r.redacts as redacts,"
" rej.event_id as rejects "
" FROM event_json as e"
Expand All @@ -391,7 +394,7 @@ def _fetch_event_rows(self, txn, events):

@defer.inlineCallbacks
def _get_event_from_row(self, internal_metadata, js, redacted,
rejected_reason=None):
format_version, rejected_reason=None):
with Measure(self._clock, "_get_event_from_row"):
d = json.loads(js)
internal_metadata = json.loads(internal_metadata)
Expand All @@ -404,7 +407,12 @@ def _get_event_from_row(self, internal_metadata, js, redacted,
desc="_get_event_from_row_rejected_reason",
)

original_ev = event_type_from_room_version("1")( # FIXME
if format_version is None:
# This means that we stored the event before we had the concept
# of a event format version, so it must be a V1 event.
format_version = EventFormatVersions.V1

original_ev = event_type_from_format_version(format_version)(
event_dict=d,
internal_metadata_dict=internal_metadata,
rejected_reason=rejected_reason,
Expand Down
2 changes: 1 addition & 1 deletion synapse/storage/prepare_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

# Remember to update this number every time a change is made to database
# schema files, so the users will be informed on server restarts.
SCHEMA_VERSION = 53
SCHEMA_VERSION = 54

dir_path = os.path.abspath(os.path.dirname(__file__))

Expand Down
16 changes: 16 additions & 0 deletions synapse/storage/schema/delta/54/event_format_version.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

ALTER TABLE event_json ADD COLUMN format_version INTEGER;
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved