Skip to content

Commit

Permalink
change the primary key to be more valid
Browse files Browse the repository at this point in the history
chunk ids should only be unique to users, not necessarily across the
whole server
  • Loading branch information
billyb2 committed Aug 18, 2024
1 parent c78b099 commit e4d9d3a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 5 additions & 0 deletions migrations/20240818052859_better_primary_key.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE chunks RENAME CONSTRAINT chunks_pkey TO chunks_pkeyold;
CREATE UNIQUE INDEX chunks_pkey ON chunks (id, user_id);
ALTER TABLE chunks DROP CONSTRAINT chunks_pkeyold;
ALTER TABLE chunks ADD PRIMARY KEY USING INDEX chunks_pkey;
ALTER TABLE chunks ADD CONSTRAINT unique_id_user_id UNIQUE (id, user_id);
4 changes: 2 additions & 2 deletions src/meta_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl MetaDB for PostgresMetaDB {
user_id: i64,
) -> Result<Option<ChunkMetadata>> {
Ok(sqlx::query(
"select chunk_size, nonce, indice from chunks where id = $1 and user_id = $2",
"select chunk_size, nonce, hash, indice from chunks where id = $1 and user_id = $2",
)
.bind(chunk_id.to_string())
.bind(user_id)
Expand Down Expand Up @@ -409,7 +409,7 @@ impl MetaDB for PostgresMetaDB {
.collect();

let mut query = QueryBuilder::new(
"select sum(enc_chunk_size + length(encrypted_metadata))::bigint as sum, user_id from chunks",
"select sum(enc_chunk_size + coalesce(length(encrypted_metadata), 0))::bigint as sum, user_id from chunks",
);

if !user_ids.is_empty() {
Expand Down

0 comments on commit e4d9d3a

Please sign in to comment.