Use cascading deletes for removal of node rows
Instead of manually removing all rows in the address table, use cascading deletes.
This commit is contained in:
parent
3cad39a3a9
commit
0ae3c00cd7
|
|
@ -17,7 +17,7 @@ create table if not exists "nodes" (
|
||||||
|
|
||||||
create table if not exists "addresses" (
|
create table if not exists "addresses" (
|
||||||
-- Node ID.
|
-- Node ID.
|
||||||
"node" text not null references "nodes" ("id"),
|
"node" text not null references "nodes" ("id") on delete cascade,
|
||||||
-- Address type.
|
-- Address type.
|
||||||
"type" text not null,
|
"type" text not null,
|
||||||
-- Address value.
|
-- Address value.
|
||||||
|
|
@ -40,7 +40,7 @@ create table if not exists "addresses" (
|
||||||
|
|
||||||
create table if not exists "announcements" (
|
create table if not exists "announcements" (
|
||||||
-- Node ID.
|
-- Node ID.
|
||||||
"node" text not null references "nodes" ("id"),
|
"node" text not null references "nodes" ("id") on delete cascade,
|
||||||
-- Repo ID, if any, for example in ref announcements.
|
-- Repo ID, if any, for example in ref announcements.
|
||||||
"repo" text not null,
|
"repo" text not null,
|
||||||
-- Announcement type.
|
-- Announcement type.
|
||||||
|
|
@ -66,7 +66,7 @@ create table if not exists "repo-sync-status" (
|
||||||
-- Repository ID.
|
-- Repository ID.
|
||||||
"repo" text not null,
|
"repo" text not null,
|
||||||
-- Node ID.
|
-- Node ID.
|
||||||
"node" text not null references "nodes" ("id"),
|
"node" text not null references "nodes" ("id") on delete cascade,
|
||||||
-- Head of your `rad/sigrefs` branch that was synced.
|
-- Head of your `rad/sigrefs` branch that was synced.
|
||||||
"head" text not null,
|
"head" text not null,
|
||||||
-- When this entry was last updated.
|
-- When this entry was last updated.
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,7 @@ impl From<sql::Connection> for Book {
|
||||||
|
|
||||||
impl Book {
|
impl Book {
|
||||||
const SCHEMA: &str = include_str!("schema.sql");
|
const SCHEMA: &str = include_str!("schema.sql");
|
||||||
|
const PRAGMA: &str = "PRAGMA foreign_keys = ON";
|
||||||
|
|
||||||
/// Open an address book at the given path. Creates a new address book if it
|
/// Open an address book at the given path. Creates a new address book if it
|
||||||
/// doesn't exist.
|
/// doesn't exist.
|
||||||
|
|
@ -58,6 +59,7 @@ impl Book {
|
||||||
.with_read_write()
|
.with_read_write()
|
||||||
.with_full_mutex(),
|
.with_full_mutex(),
|
||||||
)?;
|
)?;
|
||||||
|
db.execute(Self::PRAGMA)?;
|
||||||
db.execute(Self::SCHEMA)?;
|
db.execute(Self::SCHEMA)?;
|
||||||
|
|
||||||
Ok(Self { db })
|
Ok(Self { db })
|
||||||
|
|
@ -67,6 +69,7 @@ impl Book {
|
||||||
/// open databases, as no locking is required.
|
/// open databases, as no locking is required.
|
||||||
pub fn reader<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
|
pub fn reader<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
|
||||||
let db = sql::Connection::open_with_flags(path, sqlite::OpenFlags::new().with_read_only())?;
|
let db = sql::Connection::open_with_flags(path, sqlite::OpenFlags::new().with_read_only())?;
|
||||||
|
db.execute(Self::PRAGMA)?;
|
||||||
db.execute(Self::SCHEMA)?;
|
db.execute(Self::SCHEMA)?;
|
||||||
|
|
||||||
Ok(Self { db })
|
Ok(Self { db })
|
||||||
|
|
@ -75,6 +78,7 @@ impl Book {
|
||||||
/// Create a new in-memory address book.
|
/// Create a new in-memory address book.
|
||||||
pub fn memory() -> Result<Self, Error> {
|
pub fn memory() -> Result<Self, Error> {
|
||||||
let db = sql::Connection::open(":memory:")?;
|
let db = sql::Connection::open(":memory:")?;
|
||||||
|
db.execute(Self::PRAGMA)?;
|
||||||
db.execute(Self::SCHEMA)?;
|
db.execute(Self::SCHEMA)?;
|
||||||
|
|
||||||
Ok(Self { db })
|
Ok(Self { db })
|
||||||
|
|
@ -196,20 +200,12 @@ impl Store for Book {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove(&mut self, node: &NodeId) -> Result<bool, Error> {
|
fn remove(&mut self, node: &NodeId) -> Result<bool, Error> {
|
||||||
transaction(&self.db, move |db| {
|
let mut stmt = self.db.prepare("DELETE FROM nodes WHERE id = ?1")?;
|
||||||
db.prepare("DELETE FROM nodes WHERE id = ?")?
|
|
||||||
.into_iter()
|
|
||||||
.bind(&[node][..])?
|
|
||||||
.next();
|
|
||||||
|
|
||||||
db.prepare("DELETE FROM addresses WHERE node = ?")?
|
stmt.bind((1, node))?;
|
||||||
.into_iter()
|
stmt.next()?;
|
||||||
.bind(&[node][..])?
|
|
||||||
.next();
|
|
||||||
|
|
||||||
Ok(db.change_count() > 0)
|
Ok(self.db.change_count() > 0)
|
||||||
})
|
|
||||||
.map_err(Error::from)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn synced(
|
fn synced(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue