use autoincrement for IDs
This commit is contained in:
@@ -8,7 +8,6 @@ use rand::{RngCore, rngs::OsRng};
|
||||
use rusqlite::{Connection, OptionalExtension, params};
|
||||
use sha2::{Digest, Sha256};
|
||||
use thiserror::Error;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum DbError {
|
||||
@@ -31,7 +30,7 @@ pub struct Database {
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct User {
|
||||
pub id: String,
|
||||
pub id: i64,
|
||||
pub email: String,
|
||||
pub display_name: String,
|
||||
}
|
||||
@@ -44,26 +43,26 @@ pub struct SessionUser {
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct GroceryList {
|
||||
pub id: String,
|
||||
pub id: i64,
|
||||
pub name: String,
|
||||
pub revision: i64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Item {
|
||||
pub id: String,
|
||||
pub list_id: String,
|
||||
pub id: i64,
|
||||
pub list_id: i64,
|
||||
pub name: String,
|
||||
pub quantity: String,
|
||||
pub note: String,
|
||||
pub category_id: Option<String>,
|
||||
pub category_id: Option<i64>,
|
||||
pub checked: bool,
|
||||
pub version: i64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Category {
|
||||
pub id: String,
|
||||
pub id: i64,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
@@ -106,20 +105,21 @@ impl Database {
|
||||
password_hash: String,
|
||||
) -> DbResult<User> {
|
||||
self.call(move |connection| {
|
||||
let user = User {
|
||||
id: Uuid::new_v4().to_string(),
|
||||
email,
|
||||
display_name,
|
||||
};
|
||||
|
||||
let result = connection.execute(
|
||||
"INSERT INTO users (id, email, display_name, password_hash, created_at)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5)",
|
||||
params![user.id, user.email, user.display_name, password_hash, now()],
|
||||
"INSERT INTO users (email, display_name, password_hash, created_at)
|
||||
VALUES (?1, ?2, ?3, ?4)",
|
||||
params![email, display_name, password_hash, now()],
|
||||
);
|
||||
|
||||
match result {
|
||||
Ok(_) => Ok(user),
|
||||
Ok(_) => {
|
||||
let id = connection.last_insert_rowid();
|
||||
Ok(User {
|
||||
id,
|
||||
email,
|
||||
display_name,
|
||||
})
|
||||
}
|
||||
Err(error) if error.to_string().contains("UNIQUE") => Err(DbError::Conflict),
|
||||
Err(error) => Err(sql_error(error)),
|
||||
}
|
||||
@@ -162,7 +162,7 @@ impl Database {
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn create_session(&self, user_id: String) -> DbResult<(String, String)> {
|
||||
pub async fn create_session(&self, user_id: i64) -> DbResult<(String, String)> {
|
||||
self.call(move |connection| {
|
||||
let session_token = new_secret();
|
||||
let csrf_token = new_secret();
|
||||
@@ -248,41 +248,35 @@ impl Database {
|
||||
|
||||
pub async fn create_list(&self, name: String) -> DbResult<GroceryList> {
|
||||
self.call(move |connection| {
|
||||
let list = GroceryList {
|
||||
id: Uuid::new_v4().to_string(),
|
||||
name,
|
||||
revision: 0,
|
||||
};
|
||||
let transaction = connection.transaction().map_err(sql_error)?;
|
||||
transaction
|
||||
.execute(
|
||||
"INSERT INTO lists (id, name, revision, created_at)
|
||||
VALUES (?1, ?2, 0, ?3)",
|
||||
params![list.id, list.name, now()],
|
||||
"INSERT INTO lists (name, revision, created_at)
|
||||
VALUES (?1, 0, ?2)",
|
||||
params![name, now()],
|
||||
)
|
||||
.map_err(sql_error)?;
|
||||
let list_id = transaction.last_insert_rowid();
|
||||
for (position, category_name) in DEFAULT_CATEGORIES.iter().enumerate() {
|
||||
transaction
|
||||
.execute(
|
||||
"INSERT INTO categories (id, list_id, name, position, created_at)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5)",
|
||||
params![
|
||||
Uuid::new_v4().to_string(),
|
||||
list.id,
|
||||
category_name,
|
||||
position as i64,
|
||||
now()
|
||||
],
|
||||
"INSERT INTO categories (list_id, name, position, created_at)
|
||||
VALUES (?1, ?2, ?3, ?4)",
|
||||
params![list_id, category_name, position as i64, now()],
|
||||
)
|
||||
.map_err(sql_error)?;
|
||||
}
|
||||
transaction.commit().map_err(sql_error)?;
|
||||
Ok(list)
|
||||
Ok(GroceryList {
|
||||
id: list_id,
|
||||
name,
|
||||
revision: 0,
|
||||
})
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn list_access(&self, list_id: String) -> DbResult<Option<GroceryList>> {
|
||||
pub async fn list_access(&self, list_id: i64) -> DbResult<Option<GroceryList>> {
|
||||
self.call(move |connection| {
|
||||
connection
|
||||
.query_row(
|
||||
@@ -304,7 +298,7 @@ impl Database {
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn items(&self, list_id: String) -> DbResult<Vec<Item>> {
|
||||
pub async fn items(&self, list_id: i64) -> DbResult<Vec<Item>> {
|
||||
self.call(move |connection| {
|
||||
let mut statement = connection
|
||||
.prepare(
|
||||
@@ -333,7 +327,7 @@ impl Database {
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn categories(&self, list_id: String) -> DbResult<Vec<Category>> {
|
||||
pub async fn categories(&self, list_id: i64) -> DbResult<Vec<Category>> {
|
||||
self.call(move |connection| {
|
||||
let mut statement = connection
|
||||
.prepare(
|
||||
@@ -356,7 +350,7 @@ impl Database {
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn create_category(&self, list_id: String, name: String) -> DbResult<i64> {
|
||||
pub async fn create_category(&self, list_id: i64, name: String) -> DbResult<i64> {
|
||||
self.call(move |connection| {
|
||||
let transaction = connection.transaction().map_err(sql_error)?;
|
||||
let position: i64 = transaction
|
||||
@@ -368,9 +362,9 @@ impl Database {
|
||||
)
|
||||
.map_err(sql_error)?;
|
||||
let result = transaction.execute(
|
||||
"INSERT INTO categories (id, list_id, name, position, created_at)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5)",
|
||||
params![Uuid::new_v4().to_string(), list_id, name, position, now()],
|
||||
"INSERT INTO categories (list_id, name, position, created_at)
|
||||
VALUES (?1, ?2, ?3, ?4)",
|
||||
params![list_id, name, position, now()],
|
||||
);
|
||||
match result {
|
||||
Ok(_) => {}
|
||||
@@ -379,7 +373,7 @@ impl Database {
|
||||
}
|
||||
Err(error) => return Err(sql_error(error)),
|
||||
}
|
||||
let revision = bump_revision(&transaction, &list_id)?;
|
||||
let revision = bump_revision(&transaction, list_id)?;
|
||||
transaction.commit().map_err(sql_error)?;
|
||||
Ok(revision)
|
||||
})
|
||||
@@ -388,16 +382,15 @@ impl Database {
|
||||
|
||||
pub async fn add_item(
|
||||
&self,
|
||||
list_id: String,
|
||||
list_id: i64,
|
||||
name: String,
|
||||
quantity: String,
|
||||
note: String,
|
||||
category_id: Option<String>,
|
||||
category_id: Option<i64>,
|
||||
) -> DbResult<i64> {
|
||||
self.call(move |connection| {
|
||||
let transaction = connection.transaction().map_err(sql_error)?;
|
||||
let category_id = category_id.filter(|category_id| !category_id.is_empty());
|
||||
ensure_category(&transaction, &list_id, category_id.as_deref())?;
|
||||
ensure_category(&transaction, list_id, category_id)?;
|
||||
let position: i64 = transaction
|
||||
.query_row(
|
||||
"SELECT COALESCE(MAX(position), -1) + 1 FROM items WHERE list_id = ?1",
|
||||
@@ -408,21 +401,12 @@ impl Database {
|
||||
transaction
|
||||
.execute(
|
||||
"INSERT INTO items
|
||||
(id, list_id, name, quantity, note, category_id, checked, version, position, created_at, updated_at)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, 0, 1, ?7, ?8, ?8)",
|
||||
params![
|
||||
Uuid::new_v4().to_string(),
|
||||
list_id,
|
||||
name,
|
||||
quantity,
|
||||
note,
|
||||
category_id,
|
||||
position,
|
||||
now()
|
||||
],
|
||||
(list_id, name, quantity, note, category_id, checked, version, position, created_at, updated_at)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, 0, 1, ?6, ?7, ?7)",
|
||||
params![list_id, name, quantity, note, category_id, position, now()],
|
||||
)
|
||||
.map_err(sql_error)?;
|
||||
let revision = bump_revision(&transaction, &list_id)?;
|
||||
let revision = bump_revision(&transaction, list_id)?;
|
||||
transaction.commit().map_err(sql_error)?;
|
||||
Ok(revision)
|
||||
})
|
||||
@@ -431,8 +415,8 @@ impl Database {
|
||||
|
||||
pub async fn set_item_checked(
|
||||
&self,
|
||||
list_id: String,
|
||||
item_id: String,
|
||||
list_id: i64,
|
||||
item_id: i64,
|
||||
checked: bool,
|
||||
) -> DbResult<i64> {
|
||||
self.call(move |connection| {
|
||||
@@ -448,7 +432,7 @@ impl Database {
|
||||
if changed == 0 {
|
||||
return Err(DbError::NotFound);
|
||||
}
|
||||
let revision = bump_revision(&transaction, &list_id)?;
|
||||
let revision = bump_revision(&transaction, list_id)?;
|
||||
transaction.commit().map_err(sql_error)?;
|
||||
Ok(revision)
|
||||
})
|
||||
@@ -457,17 +441,16 @@ impl Database {
|
||||
|
||||
pub async fn update_item(
|
||||
&self,
|
||||
list_id: String,
|
||||
item_id: String,
|
||||
list_id: i64,
|
||||
item_id: i64,
|
||||
name: String,
|
||||
quantity: String,
|
||||
note: String,
|
||||
category_id: Option<String>,
|
||||
category_id: Option<i64>,
|
||||
) -> DbResult<i64> {
|
||||
self.call(move |connection| {
|
||||
let transaction = connection.transaction().map_err(sql_error)?;
|
||||
let category_id = category_id.filter(|category_id| !category_id.is_empty());
|
||||
ensure_category(&transaction, &list_id, category_id.as_deref())?;
|
||||
ensure_category(&transaction, list_id, category_id)?;
|
||||
let changed = transaction
|
||||
.execute(
|
||||
"UPDATE items
|
||||
@@ -480,14 +463,14 @@ impl Database {
|
||||
if changed == 0 {
|
||||
return Err(DbError::NotFound);
|
||||
}
|
||||
let revision = bump_revision(&transaction, &list_id)?;
|
||||
let revision = bump_revision(&transaction, list_id)?;
|
||||
transaction.commit().map_err(sql_error)?;
|
||||
Ok(revision)
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete_item(&self, list_id: String, item_id: String) -> DbResult<i64> {
|
||||
pub async fn delete_item(&self, list_id: i64, item_id: i64) -> DbResult<i64> {
|
||||
self.call(move |connection| {
|
||||
let transaction = connection.transaction().map_err(sql_error)?;
|
||||
let changed = transaction
|
||||
@@ -499,14 +482,14 @@ impl Database {
|
||||
if changed == 0 {
|
||||
return Err(DbError::NotFound);
|
||||
}
|
||||
let revision = bump_revision(&transaction, &list_id)?;
|
||||
let revision = bump_revision(&transaction, list_id)?;
|
||||
transaction.commit().map_err(sql_error)?;
|
||||
Ok(revision)
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn create_invitation(&self, created_by: String, token: String) -> DbResult<i64> {
|
||||
pub async fn create_invitation(&self, created_by: i64, token: String) -> DbResult<i64> {
|
||||
self.call(move |connection| {
|
||||
let expires_at = now() + 60 * 60 * 24 * 7;
|
||||
connection
|
||||
@@ -584,7 +567,7 @@ fn migrate(connection: &Connection) -> DbResult<()> {
|
||||
connection
|
||||
.execute_batch(
|
||||
"CREATE TABLE IF NOT EXISTS users (
|
||||
id TEXT PRIMARY KEY,
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
email TEXT NOT NULL UNIQUE COLLATE NOCASE,
|
||||
display_name TEXT NOT NULL,
|
||||
password_hash TEXT NOT NULL,
|
||||
@@ -592,19 +575,19 @@ fn migrate(connection: &Connection) -> DbResult<()> {
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
token_hash TEXT PRIMARY KEY,
|
||||
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
csrf_token TEXT NOT NULL,
|
||||
expires_at INTEGER NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS lists (
|
||||
id TEXT PRIMARY KEY,
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
revision INTEGER NOT NULL DEFAULT 0,
|
||||
created_at INTEGER NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS categories (
|
||||
id TEXT PRIMARY KEY,
|
||||
list_id TEXT NOT NULL REFERENCES lists(id) ON DELETE CASCADE,
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
list_id INTEGER NOT NULL REFERENCES lists(id) ON DELETE CASCADE,
|
||||
name TEXT NOT NULL COLLATE NOCASE,
|
||||
position INTEGER NOT NULL DEFAULT 0,
|
||||
created_at INTEGER NOT NULL,
|
||||
@@ -612,16 +595,16 @@ fn migrate(connection: &Connection) -> DbResult<()> {
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS invitations (
|
||||
token_hash TEXT PRIMARY KEY,
|
||||
created_by TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
created_by INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
expires_at INTEGER NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS items (
|
||||
id TEXT PRIMARY KEY,
|
||||
list_id TEXT NOT NULL REFERENCES lists(id) ON DELETE CASCADE,
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
list_id INTEGER NOT NULL REFERENCES lists(id) ON DELETE CASCADE,
|
||||
name TEXT NOT NULL,
|
||||
quantity TEXT NOT NULL DEFAULT '',
|
||||
note TEXT NOT NULL DEFAULT '',
|
||||
category_id TEXT REFERENCES categories(id) ON DELETE SET NULL,
|
||||
category_id INTEGER REFERENCES categories(id) ON DELETE SET NULL,
|
||||
checked INTEGER NOT NULL DEFAULT 0,
|
||||
version INTEGER NOT NULL DEFAULT 1,
|
||||
position INTEGER NOT NULL DEFAULT 0,
|
||||
@@ -634,34 +617,13 @@ fn migrate(connection: &Connection) -> DbResult<()> {
|
||||
)
|
||||
.map_err(sql_error)?;
|
||||
|
||||
if !has_column(connection, "items", "category_id")? {
|
||||
connection
|
||||
.execute(
|
||||
"ALTER TABLE items ADD COLUMN category_id TEXT REFERENCES categories(id) ON DELETE SET NULL",
|
||||
[],
|
||||
)
|
||||
.map_err(sql_error)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn has_column(connection: &Connection, table: &str, wanted: &str) -> DbResult<bool> {
|
||||
let mut statement = connection
|
||||
.prepare(&format!("PRAGMA table_info({table})"))
|
||||
.map_err(sql_error)?;
|
||||
let mut rows = statement.query([]).map_err(sql_error)?;
|
||||
while let Some(row) = rows.next().map_err(sql_error)? {
|
||||
if row.get::<_, String>(1).map_err(sql_error)? == wanted {
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
fn ensure_category(
|
||||
transaction: &rusqlite::Transaction<'_>,
|
||||
list_id: &str,
|
||||
category_id: Option<&str>,
|
||||
list_id: i64,
|
||||
category_id: Option<i64>,
|
||||
) -> DbResult<()> {
|
||||
let Some(category_id) = category_id else {
|
||||
return Ok(());
|
||||
@@ -689,7 +651,7 @@ const DEFAULT_CATEGORIES: &[&str] = &[
|
||||
"Household",
|
||||
];
|
||||
|
||||
fn bump_revision(transaction: &rusqlite::Transaction<'_>, list_id: &str) -> DbResult<i64> {
|
||||
fn bump_revision(transaction: &rusqlite::Transaction<'_>, list_id: i64) -> DbResult<i64> {
|
||||
transaction
|
||||
.execute(
|
||||
"UPDATE lists SET revision = revision + 1 WHERE id = ?1",
|
||||
|
||||
Reference in New Issue
Block a user