mirror of
https://github.com/narrrl/narl-workspace.git
synced 2026-11-03 16:41:27 +01:00
Node update, delete and move each ran as separate statements, with the slug chosen outside the write that took it. They are now one transaction each, opened with BEGIN IMMEDIATE so a read-then-write cannot fail with SQLITE_BUSY halfway. Creating a node, a space or a file does the same. A delete is one function, delete_subtree, used by the API and the SFTP mount alike: SFTP deletes used to skip the media cleanup and orphan the files a document embedded. Rows go in the transaction, bytes after the commit. Deleting a media row that backs a file in the tree is refused; the media browser marks those files and stops counting them as documents that embed them. A space can no longer be moved into a folder. The diary is found by a role column (migration 0013) instead of its slug, so renaming it no longer breaks the reminder or the agenda, and it cannot be deleted. The same migration adds the unique index root slugs never had. A card is done exactly when it is in the last list: a done patch now moves the card, a card made in the done list is stamped, and adding or deleting lists settles the board. Seeding a fresh board is atomic, and a card links only to a document. Uploads are fsynced before the rename, and zip entries are written aside and renamed like any other upload. A sweep at start and once a day removes leftover .part files, files no row knows, and uploads nothing has embedded for a week. Before a new build migrates the database it writes a VACUUM INTO copy, diary.db.pre-<version>, since migrations only go forward.
15 lines
727 B
Rust
15 lines
727 B
Rust
//! What every writer to the database shares.
|
|
|
|
use sqlx::{Sqlite, SqlitePool, Transaction};
|
|
|
|
/// A transaction that takes the write lock up front. SQLite's default
|
|
/// `BEGIN` is deferred: a transaction that reads before it writes holds only
|
|
/// a read snapshot, and if another connection commits in between, the first
|
|
/// write fails at once with `SQLITE_BUSY` — `busy_timeout` cannot help,
|
|
/// because waiting would not make the snapshot current. Taking the lock at
|
|
/// `BEGIN` makes the reads and the writes one consistent step, and turns
|
|
/// contention into a wait instead of an error.
|
|
pub async fn begin_write(db: &SqlitePool) -> sqlx::Result<Transaction<'static, Sqlite>> {
|
|
db.begin_with("BEGIN IMMEDIATE").await
|
|
}
|