diff options
author | BlackDex <[email protected]> | 2020-09-22 12:13:02 +0200 |
---|---|---|
committer | BlackDex <[email protected]> | 2020-09-22 12:13:02 +0200 |
commit | 978be0b4a9a904a2ffbd227821cf8f14cf4e4243 (patch) | |
tree | 6e80a6b062116155b0e1232d2b78e8dd8df14a9b /src/db/models/org_policy.rs | |
parent | 2f3e18caa98271f9273e39e999f55635975091aa (diff) | |
download | vaultwarden-978be0b4a9a904a2ffbd227821cf8f14cf4e4243.tar.gz vaultwarden-978be0b4a9a904a2ffbd227821cf8f14cf4e4243.zip |
Fixed foreign-key (mariadb) errors.
When using MariaDB v10.5+ Foreign-Key errors were popping up because of
some changes in that version. To mitigate this on MariaDB and other
MySQL forks those errors are now catched, and instead of a replace_into
an update will happen. I have tested this as thorough as possible with
MariaDB 10.5, 10.4, 10.3 and the default MySQL on Ubuntu Focal. And
tested it again using sqlite, all seems to be ok on all tables.
resolves #1081. resolves #1065, resolves #1050
Diffstat (limited to 'src/db/models/org_policy.rs')
-rw-r--r-- | src/db/models/org_policy.rs | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/src/db/models/org_policy.rs b/src/db/models/org_policy.rs index a58ca53f..fa18ac9d 100644 --- a/src/db/models/org_policy.rs +++ b/src/db/models/org_policy.rs @@ -56,12 +56,23 @@ impl OrgPolicy { /// Database methods impl OrgPolicy { pub fn save(&self, conn: &DbConn) -> EmptyResult { - db_run! { conn: + db_run! { conn: sqlite, mysql { - diesel::replace_into(org_policies::table) + match diesel::replace_into(org_policies::table) .values(OrgPolicyDb::to_db(self)) .execute(conn) - .map_res("Error saving org_policy") + { + 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(org_policies::table) + .filter(org_policies::uuid.eq(&self.uuid)) + .set(OrgPolicyDb::to_db(self)) + .execute(conn) + .map_res("Error saving org_policy") + } + Err(e) => Err(e.into()), + }.map_res("Error saving org_policy") } postgresql { let value = OrgPolicyDb::to_db(self); |