From ae5d5abbb7183b4eb3434d211f03a46d61987c06 Mon Sep 17 00:00:00 2001 From: Billy Batista Date: Sun, 18 Aug 2024 01:45:42 -0400 Subject: [PATCH] change the primary key to be more valid chunk ids should only be unique to users, not necessarily across the whole server --- migrations/20240818052859_better_primary_key.sql | 5 +++++ src/meta_db.rs | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 migrations/20240818052859_better_primary_key.sql diff --git a/migrations/20240818052859_better_primary_key.sql b/migrations/20240818052859_better_primary_key.sql new file mode 100644 index 0000000..c9a6e67 --- /dev/null +++ b/migrations/20240818052859_better_primary_key.sql @@ -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); diff --git a/src/meta_db.rs b/src/meta_db.rs index befaf52..2b43dd4 100644 --- a/src/meta_db.rs +++ b/src/meta_db.rs @@ -153,7 +153,7 @@ impl MetaDB for PostgresMetaDB { user_id: i64, ) -> Result> { 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)