sqlite-err-parser 0.5.0

Deconstruct certain sqlite errors
Documentation
use rusqlite::{Connection, params};

use sqlite_err_parser::{InterpretedError, deconstruct_error};

#[test]
fn delete_restricted() {
  let conn = Connection::open_in_memory().unwrap();

  conn.pragma_update(None, "foreign_keys", "ON").unwrap();

  conn
    .execute(
      "CREATE TABLE roles (
  id    INTEGER PRIMARY KEY,
  title TEXT    UNIQUE NOT NULL
)",
      ()
    )
    .unwrap();

  conn
    .execute("INSERT INTO roles (id, title) VALUES (1, 'janitor');", [])
    .unwrap();
  conn
    .execute("INSERT INTO roles (id, title) VALUES (2, 'spy');", [])
    .unwrap();

  conn
    .execute(
      "CREATE TABLE agents (
  id      INTEGER PRIMARY KEY,
  name    TEXT    UNIQUE NOT NULL,
  role_id INTEGER NOT NULL
          REFERENCES roles(id)
          ON DELETE RESTRICT
)",
      ()
    )
    .unwrap();

  conn
    .execute(
      "INSERT INTO agents (role_id, name) VALUES (?, ?)",
      params!(2, "frank")
    )
    .unwrap();

  // At this point, it shouldn't be possible to delete role.id = 2, because it
  // is "locked" by the agent.

  let Err(err) = conn.execute("DELETE FROM roles WHERE id=2;", []) else {
    panic!("Unexpectedly not Err()");
  };
  let Some(InterpretedError::ForeignKey) = deconstruct_error(&err) else {
    panic!("Not treated as InterpretedError::ForeignKey; {err}");
  };
}

// vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :