ActaDB

Personal Data That Can Be Erased

ActaDB never changes or removes a stored event. A person can still ask that their personal data is erased. This example shows a simple way to have both. The personal data is not in the events. It is a row in a table, and an event holds only the number of this row.

Watch It

The video shows one run of the example in about a minute and a half. The program registers two customers, places three orders, erases one customer and checks what is left of both.

The output of one run of the example in a console. The video has sound.

Run It

The whole program is one file. You run it from the root of the repository.

git clone https://github.com/ricofritzsche/actadb
cd actadb
cargo run --release -p personal-data

Nothing has to be installed before. The table is in an encrypted DuckDB file beside the event store. The first build downloads the DuckDB library, and the first run downloads the DuckDB extension httpfs, because DuckDB writes an encrypted file only with this extension. The program creates its two files in the temporary folder of your system and removes them at the end.

A Number Instead of a Name

A registration stores the name and the email address as a row in the table, and the table gives the row a number. The event CustomerRegistered gets only this number.

/// Stores the personal data as a row and appends an event with the number of the row.
/// The event holds no name and no email address.
fn register(store: &EventStore, table: &Connection, person: (&str, &str)) -> Result<i64, Failure> {
    let customer: i64 = table.query_row(INSERT, [person.0, person.1], |row| row.get(0))?;
    let registered = event(CUSTOMER_REGISTERED, format!("customer {customer}"));
    if let Err(error) = store.append(vec![registered], None) {
        table.execute(DELETE, [customer])?;
        return Err(error.into());
    }
    Ok(customer)
}

Notice that the payload of the event is customer 1 and nothing else. The row is stored first and the event after it. If the append fails, the program deletes the row again, so no row stays without its event. INSERT and DELETE are the SQL statements on the table.

The Erasure

An erasure deletes the row of the customer and appends one event that says so.

/// Deletes the row of the customer and appends an event that says so.
/// The row is deleted first, so the personal data is gone even if the append does not happen.
fn erase(store: &EventStore, table: &Connection, customer: i64) -> Result<Answer, Failure> {
    let query = Query::event_types([CUSTOMER_REGISTERED, CUSTOMER_ERASED]);
    let who = format!("customer {customer}");
    for _attempt in 1..=3 {
        let selection = store.query(&query)?;
        if !holds(&selection.records, CUSTOMER_REGISTERED, &who) {
            return Ok(Answer::CustomerUnknown);
        }
        table.execute(DELETE, [customer])?;
        if holds(&selection.records, CUSTOMER_ERASED, &who) {
            return Ok(Answer::CustomerErased);
        }
        let erased = event(CUSTOMER_ERASED, who.clone());
        match store.append(vec![erased], Some(selection.token))? {
            Commit::Appended { .. } => return Ok(Answer::Erased),
            Commit::Conflict => continue,
        }
    }
    Ok(Answer::NotProcessed)
}

Notice that this function removes no event. The row is deleted first, so the personal data is gone even if the program stops before the append. The next erasure of this customer then appends the event. The event CustomerErased holds the number of the customer and nothing else.

What It Prints

Every run prints the same. This is the first part, from the registration to an order for the erased customer.

Register 2 customers. The table gets name and email, the event gets the number.
  customer 1   Ada Moore <ada@example.com>
  customer 2   Bob Stone <bob@example.com>

Place 3 orders. An order is an event with the number of the customer.
  customer 1   coffee grinder   Placed
  customer 2   desk lamp        Placed
  customer 1   water kettle     Placed

The orders, with the personal data from the table.
  order 1   coffee grinder   Ada Moore <ada@example.com>
  order 2   desk lamp        Bob Stone <bob@example.com>
  order 3   water kettle     Ada Moore <ada@example.com>

Erase customer 1. The row is deleted, and one event says that it happened.
  customer 1   Erased

The orders after the erasure. No order is lost.
  order 1   coffee grinder   customer 1, personal data erased
  order 2   desk lamp        Bob Stone <bob@example.com>
  order 3   water kettle     customer 1, personal data erased

An order for the erased customer is refused.
  customer 1   tea pot          CustomerErased

The list of orders is built from the OrderPlaced events. For every order the program looks up the row of the customer, and this is the only place where the events and the personal data come together. After the erasure all three orders are still in the list. The row of customer 1 is gone, and the list says so.

The new order for customer 1 is refused. The function that places an order does not need the table for this. It reads the CustomerRegistered and CustomerErased events, finds the erasure and appends nothing.

The second part is the check at the end.

All events in the store. No event holds a name or an email address.
  1   CustomerRegistered   customer 1
  2   CustomerRegistered   customer 2
  3   OrderPlaced          customer 1: coffee grinder
  4   OrderPlaced          customer 2: desk lamp
  5   OrderPlaced          customer 1: water kettle
  6   CustomerErased       customer 1

What is left of the 2 customers.
  Ada Moore, ada@example.com   rows in the table: 0   found in the event store: 0
  Bob Stone, bob@example.com   rows in the table: 1   found in the event store: 0

The file of the table is encrypted. Names and email addresses readable in its bytes: 0

The store holds six events. No event was changed or removed for the erasure, and no event holds a name or an email address.

For both customers the program then counts the rows in the table with a query, and it searches the bytes of the storage file of ActaDB for the name and the email address. Ada Moore has no row any more, and Bob Stone still has his. The storage file holds neither of them, because no event ever held them.

The last line is about the file of the table. The program searches its bytes for the names and the email addresses of both customers and finds none, also not those of Bob Stone, whose row still exists. The reason is that this file is encrypted.

A Note on Deleted Rows

After the erasure no query returns the row. DuckDB still keeps the bytes of a deleted row in its file, encrypted like everything else in it, until the database is copied into a new file with COPY FROM DATABASE. Not every application needs this step. An application that also has to remove these bytes copies the database after an erasure, or at regular times.

The README of the example says what the program takes care of and what the example leaves out.

A Table Beside the Store

The example is about the principle and not about DuckDB. Another database with a table can take its place. What counts is that personal data never gets into an event. Then the events can stay as they are, and an erasure is one deleted row and one new event.

The page How It Works explains the append with its condition. The page Examples leads to the other examples.