Browse Source

Use insert_into when possible

pull/6437/head
Timshel 4 weeks ago
parent
commit
0a1fb0a1a9
  1. 2
      src/api/core/accounts.rs
  2. 22
      src/db/models/attachment.rs
  3. 30
      src/db/models/auth_request.rs
  4. 22
      src/db/models/cipher.rs
  5. 92
      src/db/models/collection.rs
  6. 9
      src/db/models/device.rs
  7. 22
      src/db/models/emergency_access.rs
  8. 27
      src/db/models/event.rs
  9. 33
      src/db/models/folder.rs
  10. 127
      src/db/models/group.rs
  11. 68
      src/db/models/organization.rs
  12. 22
      src/db/models/send.rs
  13. 16
      src/db/models/user.rs

2
src/api/core/accounts.rs

@ -1476,7 +1476,7 @@ async fn post_auth_request(
_ => err!("AuthRequest doesn't exist", "Device verification failed"), _ => err!("AuthRequest doesn't exist", "Device verification failed"),
}; };
let mut auth_request = AuthRequest::new( let auth_request = AuthRequest::new(
user.uuid.clone(), user.uuid.clone(),
data.device_identifier.clone(), data.device_identifier.clone(),
client_headers.device_type, client_headers.device_type,

22
src/db/models/attachment.rs

@ -77,24 +77,16 @@ use crate::error::MapResult;
impl Attachment { impl Attachment {
pub async fn save(&self, conn: &DbConn) -> EmptyResult { pub async fn save(&self, conn: &DbConn) -> EmptyResult {
db_run! { conn: db_run! { conn:
sqlite, mysql { mysql {
match diesel::replace_into(attachments::table) diesel::insert_into(attachments::table)
.values(self) .values(self)
.on_conflict(diesel::dsl::DuplicatedKeys)
.do_update()
.set(self)
.execute(conn) .execute(conn)
{ .map_res("Error saving attachment")
Ok(_) => Ok(()),
// Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first.
Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => {
diesel::update(attachments::table)
.filter(attachments::id.eq(&self.id))
.set(self)
.execute(conn)
.map_res("Error saving attachment")
}
Err(e) => Err(e.into()),
}.map_res("Error saving attachment")
} }
postgresql { postgresql, sqlite {
diesel::insert_into(attachments::table) diesel::insert_into(attachments::table)
.values(self) .values(self)
.on_conflict(attachments::id) .on_conflict(attachments::id)

30
src/db/models/auth_request.rs

@ -80,31 +80,23 @@ use crate::api::EmptyResult;
use crate::error::MapResult; use crate::error::MapResult;
impl AuthRequest { impl AuthRequest {
pub async fn save(&mut self, conn: &DbConn) -> EmptyResult { pub async fn save(&self, conn: &DbConn) -> EmptyResult {
db_run! { conn: db_run! { conn:
sqlite, mysql { mysql {
match diesel::replace_into(auth_requests::table) diesel::insert_into(auth_requests::table)
.values(&*self) .values(self)
.on_conflict(diesel::dsl::DuplicatedKeys)
.do_update()
.set(self)
.execute(conn) .execute(conn)
{ .map_res("Error saving auth_request")
Ok(_) => Ok(()),
// Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first.
Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => {
diesel::update(auth_requests::table)
.filter(auth_requests::uuid.eq(&self.uuid))
.set(&*self)
.execute(conn)
.map_res("Error auth_request")
}
Err(e) => Err(e.into()),
}.map_res("Error auth_request")
} }
postgresql { postgresql, sqlite {
diesel::insert_into(auth_requests::table) diesel::insert_into(auth_requests::table)
.values(&*self) .values(self)
.on_conflict(auth_requests::uuid) .on_conflict(auth_requests::uuid)
.do_update() .do_update()
.set(&*self) .set(self)
.execute(conn) .execute(conn)
.map_res("Error saving auth_request") .map_res("Error saving auth_request")
} }

22
src/db/models/cipher.rs

@ -438,24 +438,16 @@ impl Cipher {
self.updated_at = Utc::now().naive_utc(); self.updated_at = Utc::now().naive_utc();
db_run! { conn: db_run! { conn:
sqlite, mysql { mysql {
match diesel::replace_into(ciphers::table) diesel::insert_into(ciphers::table)
.values(&*self) .values(&*self)
.on_conflict(diesel::dsl::DuplicatedKeys)
.do_update()
.set(&*self)
.execute(conn) .execute(conn)
{ .map_res("Error saving cipher")
Ok(_) => Ok(()),
// Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first.
Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => {
diesel::update(ciphers::table)
.filter(ciphers::uuid.eq(&self.uuid))
.set(&*self)
.execute(conn)
.map_res("Error saving cipher")
}
Err(e) => Err(e.into()),
}.map_res("Error saving cipher")
} }
postgresql { postgresql, sqlite {
diesel::insert_into(ciphers::table) diesel::insert_into(ciphers::table)
.values(&*self) .values(&*self)
.on_conflict(ciphers::uuid) .on_conflict(ciphers::uuid)

92
src/db/models/collection.rs

@ -158,24 +158,16 @@ impl Collection {
self.update_users_revision(conn).await; self.update_users_revision(conn).await;
db_run! { conn: db_run! { conn:
sqlite, mysql { mysql {
match diesel::replace_into(collections::table) diesel::insert_into(collections::table)
.values(self) .values(self)
.on_conflict(diesel::dsl::DuplicatedKeys)
.do_update()
.set(self)
.execute(conn) .execute(conn)
{ .map_res("Error saving collection")
Ok(_) => Ok(()),
// Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first.
Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => {
diesel::update(collections::table)
.filter(collections::uuid.eq(&self.uuid))
.set(self)
.execute(conn)
.map_res("Error saving collection")
}
Err(e) => Err(e.into()),
}.map_res("Error saving collection")
} }
postgresql { postgresql, sqlite {
diesel::insert_into(collections::table) diesel::insert_into(collections::table)
.values(self) .values(self)
.on_conflict(collections::uuid) .on_conflict(collections::uuid)
@ -605,53 +597,30 @@ impl CollectionUser {
) -> EmptyResult { ) -> EmptyResult {
User::update_uuid_revision(user_uuid, conn).await; User::update_uuid_revision(user_uuid, conn).await;
let values = (
users_collections::user_uuid.eq(user_uuid),
users_collections::collection_uuid.eq(collection_uuid),
users_collections::read_only.eq(read_only),
users_collections::hide_passwords.eq(hide_passwords),
users_collections::manage.eq(manage),
);
db_run! { conn: db_run! { conn:
sqlite, mysql { mysql {
match diesel::replace_into(users_collections::table) diesel::insert_into(users_collections::table)
.values(( .values(values)
users_collections::user_uuid.eq(user_uuid), .on_conflict(diesel::dsl::DuplicatedKeys)
users_collections::collection_uuid.eq(collection_uuid), .do_update()
users_collections::read_only.eq(read_only), .set(values)
users_collections::hide_passwords.eq(hide_passwords),
users_collections::manage.eq(manage),
))
.execute(conn) .execute(conn)
{ .map_res("Error adding user to collection")
Ok(_) => Ok(()),
// Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first.
Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => {
diesel::update(users_collections::table)
.filter(users_collections::user_uuid.eq(user_uuid))
.filter(users_collections::collection_uuid.eq(collection_uuid))
.set((
users_collections::user_uuid.eq(user_uuid),
users_collections::collection_uuid.eq(collection_uuid),
users_collections::read_only.eq(read_only),
users_collections::hide_passwords.eq(hide_passwords),
users_collections::manage.eq(manage),
))
.execute(conn)
.map_res("Error adding user to collection")
}
Err(e) => Err(e.into()),
}.map_res("Error adding user to collection")
} }
postgresql { postgresql, sqlite {
diesel::insert_into(users_collections::table) diesel::insert_into(users_collections::table)
.values(( .values(values)
users_collections::user_uuid.eq(user_uuid),
users_collections::collection_uuid.eq(collection_uuid),
users_collections::read_only.eq(read_only),
users_collections::hide_passwords.eq(hide_passwords),
users_collections::manage.eq(manage),
))
.on_conflict((users_collections::user_uuid, users_collections::collection_uuid)) .on_conflict((users_collections::user_uuid, users_collections::collection_uuid))
.do_update() .do_update()
.set(( .set(values)
users_collections::read_only.eq(read_only),
users_collections::hide_passwords.eq(hide_passwords),
users_collections::manage.eq(manage),
))
.execute(conn) .execute(conn)
.map_res("Error adding user to collection") .map_res("Error adding user to collection")
} }
@ -767,19 +736,18 @@ impl CollectionCipher {
Self::update_users_revision(collection_uuid, conn).await; Self::update_users_revision(collection_uuid, conn).await;
db_run! { conn: db_run! { conn:
sqlite, mysql { mysql {
// Not checking for ForeignKey Constraints here. diesel::insert_into(ciphers_collections::table)
// Table ciphers_collections does not have ForeignKey Constraints which would cause conflicts.
// This table has no constraints pointing to itself, but only to others.
diesel::replace_into(ciphers_collections::table)
.values(( .values((
ciphers_collections::cipher_uuid.eq(cipher_uuid), ciphers_collections::cipher_uuid.eq(cipher_uuid),
ciphers_collections::collection_uuid.eq(collection_uuid), ciphers_collections::collection_uuid.eq(collection_uuid),
)) ))
.on_conflict(diesel::dsl::DuplicatedKeys)
.do_nothing()
.execute(conn) .execute(conn)
.map_res("Error adding cipher to collection") .map_res("Error adding cipher to collection")
} }
postgresql { postgresql, sqlite {
diesel::insert_into(ciphers_collections::table) diesel::insert_into(ciphers_collections::table)
.values(( .values((
ciphers_collections::cipher_uuid.eq(cipher_uuid), ciphers_collections::cipher_uuid.eq(cipher_uuid),

9
src/db/models/device.rs

@ -138,15 +138,18 @@ impl Device {
async fn inner_save(&self, conn: &DbConn) -> EmptyResult { async fn inner_save(&self, conn: &DbConn) -> EmptyResult {
db_run! { conn: db_run! { conn:
sqlite, mysql { mysql {
crate::util::retry(|| crate::util::retry(||
diesel::replace_into(devices::table) diesel::insert_into(devices::table)
.values(self) .values(self)
.on_conflict(diesel::dsl::DuplicatedKeys)
.do_update()
.set(self)
.execute(conn), .execute(conn),
10, 10,
).map_res("Error saving device") ).map_res("Error saving device")
} }
postgresql { postgresql, sqlite {
crate::util::retry(|| crate::util::retry(||
diesel::insert_into(devices::table) diesel::insert_into(devices::table)
.values(self) .values(self)

22
src/db/models/emergency_access.rs

@ -144,24 +144,16 @@ impl EmergencyAccess {
self.updated_at = Utc::now().naive_utc(); self.updated_at = Utc::now().naive_utc();
db_run! { conn: db_run! { conn:
sqlite, mysql { mysql {
match diesel::replace_into(emergency_access::table) diesel::insert_into(emergency_access::table)
.values(&*self) .values(&*self)
.on_conflict(diesel::dsl::DuplicatedKeys)
.do_update()
.set(&*self)
.execute(conn) .execute(conn)
{ .map_res("Error saving emergency access")
Ok(_) => Ok(()),
// Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first.
Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => {
diesel::update(emergency_access::table)
.filter(emergency_access::uuid.eq(&self.uuid))
.set(&*self)
.execute(conn)
.map_res("Error updating emergency access")
}
Err(e) => Err(e.into()),
}.map_res("Error saving emergency access")
} }
postgresql { postgresql, sqlite {
diesel::insert_into(emergency_access::table) diesel::insert_into(emergency_access::table)
.values(&*self) .values(&*self)
.on_conflict(emergency_access::uuid) .on_conflict(emergency_access::uuid)

27
src/db/models/event.rs

@ -195,20 +195,23 @@ impl Event {
/// Basic Queries /// Basic Queries
pub async fn save(&self, conn: &DbConn) -> EmptyResult { pub async fn save(&self, conn: &DbConn) -> EmptyResult {
db_run! { conn: db_run! { conn:
sqlite, mysql { mysql {
diesel::replace_into(event::table) diesel::insert_into(event::table)
.values(self) .values(self)
.execute(conn) .on_conflict(diesel::dsl::DuplicatedKeys)
.map_res("Error saving event") .do_update()
.set(self)
.execute(conn)
.map_res("Error saving event")
} }
postgresql { postgresql, sqlite {
diesel::insert_into(event::table) diesel::insert_into(event::table)
.values(self) .values(self)
.on_conflict(event::uuid) .on_conflict(event::uuid)
.do_update() .do_update()
.set(self) .set(self)
.execute(conn) .execute(conn)
.map_res("Error saving event") .map_res("Error saving event")
} }
} }
} }

33
src/db/models/folder.rs

@ -74,24 +74,16 @@ impl Folder {
self.updated_at = Utc::now().naive_utc(); self.updated_at = Utc::now().naive_utc();
db_run! { conn: db_run! { conn:
sqlite, mysql { mysql {
match diesel::replace_into(folders::table) diesel::insert_into(folders::table)
.values(&*self) .values(&*self)
.on_conflict(diesel::dsl::DuplicatedKeys)
.do_update()
.set(&*self)
.execute(conn) .execute(conn)
{ .map_res("Error saving folder")
Ok(_) => Ok(()),
// Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first.
Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => {
diesel::update(folders::table)
.filter(folders::uuid.eq(&self.uuid))
.set(&*self)
.execute(conn)
.map_res("Error saving folder")
}
Err(e) => Err(e.into()),
}.map_res("Error saving folder")
} }
postgresql { postgresql, sqlite {
diesel::insert_into(folders::table) diesel::insert_into(folders::table)
.values(&*self) .values(&*self)
.on_conflict(folders::uuid) .on_conflict(folders::uuid)
@ -144,16 +136,15 @@ impl Folder {
impl FolderCipher { impl FolderCipher {
pub async fn save(&self, conn: &DbConn) -> EmptyResult { pub async fn save(&self, conn: &DbConn) -> EmptyResult {
db_run! { conn: db_run! { conn:
sqlite, mysql { mysql {
// Not checking for ForeignKey Constraints here. diesel::insert_into(folders_ciphers::table)
// Table folders_ciphers does not have ForeignKey Constraints which would cause conflicts.
// This table has no constraints pointing to itself, but only to others.
diesel::replace_into(folders_ciphers::table)
.values(self) .values(self)
.on_conflict(diesel::dsl::DuplicatedKeys)
.do_nothing()
.execute(conn) .execute(conn)
.map_res("Error adding cipher to folder") .map_res("Error adding cipher to folder")
} }
postgresql { postgresql, sqlite {
diesel::insert_into(folders_ciphers::table) diesel::insert_into(folders_ciphers::table)
.values(self) .values(self)
.on_conflict((folders_ciphers::cipher_uuid, folders_ciphers::folder_uuid)) .on_conflict((folders_ciphers::cipher_uuid, folders_ciphers::folder_uuid))

127
src/db/models/group.rs

@ -160,24 +160,16 @@ impl Group {
self.revision_date = Utc::now().naive_utc(); self.revision_date = Utc::now().naive_utc();
db_run! { conn: db_run! { conn:
sqlite, mysql { mysql {
match diesel::replace_into(groups::table) diesel::insert_into(groups::table)
.values(&*self) .values(&*self)
.on_conflict(diesel::dsl::DuplicatedKeys)
.do_update()
.set(&*self)
.execute(conn) .execute(conn)
{ .map_res("Error saving group")
Ok(_) => Ok(()),
// Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first.
Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => {
diesel::update(groups::table)
.filter(groups::uuid.eq(&self.uuid))
.set(&*self)
.execute(conn)
.map_res("Error saving group")
}
Err(e) => Err(e.into()),
}.map_res("Error saving group")
} }
postgresql { postgresql, sqlite {
diesel::insert_into(groups::table) diesel::insert_into(groups::table)
.values(&*self) .values(&*self)
.on_conflict(groups::uuid) .on_conflict(groups::uuid)
@ -312,53 +304,30 @@ impl CollectionGroup {
group_user.update_user_revision(conn).await; group_user.update_user_revision(conn).await;
} }
let values = (
collections_groups::collections_uuid.eq(&self.collections_uuid),
collections_groups::groups_uuid.eq(&self.groups_uuid),
collections_groups::read_only.eq(&self.read_only),
collections_groups::hide_passwords.eq(&self.hide_passwords),
collections_groups::manage.eq(&self.manage),
);
db_run! { conn: db_run! { conn:
sqlite, mysql { mysql {
match diesel::replace_into(collections_groups::table) diesel::insert_into(collections_groups::table)
.values(( .values(values)
collections_groups::collections_uuid.eq(&self.collections_uuid), .on_conflict(diesel::dsl::DuplicatedKeys)
collections_groups::groups_uuid.eq(&self.groups_uuid), .do_update()
collections_groups::read_only.eq(&self.read_only), .set(values)
collections_groups::hide_passwords.eq(&self.hide_passwords),
collections_groups::manage.eq(&self.manage),
))
.execute(conn) .execute(conn)
{ .map_res("Error adding group to collection")
Ok(_) => Ok(()),
// Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first.
Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => {
diesel::update(collections_groups::table)
.filter(collections_groups::collections_uuid.eq(&self.collections_uuid))
.filter(collections_groups::groups_uuid.eq(&self.groups_uuid))
.set((
collections_groups::collections_uuid.eq(&self.collections_uuid),
collections_groups::groups_uuid.eq(&self.groups_uuid),
collections_groups::read_only.eq(&self.read_only),
collections_groups::hide_passwords.eq(&self.hide_passwords),
collections_groups::manage.eq(&self.manage),
))
.execute(conn)
.map_res("Error adding group to collection")
}
Err(e) => Err(e.into()),
}.map_res("Error adding group to collection")
} }
postgresql { postgresql, sqlite {
diesel::insert_into(collections_groups::table) diesel::insert_into(collections_groups::table)
.values(( .values(values)
collections_groups::collections_uuid.eq(&self.collections_uuid),
collections_groups::groups_uuid.eq(&self.groups_uuid),
collections_groups::read_only.eq(self.read_only),
collections_groups::hide_passwords.eq(self.hide_passwords),
collections_groups::manage.eq(self.manage),
))
.on_conflict((collections_groups::collections_uuid, collections_groups::groups_uuid)) .on_conflict((collections_groups::collections_uuid, collections_groups::groups_uuid))
.do_update() .do_update()
.set(( .set(values)
collections_groups::read_only.eq(self.read_only),
collections_groups::hide_passwords.eq(self.hide_passwords),
collections_groups::manage.eq(self.manage),
))
.execute(conn) .execute(conn)
.map_res("Error adding group to collection") .map_res("Error adding group to collection")
} }
@ -451,43 +420,25 @@ impl GroupUser {
pub async fn save(&mut self, conn: &DbConn) -> EmptyResult { pub async fn save(&mut self, conn: &DbConn) -> EmptyResult {
self.update_user_revision(conn).await; self.update_user_revision(conn).await;
let values = (
groups_users::users_organizations_uuid.eq(&self.users_organizations_uuid),
groups_users::groups_uuid.eq(&self.groups_uuid),
);
db_run! { conn: db_run! { conn:
sqlite, mysql { mysql {
match diesel::replace_into(groups_users::table) diesel::insert_into(groups_users::table)
.values(( .values(values)
groups_users::users_organizations_uuid.eq(&self.users_organizations_uuid), .on_conflict(diesel::dsl::DuplicatedKeys)
groups_users::groups_uuid.eq(&self.groups_uuid), .do_nothing()
))
.execute(conn) .execute(conn)
{ .map_res("Error adding user to group")
Ok(_) => Ok(()),
// Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first.
Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => {
diesel::update(groups_users::table)
.filter(groups_users::users_organizations_uuid.eq(&self.users_organizations_uuid))
.filter(groups_users::groups_uuid.eq(&self.groups_uuid))
.set((
groups_users::users_organizations_uuid.eq(&self.users_organizations_uuid),
groups_users::groups_uuid.eq(&self.groups_uuid),
))
.execute(conn)
.map_res("Error adding user to group")
}
Err(e) => Err(e.into()),
}.map_res("Error adding user to group")
} }
postgresql { postgresql, sqlite {
diesel::insert_into(groups_users::table) diesel::insert_into(groups_users::table)
.values(( .values(values)
groups_users::users_organizations_uuid.eq(&self.users_organizations_uuid),
groups_users::groups_uuid.eq(&self.groups_uuid),
))
.on_conflict((groups_users::users_organizations_uuid, groups_users::groups_uuid)) .on_conflict((groups_users::users_organizations_uuid, groups_users::groups_uuid))
.do_update() .do_nothing()
.set((
groups_users::users_organizations_uuid.eq(&self.users_organizations_uuid),
groups_users::groups_uuid.eq(&self.groups_uuid),
))
.execute(conn) .execute(conn)
.map_res("Error adding user to group") .map_res("Error adding user to group")
} }

68
src/db/models/organization.rs

@ -338,25 +338,16 @@ impl Organization {
} }
db_run! { conn: db_run! { conn:
sqlite, mysql { mysql {
match diesel::replace_into(organizations::table) diesel::insert_into(organizations::table)
.values(self) .values(self)
.on_conflict(diesel::dsl::DuplicatedKeys)
.do_update()
.set(self)
.execute(conn) .execute(conn)
{ .map_res("Error saving organization")
Ok(_) => Ok(()),
// Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first.
Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => {
diesel::update(organizations::table)
.filter(organizations::uuid.eq(&self.uuid))
.set(self)
.execute(conn)
.map_res("Error saving organization")
}
Err(e) => Err(e.into()),
}.map_res("Error saving organization")
} }
postgresql { postgresql, sqlite {
diesel::insert_into(organizations::table) diesel::insert_into(organizations::table)
.values(self) .values(self)
.on_conflict(organizations::uuid) .on_conflict(organizations::uuid)
@ -744,24 +735,16 @@ impl Membership {
User::update_uuid_revision(&self.user_uuid, conn).await; User::update_uuid_revision(&self.user_uuid, conn).await;
db_run! { conn: db_run! { conn:
sqlite, mysql { mysql {
match diesel::replace_into(users_organizations::table) diesel::insert_into(users_organizations::table)
.values(self) .values(self)
.on_conflict(diesel::dsl::DuplicatedKeys)
.do_update()
.set(self)
.execute(conn) .execute(conn)
{ .map_res("Error adding user to organization")
Ok(_) => Ok(()),
// Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first.
Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => {
diesel::update(users_organizations::table)
.filter(users_organizations::uuid.eq(&self.uuid))
.set(self)
.execute(conn)
.map_res("Error adding user to organization")
},
Err(e) => Err(e.into()),
}.map_res("Error adding user to organization")
} }
postgresql { postgresql, sqlite {
diesel::insert_into(users_organizations::table) diesel::insert_into(users_organizations::table)
.values(self) .values(self)
.on_conflict(users_organizations::uuid) .on_conflict(users_organizations::uuid)
@ -1147,25 +1130,16 @@ impl Membership {
impl OrganizationApiKey { impl OrganizationApiKey {
pub async fn save(&self, conn: &DbConn) -> EmptyResult { pub async fn save(&self, conn: &DbConn) -> EmptyResult {
db_run! { conn: db_run! { conn:
sqlite, mysql { mysql {
match diesel::replace_into(organization_api_key::table) diesel::insert_into(organization_api_key::table)
.values(self) .values(self)
.on_conflict(diesel::dsl::DuplicatedKeys)
.do_update()
.set(self)
.execute(conn) .execute(conn)
{ .map_res("Error saving organization")
Ok(_) => Ok(()),
// Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first.
Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => {
diesel::update(organization_api_key::table)
.filter(organization_api_key::uuid.eq(&self.uuid))
.set(self)
.execute(conn)
.map_res("Error saving organization")
}
Err(e) => Err(e.into()),
}.map_res("Error saving organization")
} }
postgresql { postgresql, sqlite {
diesel::insert_into(organization_api_key::table) diesel::insert_into(organization_api_key::table)
.values(self) .values(self)
.on_conflict((organization_api_key::uuid, organization_api_key::org_uuid)) .on_conflict((organization_api_key::uuid, organization_api_key::org_uuid))

22
src/db/models/send.rs

@ -192,24 +192,16 @@ impl Send {
self.revision_date = Utc::now().naive_utc(); self.revision_date = Utc::now().naive_utc();
db_run! { conn: db_run! { conn:
sqlite, mysql { mysql {
match diesel::replace_into(sends::table) diesel::insert_into(sends::table)
.values(&*self) .values(&*self)
.on_conflict(diesel::dsl::DuplicatedKeys)
.do_update()
.set(&*self)
.execute(conn) .execute(conn)
{ .map_res("Error saving send")
Ok(_) => Ok(()),
// Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first.
Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => {
diesel::update(sends::table)
.filter(sends::uuid.eq(&self.uuid))
.set(&*self)
.execute(conn)
.map_res("Error saving send")
}
Err(e) => Err(e.into()),
}.map_res("Error saving send")
} }
postgresql { postgresql, sqlite {
diesel::insert_into(sends::table) diesel::insert_into(sends::table)
.values(&*self) .values(&*self)
.on_conflict(sends::uuid) .on_conflict(sends::uuid)

16
src/db/models/user.rs

@ -432,15 +432,15 @@ impl Invitation {
} }
db_run! { conn: db_run! { conn:
sqlite, mysql { mysql {
// Not checking for ForeignKey Constraints here diesel::insert_into(invitations::table)
// Table invitations does not have any ForeignKey Constraints.
diesel::replace_into(invitations::table)
.values(self) .values(self)
.on_conflict(diesel::dsl::DuplicatedKeys)
.do_nothing()
.execute(conn) .execute(conn)
.map_res("Error saving invitation") .map_res("Error saving invitation")
} }
postgresql { postgresql, sqlite {
diesel::insert_into(invitations::table) diesel::insert_into(invitations::table)
.values(self) .values(self)
.on_conflict(invitations::email) .on_conflict(invitations::email)
@ -500,13 +500,13 @@ pub struct UserId(String);
impl SsoUser { impl SsoUser {
pub async fn save(&self, conn: &DbConn) -> EmptyResult { pub async fn save(&self, conn: &DbConn) -> EmptyResult {
db_run! { conn: db_run! { conn:
sqlite, mysql { mysql {
diesel::replace_into(sso_users::table) diesel::insert_into(sso_users::table)
.values(self) .values(self)
.execute(conn) .execute(conn)
.map_res("Error saving SSO user") .map_res("Error saving SSO user")
} }
postgresql { postgresql, sqlite {
diesel::insert_into(sso_users::table) diesel::insert_into(sso_users::table)
.values(self) .values(self)
.execute(conn) .execute(conn)

Loading…
Cancel
Save