Fix stale server queries, use foreign keys from connectionsn to servers

This commit is contained in:
Max Brunsfeld 2022-12-14 17:34:24 -08:00
parent 363e3cae4b
commit 6c58a4f885
10 changed files with 128 additions and 122 deletions

View file

@ -1,4 +1,4 @@
use super::{ProjectId, RoomId, UserId};
use super::{ProjectId, RoomId, ServerId, UserId};
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
@ -9,7 +9,7 @@ pub struct Model {
pub room_id: RoomId,
pub host_user_id: UserId,
pub host_connection_id: i32,
pub host_connection_epoch: i32,
pub host_connection_server_id: ServerId,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]

View file

@ -1,4 +1,4 @@
use super::{ProjectCollaboratorId, ProjectId, ReplicaId, UserId};
use super::{ProjectCollaboratorId, ProjectId, ReplicaId, ServerId, UserId};
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
@ -8,7 +8,7 @@ pub struct Model {
pub id: ProjectCollaboratorId,
pub project_id: ProjectId,
pub connection_id: i32,
pub connection_epoch: i32,
pub connection_server_id: ServerId,
pub user_id: UserId,
pub replica_id: ReplicaId,
pub is_host: bool,

View file

@ -1,4 +1,4 @@
use super::{ProjectId, RoomId, RoomParticipantId, UserId};
use super::{ProjectId, RoomId, RoomParticipantId, ServerId, UserId};
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
@ -9,14 +9,14 @@ pub struct Model {
pub room_id: RoomId,
pub user_id: UserId,
pub answering_connection_id: Option<i32>,
pub answering_connection_epoch: Option<i32>,
pub answering_connection_server_id: Option<ServerId>,
pub answering_connection_lost: bool,
pub location_kind: Option<i32>,
pub location_project_id: Option<ProjectId>,
pub initial_project_id: Option<ProjectId>,
pub calling_user_id: UserId,
pub calling_connection_id: i32,
pub calling_connection_epoch: i32,
pub calling_connection_server_id: Option<ServerId>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]

View file

@ -1,11 +1,11 @@
use super::ServerEpoch;
use super::ServerId;
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "servers")]
pub struct Model {
#[sea_orm(primary_key)]
pub epoch: ServerEpoch,
pub id: ServerId,
pub environment: String,
}

View file

@ -410,6 +410,8 @@ test_both_dbs!(
test_project_count_sqlite,
db,
{
let epoch = db.create_server("test").await.unwrap().0 as u32;
let user1 = db
.create_user(
&format!("admin@example.com"),
@ -436,7 +438,7 @@ test_both_dbs!(
.unwrap();
let room_id = RoomId::from_proto(
db.create_room(user1.user_id, ConnectionId { epoch: 0, id: 0 }, "")
db.create_room(user1.user_id, ConnectionId { epoch, id: 0 }, "")
.await
.unwrap()
.id,
@ -444,36 +446,34 @@ test_both_dbs!(
db.call(
room_id,
user1.user_id,
ConnectionId { epoch: 0, id: 0 },
ConnectionId { epoch, id: 0 },
user2.user_id,
None,
)
.await
.unwrap();
db.join_room(room_id, user2.user_id, ConnectionId { epoch: 0, id: 1 })
db.join_room(room_id, user2.user_id, ConnectionId { epoch, id: 1 })
.await
.unwrap();
assert_eq!(db.project_count_excluding_admins().await.unwrap(), 0);
db.share_project(room_id, ConnectionId { epoch: 0, id: 1 }, &[])
db.share_project(room_id, ConnectionId { epoch, id: 1 }, &[])
.await
.unwrap();
assert_eq!(db.project_count_excluding_admins().await.unwrap(), 1);
db.share_project(room_id, ConnectionId { epoch: 0, id: 1 }, &[])
db.share_project(room_id, ConnectionId { epoch, id: 1 }, &[])
.await
.unwrap();
assert_eq!(db.project_count_excluding_admins().await.unwrap(), 2);
// Projects shared by admins aren't counted.
db.share_project(room_id, ConnectionId { epoch: 0, id: 0 }, &[])
db.share_project(room_id, ConnectionId { epoch, id: 0 }, &[])
.await
.unwrap();
assert_eq!(db.project_count_excluding_admins().await.unwrap(), 2);
db.leave_room(ConnectionId { epoch: 0, id: 1 })
.await
.unwrap();
db.leave_room(ConnectionId { epoch, id: 1 }).await.unwrap();
assert_eq!(db.project_count_excluding_admins().await.unwrap(), 0);
}
);