Add FK restraints and IF NOT EXISTS
parent
3daf421742
commit
9503e172f3
|
@ -939,7 +939,7 @@ class RegistrationStore(RegistrationBackgroundUpdateStore):
|
||||||
token: str,
|
token: str,
|
||||||
device_id: Optional[str],
|
device_id: Optional[str],
|
||||||
valid_until_ms: Optional[int],
|
valid_until_ms: Optional[int],
|
||||||
) -> None:
|
) -> int:
|
||||||
"""Adds an access token for the given user.
|
"""Adds an access token for the given user.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
@ -949,6 +949,8 @@ class RegistrationStore(RegistrationBackgroundUpdateStore):
|
||||||
valid_until_ms: when the token is valid until. None for no expiry.
|
valid_until_ms: when the token is valid until. None for no expiry.
|
||||||
Raises:
|
Raises:
|
||||||
StoreError if there was a problem adding this.
|
StoreError if there was a problem adding this.
|
||||||
|
Returns:
|
||||||
|
The token ID
|
||||||
"""
|
"""
|
||||||
next_id = self._access_tokens_id_gen.get_next()
|
next_id = self._access_tokens_id_gen.get_next()
|
||||||
|
|
||||||
|
@ -964,6 +966,8 @@ class RegistrationStore(RegistrationBackgroundUpdateStore):
|
||||||
desc="add_access_token_to_user",
|
desc="add_access_token_to_user",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return next_id
|
||||||
|
|
||||||
def _set_device_for_access_token_txn(self, txn, token: str, device_id: str) -> str:
|
def _set_device_for_access_token_txn(self, txn, token: str, device_id: str) -> str:
|
||||||
old_device_id = self.db_pool.simple_select_one_onecol_txn(
|
old_device_id = self.db_pool.simple_select_one_onecol_txn(
|
||||||
txn, "access_tokens", {"token": token}, "device_id"
|
txn, "access_tokens", {"token": token}, "device_id"
|
||||||
|
|
|
@ -17,16 +17,23 @@
|
||||||
-- A map of recent events persisted with transaction IDs. Used to deduplicate
|
-- A map of recent events persisted with transaction IDs. Used to deduplicate
|
||||||
-- send event requests with the same transaction ID.
|
-- send event requests with the same transaction ID.
|
||||||
--
|
--
|
||||||
-- Note, transaction IDs are scoped to the user ID/access token that was used to
|
-- Note: transaction IDs are scoped to the room ID/user ID/access token that was
|
||||||
-- make the request.
|
-- used to make the request.
|
||||||
CREATE TABLE event_txn_id (
|
--
|
||||||
|
-- Note: The foreign key constraints are ON DELETE CASCADE, as if we delete the
|
||||||
|
-- events or access token we don't want to try and de-duplicate the event.
|
||||||
|
CREATE TABLE IF NOT EXISTS event_txn_id (
|
||||||
event_id TEXT NOT NULL,
|
event_id TEXT NOT NULL,
|
||||||
user_id TEXT NOT NULL,
|
user_id TEXT NOT NULL,
|
||||||
token_id BIGINT NOT NULL,
|
token_id BIGINT NOT NULL,
|
||||||
txn_id TEXT NOT NULL,
|
txn_id TEXT NOT NULL,
|
||||||
inserted_ts BIGINT NOT NULL
|
inserted_ts BIGINT NOT NULL,
|
||||||
|
FOREIGN KEY (event_id)
|
||||||
|
REFERENCES events (event_id) ON DELETE CASCADE,
|
||||||
|
FOREIGN KEY (token_id)
|
||||||
|
REFERENCES access_tokens (id) ON DELETE CASCADE
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE UNIQUE INDEX event_txn_id_event_id ON event_txn_id(event_id);
|
CREATE UNIQUE INDEX IF NOT EXISTS event_txn_id_event_id ON event_txn_id(event_id);
|
||||||
CREATE UNIQUE INDEX event_txn_id_txn_id ON event_txn_id(user_id, token_id, txn_id);
|
CREATE UNIQUE INDEX IF NOT EXISTS event_txn_id_txn_id ON event_txn_id(user_id, token_id, txn_id);
|
||||||
CREATE INDEX event_txn_id_ts ON event_txn_id(inserted_ts);
|
CREATE INDEX IF NOT EXISTS event_txn_id_ts ON event_txn_id(inserted_ts);
|
||||||
|
|
|
@ -44,12 +44,15 @@ class EventCreationTestCase(unittest.HomeserverTestCase):
|
||||||
access_token = self.login("tester", "foobar")
|
access_token = self.login("tester", "foobar")
|
||||||
room_id = self.helper.create_room_as(user_id, tok=access_token)
|
room_id = self.helper.create_room_as(user_id, tok=access_token)
|
||||||
|
|
||||||
# We make the IDs up here, which is fine.
|
info = self.get_success(
|
||||||
token_id = 4957834
|
self.hs.get_datastore().get_user_by_access_token(access_token,)
|
||||||
txn_id = "something_suitably_random"
|
)
|
||||||
|
token_id = info["token_id"]
|
||||||
|
|
||||||
requester = create_requester(user_id, access_token_id=token_id)
|
requester = create_requester(user_id, access_token_id=token_id)
|
||||||
|
|
||||||
|
txn_id = "something_suitably_random"
|
||||||
|
|
||||||
def create_duplicate_event():
|
def create_duplicate_event():
|
||||||
return self.get_success(
|
return self.get_success(
|
||||||
handler.create_event(
|
handler.create_event(
|
||||||
|
|
|
@ -254,17 +254,24 @@ class HomeserverTestCase(TestCase):
|
||||||
if hasattr(self, "user_id"):
|
if hasattr(self, "user_id"):
|
||||||
if self.hijack_auth:
|
if self.hijack_auth:
|
||||||
|
|
||||||
|
# We need a valid token ID to satisfy foreign key constraints.
|
||||||
|
token_id = self.get_success(
|
||||||
|
self.hs.get_datastore().add_access_token_to_user(
|
||||||
|
self.helper.auth_user_id, "some_fake_token", None, None,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
async def get_user_by_access_token(token=None, allow_guest=False):
|
async def get_user_by_access_token(token=None, allow_guest=False):
|
||||||
return {
|
return {
|
||||||
"user": UserID.from_string(self.helper.auth_user_id),
|
"user": UserID.from_string(self.helper.auth_user_id),
|
||||||
"token_id": 1,
|
"token_id": token_id,
|
||||||
"is_guest": False,
|
"is_guest": False,
|
||||||
}
|
}
|
||||||
|
|
||||||
async def get_user_by_req(request, allow_guest=False, rights="access"):
|
async def get_user_by_req(request, allow_guest=False, rights="access"):
|
||||||
return create_requester(
|
return create_requester(
|
||||||
UserID.from_string(self.helper.auth_user_id),
|
UserID.from_string(self.helper.auth_user_id),
|
||||||
1,
|
token_id,
|
||||||
False,
|
False,
|
||||||
False,
|
False,
|
||||||
None,
|
None,
|
||||||
|
|
Loading…
Reference in New Issue