Skip to content

Commit 9bc614d

Browse files
authored
fix: set collection id col to auto-incr and fix get_collection_id (mozilla-services#1929)
Inserting into collections in `get_or_create_collection_id` was failing. The wrong column name was used in `get_collection_id`. It's also been converted to Diesel ORM calls. Closes STOR-422
1 parent 56c21e0 commit 9bc614d

File tree

2 files changed

+12
-22
lines changed
  • syncstorage-postgres

2 files changed

+12
-22
lines changed

syncstorage-postgres/migrations/2025-10-20-155711_create_schema/up.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ CREATE INDEX bsos_expiry_idx ON bsos (
3939

4040
-- collections table
4141
CREATE TABLE collections (
42-
collection_id INTEGER PRIMARY KEY,
42+
collection_id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
4343
name VARCHAR(32) NOT NULL UNIQUE
4444
);
4545

@@ -87,4 +87,4 @@ CREATE TABLE batch_bsos (
8787
collection_id,
8888
batch_id
8989
) ON DELETE CASCADE
90-
);
90+
);

syncstorage-postgres/src/db/db_impl.rs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use chrono::{NaiveDateTime, TimeDelta};
55
use diesel::{
66
delete,
77
dsl::{count, max, sql},
8-
sql_query,
9-
sql_types::{BigInt, Integer, Nullable, Text},
8+
sql_types::{BigInt, Integer, Nullable},
109
ExpressionMethods, OptionalExtension, QueryDsl, SelectableHelper,
1110
};
1211
use diesel_async::{AsyncConnection, RunQueryDsl, TransactionManager};
@@ -23,7 +22,7 @@ use crate::{
2322
db::CollectionLock,
2423
orm_models::BsoChangeset,
2524
pool::Conn,
26-
schema::{bsos, user_collections},
25+
schema::{bsos, collections, user_collections},
2726
DbError, DbResult,
2827
};
2928

@@ -513,17 +512,14 @@ impl Db for PgDb {
513512
return Ok(id);
514513
}
515514

516-
let id = sql_query(
517-
"SELECT id
518-
FROM collections
519-
WHERE name = $1",
520-
)
521-
.bind::<Text, _>(name)
522-
.get_result::<IdResult>(&mut self.conn)
523-
.await
524-
.optional()?
525-
.ok_or_else(DbError::collection_not_found)?
526-
.id;
515+
let id = collections::table
516+
.select(collections::collection_id)
517+
.filter(collections::name.eq(name))
518+
.first::<i32>(&mut self.conn)
519+
.await
520+
.optional()?
521+
.ok_or_else(DbError::collection_not_found)?;
522+
527523
if !self.session.in_write_transaction {
528524
self.coll_cache.put(id, name.to_owned())?;
529525
}
@@ -600,12 +596,6 @@ impl Db for PgDb {
600596
}
601597
}
602598

603-
#[derive(Debug, QueryableByName)]
604-
struct IdResult {
605-
#[diesel(sql_type = Integer)]
606-
id: i32,
607-
}
608-
609599
#[derive(Debug, Queryable, Selectable)]
610600
#[diesel(table_name = bsos)]
611601
pub struct GetBso {

0 commit comments

Comments
 (0)