Skip to content

Commit

Permalink
implement migrations for all client tables
Browse files Browse the repository at this point in the history
  • Loading branch information
kkohbrok committed Sep 12, 2024
1 parent 259aed3 commit 343ecb7
Show file tree
Hide file tree
Showing 39 changed files with 474 additions and 219 deletions.
2 changes: 2 additions & 0 deletions coreclient/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ image = "0.25.1"
kamadak-exif = "0.5.5"

# Persistence
refinery = { version = "0.8", features = ["rusqlite", "enums"] }
barrel = { version = "0.7", features = ["sqlite3"] }
async-trait = "0.1"
serde = { version = "1", features = ["derive"] }
rusqlite = { version = "0.30", features = ["bundled", "uuid", "chrono"] }
Expand Down
12 changes: 12 additions & 0 deletions coreclient/migrations/V10__create_conversation_messages_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::conversations::messages::ConversationMessage;
use crate::utils::persistence::Storable;

pub fn migration() -> String {
<ConversationMessage as Storable>::CREATE_TABLE_STATEMENT.to_string()
}
12 changes: 12 additions & 0 deletions coreclient/migrations/V11__create_as_credentials_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::key_stores::as_credentials::AsCredentials;
use crate::utils::persistence::Storable;

pub fn migration() -> String {
<AsCredentials as Storable>::CREATE_TABLE_STATEMENT.to_string()
}
12 changes: 12 additions & 0 deletions coreclient/migrations/V12__create_leaf_keys_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::key_stores::leaf_keys::LeafKeys;
use crate::utils::persistence::Storable;

pub fn migration() -> String {
<LeafKeys as Storable>::CREATE_TABLE_STATEMENT.to_string()
}
12 changes: 12 additions & 0 deletions coreclient/migrations/V13__create_qs_verifying_keys_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::key_stores::qs_verifying_keys::StorableQsVerifyingKey;
use crate::utils::persistence::Storable;

pub fn migration() -> String {
<StorableQsVerifyingKey as Storable>::CREATE_TABLE_STATEMENT.to_string()
}
14 changes: 14 additions & 0 deletions coreclient/migrations/V14__create_queue_ratchets_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::key_stores::queue_ratchets::StorableAsQueueRatchet;
use crate::utils::persistence::Storable;

/// The table for queue ratchets contains both the AsQueueRatchet and the
/// QsQueueRatchet.
pub fn migration() -> String {
<StorableAsQueueRatchet as Storable>::CREATE_TABLE_STATEMENT.to_string()
}
13 changes: 13 additions & 0 deletions coreclient/migrations/V15__create_group_data_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::groups::openmls_provider::group_data::StorableGroupData;
use crate::utils::persistence::Storable;

/// OpenMLS provider data
pub fn migration() -> String {
<StorableGroupData<u8> as Storable>::CREATE_TABLE_STATEMENT.to_string()
}
13 changes: 13 additions & 0 deletions coreclient/migrations/V16__create_leaf_nodes_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::groups::openmls_provider::own_leaf_nodes::StorableLeafNode;
use crate::utils::persistence::Storable;

/// OpenMLS provider data
pub fn migration() -> String {
<StorableLeafNode<u8> as Storable>::CREATE_TABLE_STATEMENT.to_string()
}
13 changes: 13 additions & 0 deletions coreclient/migrations/V17__create_proposals_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::groups::openmls_provider::proposals::StorableProposal;
use crate::utils::persistence::Storable;

/// OpenMLS provider data
pub fn migration() -> String {
<StorableProposal<u8, u8> as Storable>::CREATE_TABLE_STATEMENT.to_string()
}
13 changes: 13 additions & 0 deletions coreclient/migrations/V18__create_signature_keypairs_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::groups::openmls_provider::signature_key_pairs::StorableSignatureKeyPairs;
use crate::utils::persistence::Storable;

/// OpenMLS provider data
pub fn migration() -> String {
<StorableSignatureKeyPairs<u8> as Storable>::CREATE_TABLE_STATEMENT.to_string()
}
13 changes: 13 additions & 0 deletions coreclient/migrations/V19__create_epoch_key_pairs_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::groups::openmls_provider::epoch_key_pairs::StorableEpochKeyPairs;
use crate::utils::persistence::Storable;

/// OpenMLS provider data
pub fn migration() -> String {
<StorableEpochKeyPairs<u8> as Storable>::CREATE_TABLE_STATEMENT.to_string()
}
12 changes: 12 additions & 0 deletions coreclient/migrations/V1__create_user_creation_state_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::clients::store::UserCreationState;
use crate::utils::persistence::Storable;

pub fn migration() -> String {
<UserCreationState as Storable>::CREATE_TABLE_STATEMENT.to_string()
}
13 changes: 13 additions & 0 deletions coreclient/migrations/V20__create_encryption_key_pairs_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::groups::openmls_provider::encryption_key_pairs::StorableEncryptionKeyPair;
use crate::utils::persistence::Storable;

/// OpenMLS provider data
pub fn migration() -> String {
<StorableEncryptionKeyPair<u8> as Storable>::CREATE_TABLE_STATEMENT.to_string()
}
13 changes: 13 additions & 0 deletions coreclient/migrations/V21__create_key_packages_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::groups::openmls_provider::key_packages::StorableKeyPackage;
use crate::utils::persistence::Storable;

/// OpenMLS provider data
pub fn migration() -> String {
<StorableKeyPackage<u8> as Storable>::CREATE_TABLE_STATEMENT.to_string()
}
13 changes: 13 additions & 0 deletions coreclient/migrations/V22__create_psk_bundles_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::groups::openmls_provider::psks::StorablePskBundle;
use crate::utils::persistence::Storable;

/// OpenMLS provider data
pub fn migration() -> String {
<StorablePskBundle<u8> as Storable>::CREATE_TABLE_STATEMENT.to_string()
}
32 changes: 32 additions & 0 deletions coreclient/migrations/V23__create_group_membership_trigger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::groups::openmls_provider::key_packages::StorableKeyPackage;
use crate::utils::persistence::Storable;

/// OpenMLS provider data
pub fn migration() -> String {
"CREATE TRIGGER IF NOT EXISTS delete_orphaned_data
AFTER DELETE ON group_membership
FOR EACH ROW
BEGIN
-- Delete client credentials if they are not our own and not used in any group.
DELETE FROM client_credentials
WHERE fingerprint = OLD.client_credential_fingerprint AND NOT EXISTS (
SELECT 1 FROM group_membership WHERE client_credential_fingerprint = OLD.client_credential_fingerprint
) AND NOT EXISTS (
SELECT 1 FROM own_client_info WHERE as_client_uuid = OLD.client_uuid
);
-- Delete user profiles of users that are not in any group and that are not our own.
DELETE FROM users
WHERE user_name = OLD.user_name AND NOT EXISTS (
SELECT 1 FROM group_membership WHERE user_name = OLD.user_name
) AND NOT EXISTS (
SELECT 1 FROM own_client_info WHERE as_user_name = OLD.user_name
);
END".to_string()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::groups::openmls_provider::key_packages::StorableKeyPackage;
use crate::utils::persistence::Storable;

/// OpenMLS provider data
pub fn migration() -> String {
"DROP TRIGGER IF EXISTS no_partial_contact_overlap_on_insert;
CREATE TRIGGER no_partial_contact_overlap_on_insert
BEFORE INSERT ON contacts
FOR EACH ROW
BEGIN
SELECT CASE
WHEN EXISTS (SELECT 1 FROM partial_contacts WHERE user_name = NEW.user_name)
THEN RAISE(FAIL, 'Can''t insert Contact: There already exists a partial contact with this user_name')
END;
END;".to_string()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::groups::openmls_provider::key_packages::StorableKeyPackage;
use crate::utils::persistence::Storable;

/// OpenMLS provider data
pub fn migration() -> String {
"DROP TRIGGER IF EXISTS no_partial_contact_overlap_on_update;
CREATE TRIGGER no_partial_contact_overlap_on_update
BEFORE UPDATE ON contacts
FOR EACH ROW
BEGIN
SELECT CASE
WHEN EXISTS (SELECT 1 FROM partial_contacts WHERE user_name = NEW.user_name)
THEN RAISE(FAIL, 'Can''t update Contact: There already exists a partial contact with this user_name')
END;
END;".to_string()
}
23 changes: 23 additions & 0 deletions coreclient/migrations/V26__create_contacts_insert_trigger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::groups::openmls_provider::key_packages::StorableKeyPackage;
use crate::utils::persistence::Storable;

/// OpenMLS provider data
pub fn migration() -> String {
"DROP TRIGGER IF EXISTS no_contact_overlap_on_insert;
CREATE TRIGGER no_contact_overlap_on_insert
BEFORE INSERT ON partial_contacts
FOR EACH ROW
BEGIN
SELECT CASE
WHEN EXISTS (SELECT 1 FROM contacts WHERE user_name = NEW.user_name)
THEN RAISE(FAIL, 'Can''t insert PartialContact: There already exists a contact with this user_name')
END;
END;".to_string()
}
23 changes: 23 additions & 0 deletions coreclient/migrations/V27__create_contacts_update_trigger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::groups::openmls_provider::key_packages::StorableKeyPackage;
use crate::utils::persistence::Storable;

/// OpenMLS provider data
pub fn migration() -> String {
"DROP TRIGGER IF EXISTS no_contact_overlap_on_update;
CREATE TRIGGER no_contact_overlap_on_update
BEFORE UPDATE ON partial_contacts
FOR EACH ROW
BEGIN
SELECT CASE
WHEN EXISTS (SELECT 1 FROM contacts WHERE user_name = NEW.user_name)
THEN RAISE(FAIL, 'Can''t update PartialContact: There already exists a contact with this user_name')
END;
END;".to_string()
}
12 changes: 12 additions & 0 deletions coreclient/migrations/V2__create_own_client_info_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::clients::own_client_info::OwnClientInfo;
use crate::utils::persistence::Storable;

pub fn migration() -> String {
<OwnClientInfo as Storable>::CREATE_TABLE_STATEMENT.to_string()
}
12 changes: 12 additions & 0 deletions coreclient/migrations/V3__create_own_user_profile_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::user_profiles::UserProfile;
use crate::utils::persistence::Storable;

pub fn migration() -> String {
<UserProfile as Storable>::CREATE_TABLE_STATEMENT.to_string()
}
12 changes: 12 additions & 0 deletions coreclient/migrations/V4__create_groups_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::groups::persistence::StorableGroup;
use crate::utils::persistence::Storable;

pub fn migration() -> String {
<StorableGroup as Storable>::CREATE_TABLE_STATEMENT.to_string()
}
12 changes: 12 additions & 0 deletions coreclient/migrations/V5__create_client_credential_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::groups::client_auth_info::StorableClientCredential;
use crate::utils::persistence::Storable;

pub fn migration() -> String {
<StorableClientCredential as Storable>::CREATE_TABLE_STATEMENT.to_string()
}
12 changes: 12 additions & 0 deletions coreclient/migrations/V6__create_group_membership_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2023 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use barrel::{backend::Sqlite, types, Migration};

use crate::groups::client_auth_info::GroupMembership;
use crate::utils::persistence::Storable;

pub fn migration() -> String {
<GroupMembership as Storable>::CREATE_TABLE_STATEMENT.to_string()
}
Loading

0 comments on commit 343ecb7

Please sign in to comment.