Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

refactor(rust): Use collection_id and file_id as primary keys

Manav Rathi34325691e708
diff --git a/rust/src/storage/schema.rs b/rust/src/storage/schema.rs
index f9a6353292..fd44b25249 100644
--- a/rust/src/storage/schema.rs
+++ b/rust/src/storage/schema.rs
@@ -44,30 +44,28 @@ pub fn create_tables(conn: &Connection) -> Result<()> {
)?;
// Collections/Albums table
+ // Note: collection_id is globally unique across all users, so we use it as the primary key
conn.execute(
"CREATE TABLE IF NOT EXISTS collections (
- id INTEGER PRIMARY KEY,
+ collection_id INTEGER PRIMARY KEY,
account_id INTEGER NOT NULL,
- collection_id INTEGER NOT NULL,
name TEXT NOT NULL,
type TEXT NOT NULL,
owner INTEGER NOT NULL,
is_deleted INTEGER NOT NULL DEFAULT 0,
metadata TEXT,
updated_at INTEGER NOT NULL,
- FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE,
- UNIQUE(account_id, collection_id),
- UNIQUE(collection_id)
+ FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE
)",
[],
)?;
// Files table (for caching remote file metadata)
+ // Note: file_id is globally unique across all users, so we use it as the primary key
conn.execute(
"CREATE TABLE IF NOT EXISTS files (
- id INTEGER PRIMARY KEY,
+ file_id INTEGER PRIMARY KEY,
account_id INTEGER NOT NULL,
- file_id INTEGER NOT NULL,
collection_id INTEGER NOT NULL,
encrypted_key TEXT NOT NULL,
key_decryption_nonce TEXT NOT NULL,
@@ -78,8 +76,7 @@ pub fn create_tables(conn: &Connection) -> Result<()> {
local_path TEXT,
updated_at INTEGER NOT NULL,
FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE,
- FOREIGN KEY (collection_id) REFERENCES collections(collection_id),
- UNIQUE(account_id, file_id)
+ FOREIGN KEY (collection_id) REFERENCES collections(collection_id)
)",
[],
)?;
@@ -87,14 +84,15 @@ pub fn create_tables(conn: &Connection) -> Result<()> {
// Album files mapping
conn.execute(
"CREATE TABLE IF NOT EXISTS album_files (
- id INTEGER PRIMARY KEY,
- account_id INTEGER NOT NULL,
album_id INTEGER NOT NULL,
file_id INTEGER NOT NULL,
+ account_id INTEGER NOT NULL,
synced_locally INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL,
+ PRIMARY KEY (album_id, file_id),
FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE,
- UNIQUE(account_id, album_id, file_id)
+ FOREIGN KEY (album_id) REFERENCES collections(collection_id),
+ FOREIGN KEY (file_id) REFERENCES files(file_id)
)",
[],
)?;
@@ -115,13 +113,25 @@ pub fn create_tables(conn: &Connection) -> Result<()> {
// Create indices for better performance
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_files_collection
- ON files(account_id, collection_id)",
+ ON files(collection_id)",
+ [],
+ )?;
+
+ conn.execute(
+ "CREATE INDEX IF NOT EXISTS idx_files_account
+ ON files(account_id)",
[],
)?;
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_album_files_album
- ON album_files(account_id, album_id)",
+ ON album_files(album_id)",
+ [],
+ )?;
+
+ conn.execute(
+ "CREATE INDEX IF NOT EXISTS idx_album_files_account
+ ON album_files(account_id)",
[],
)?;
diff --git a/rust/src/storage/sync.rs b/rust/src/storage/sync.rs
index 7b8063c6ad..02d45b1e25 100644
--- a/rust/src/storage/sync.rs
+++ b/rust/src/storage/sync.rs
@@ -22,11 +22,11 @@ impl<'a> SyncStore<'a> {
self.conn.execute(
"INSERT OR REPLACE INTO collections
- (account_id, collection_id, name, type, owner, is_deleted, metadata, updated_at)
+ (collection_id, account_id, name, type, owner, is_deleted, metadata, updated_at)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
params![
- account_id,
collection.id,
+ account_id,
collection.name,
format!("{:?}", collection.collection_type).to_lowercase(),
collection.owner,
@@ -66,8 +66,8 @@ impl<'a> SyncStore<'a> {
let existing_sync_status: Option<i32> = self
.conn
.query_row(
- "SELECT is_synced_locally FROM files WHERE account_id = ?1 AND file_id = ?2",
- params![account_id, file.id],
+ "SELECT is_synced_locally FROM files WHERE file_id = ?1",
+ params![file.id],
|row| row.get(0),
)
.optional()?;
@@ -76,12 +76,12 @@ impl<'a> SyncStore<'a> {
self.conn.execute(
"INSERT OR REPLACE INTO files
- (account_id, file_id, collection_id, encrypted_key, key_decryption_nonce,
+ (file_id, account_id, collection_id, encrypted_key, key_decryption_nonce,
file_info, metadata, is_deleted, is_synced_locally, updated_at)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)",
params![
- account_id,
file.id,
+ account_id,
file.collection_id,
file.encrypted_key,
file.key_decryption_nonce,
@@ -300,15 +300,15 @@ impl<'a> SyncStore<'a> {
) -> Result<()> {
let rows_updated = if let Some(path) = local_path {
self.conn.execute(
- "UPDATE files SET is_synced_locally = 1, local_path = ?3
- WHERE account_id = ?1 AND file_id = ?2",
- params![account_id, file_id, path],
+ "UPDATE files SET is_synced_locally = 1, local_path = ?2
+ WHERE file_id = ?1",
+ params![file_id, path],
)?
} else {
self.conn.execute(
"UPDATE files SET is_synced_locally = 1
- WHERE account_id = ?1 AND file_id = ?2",
- params![account_id, file_id],
+ WHERE file_id = ?1",
+ params![file_id],
)?
};