Disambiguate queries on state_key (#11497)

We're going to add a `state_key` column to the `events` table, so we need to
add some disambiguation to queries which use it.
This commit is contained in:
Richard van der Hoff 2021-12-02 22:42:58 +00:00 committed by GitHub
parent d26808dd85
commit 5640992d17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 21 additions and 16 deletions

1
changelog.d/11497.misc Normal file
View File

@ -0,0 +1 @@
Preparation for database schema simplifications: disambiguate queries on `state_key`.

View File

@ -1552,9 +1552,9 @@ class EventFederationStore(EventFederationWorkerStore):
DELETE FROM event_auth DELETE FROM event_auth
WHERE event_id IN ( WHERE event_id IN (
SELECT event_id FROM events SELECT event_id FROM events
LEFT JOIN state_events USING (room_id, event_id) LEFT JOIN state_events AS se USING (room_id, event_id)
WHERE ? <= stream_ordering AND stream_ordering < ? WHERE ? <= stream_ordering AND stream_ordering < ?
AND state_key IS null AND se.state_key IS null
) )
""" """

View File

@ -575,9 +575,9 @@ class PersistEventsStore:
# fetch their auth event info. # fetch their auth event info.
while missing_auth_chains: while missing_auth_chains:
sql = """ sql = """
SELECT event_id, events.type, state_key, chain_id, sequence_number SELECT event_id, events.type, se.state_key, chain_id, sequence_number
FROM events FROM events
INNER JOIN state_events USING (event_id) INNER JOIN state_events AS se USING (event_id)
LEFT JOIN event_auth_chains USING (event_id) LEFT JOIN event_auth_chains USING (event_id)
WHERE WHERE
""" """

View File

@ -1408,10 +1408,10 @@ class EventsWorkerStore(SQLBaseStore):
) -> List[Tuple[int, str, str, str, str, str, str, str, str]]: ) -> List[Tuple[int, str, str, str, str, str, str, str, str]]:
sql = ( sql = (
"SELECT e.stream_ordering, e.event_id, e.room_id, e.type," "SELECT e.stream_ordering, e.event_id, e.room_id, e.type,"
" state_key, redacts, relates_to_id, membership, rejections.reason IS NOT NULL" " se.state_key, redacts, relates_to_id, membership, rejections.reason IS NOT NULL"
" FROM events AS e" " FROM events AS e"
" LEFT JOIN redactions USING (event_id)" " LEFT JOIN redactions USING (event_id)"
" LEFT JOIN state_events USING (event_id)" " LEFT JOIN state_events AS se USING (event_id)"
" LEFT JOIN event_relations USING (event_id)" " LEFT JOIN event_relations USING (event_id)"
" LEFT JOIN room_memberships USING (event_id)" " LEFT JOIN room_memberships USING (event_id)"
" LEFT JOIN rejections USING (event_id)" " LEFT JOIN rejections USING (event_id)"
@ -1449,11 +1449,11 @@ class EventsWorkerStore(SQLBaseStore):
) -> List[Tuple[int, str, str, str, str, str, str, str, str]]: ) -> List[Tuple[int, str, str, str, str, str, str, str, str]]:
sql = ( sql = (
"SELECT event_stream_ordering, e.event_id, e.room_id, e.type," "SELECT event_stream_ordering, e.event_id, e.room_id, e.type,"
" state_key, redacts, relates_to_id, membership, rejections.reason IS NOT NULL" " se.state_key, redacts, relates_to_id, membership, rejections.reason IS NOT NULL"
" FROM events AS e" " FROM events AS e"
" INNER JOIN ex_outlier_stream AS out USING (event_id)" " INNER JOIN ex_outlier_stream AS out USING (event_id)"
" LEFT JOIN redactions USING (event_id)" " LEFT JOIN redactions USING (event_id)"
" LEFT JOIN state_events USING (event_id)" " LEFT JOIN state_events AS se USING (event_id)"
" LEFT JOIN event_relations USING (event_id)" " LEFT JOIN event_relations USING (event_id)"
" LEFT JOIN room_memberships USING (event_id)" " LEFT JOIN room_memberships USING (event_id)"
" LEFT JOIN rejections USING (event_id)" " LEFT JOIN rejections USING (event_id)"
@ -1507,10 +1507,10 @@ class EventsWorkerStore(SQLBaseStore):
) -> Tuple[List[Tuple[int, Tuple[str, str, str, str, str, str]]], int, bool]: ) -> Tuple[List[Tuple[int, Tuple[str, str, str, str, str, str]]], int, bool]:
sql = ( sql = (
"SELECT -e.stream_ordering, e.event_id, e.room_id, e.type," "SELECT -e.stream_ordering, e.event_id, e.room_id, e.type,"
" state_key, redacts, relates_to_id" " se.state_key, redacts, relates_to_id"
" FROM events AS e" " FROM events AS e"
" LEFT JOIN redactions USING (event_id)" " LEFT JOIN redactions USING (event_id)"
" LEFT JOIN state_events USING (event_id)" " LEFT JOIN state_events AS se USING (event_id)"
" LEFT JOIN event_relations USING (event_id)" " LEFT JOIN event_relations USING (event_id)"
" WHERE ? > stream_ordering AND stream_ordering >= ?" " WHERE ? > stream_ordering AND stream_ordering >= ?"
" AND instance_name = ?" " AND instance_name = ?"
@ -1537,11 +1537,11 @@ class EventsWorkerStore(SQLBaseStore):
sql = ( sql = (
"SELECT -event_stream_ordering, e.event_id, e.room_id, e.type," "SELECT -event_stream_ordering, e.event_id, e.room_id, e.type,"
" state_key, redacts, relates_to_id" " se.state_key, redacts, relates_to_id"
" FROM events AS e" " FROM events AS e"
" INNER JOIN ex_outlier_stream AS out USING (event_id)" " INNER JOIN ex_outlier_stream AS out USING (event_id)"
" LEFT JOIN redactions USING (event_id)" " LEFT JOIN redactions USING (event_id)"
" LEFT JOIN state_events USING (event_id)" " LEFT JOIN state_events AS se USING (event_id)"
" LEFT JOIN event_relations USING (event_id)" " LEFT JOIN event_relations USING (event_id)"
" WHERE ? > event_stream_ordering" " WHERE ? > event_stream_ordering"
" AND event_stream_ordering >= ?" " AND event_stream_ordering >= ?"

View File

@ -118,7 +118,7 @@ class PurgeEventsStore(StateGroupWorkerStore, CacheInvalidationWorkerStore):
logger.info("[purge] looking for events to delete") logger.info("[purge] looking for events to delete")
should_delete_expr = "state_key IS NULL" should_delete_expr = "state_events.state_key IS NULL"
should_delete_params: Tuple[Any, ...] = () should_delete_params: Tuple[Any, ...] = ()
if not delete_local_events: if not delete_local_events:
should_delete_expr += " AND event_id NOT LIKE ?" should_delete_expr += " AND event_id NOT LIKE ?"

View File

@ -476,7 +476,7 @@ class RoomMemberWorkerStore(EventsWorkerStore):
INNER JOIN events AS e USING (room_id, event_id) INNER JOIN events AS e USING (room_id, event_id)
WHERE WHERE
c.type = 'm.room.member' c.type = 'm.room.member'
AND state_key = ? AND c.state_key = ?
AND c.membership = ? AND c.membership = ?
""" """
else: else:
@ -487,7 +487,7 @@ class RoomMemberWorkerStore(EventsWorkerStore):
INNER JOIN events AS e USING (room_id, event_id) INNER JOIN events AS e USING (room_id, event_id)
WHERE WHERE
c.type = 'm.room.member' c.type = 'm.room.member'
AND state_key = ? AND c.state_key = ?
AND m.membership = ? AND m.membership = ?
""" """

View File

@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
SCHEMA_VERSION = 65 # remember to update the list below when updating SCHEMA_VERSION = 66 # remember to update the list below when updating
"""Represents the expectations made by the codebase about the database schema """Represents the expectations made by the codebase about the database schema
This should be incremented whenever the codebase changes its requirements on the This should be incremented whenever the codebase changes its requirements on the
@ -46,6 +46,10 @@ Changes in SCHEMA_VERSION = 65:
- MSC2716: Remove unique event_id constraint from insertion_event_edges - MSC2716: Remove unique event_id constraint from insertion_event_edges
because an insertion event can have multiple edges. because an insertion event can have multiple edges.
- Remove unused tables `user_stats_historical` and `room_stats_historical`. - Remove unused tables `user_stats_historical` and `room_stats_historical`.
Changes in SCHEMA_VERSION = 66:
- Queries on state_key columns are now disambiguated (ie, the codebase can handle
the `events` table having a `state_key` column).
""" """