KeiSeiKit-1.0/_primitives/_rust/kei-auth/src/schema.rs
Parfii-bot a4e667de10 KeiSeiKit-public — clean state
Single-commit clean baseline after security scrub of niche-tells,
project codenames, internal jargon, and contributor-email leaks.

Contents:
- 100 Rust crates (_primitives/_rust/)
- 37 agent manifests (_manifests/) + generated specs (_generated/)
- 67 user-invocable skills (skills/)
- 33 hooks (hooks/)
- Composition blocks (_blocks/)
- Documentation (docs/, README.md)
- TS adapter packages (_ts_packages/)
- Assembler (_assembler/)
- Roles (_roles/)
- Templates (_templates/)
- Forgejo CI (.forgejo/)

Author: Denis Parfionovich <info@greendragon.info>

License: see LICENSE.
2026-05-01 12:09:03 +08:00

36 lines
1.1 KiB
Rust

use anyhow::{Context, Result};
use rusqlite::Connection;
use std::path::Path;
pub fn open(path: &Path) -> Result<Connection> {
if let Some(parent) = path.parent() {
let _ = std::fs::create_dir_all(parent);
}
let conn = Connection::open(path).context("open sqlite")?;
create_schema(&conn)?;
Ok(conn)
}
pub fn open_memory() -> Result<Connection> {
let conn = Connection::open_in_memory()?;
create_schema(&conn)?;
Ok(conn)
}
pub fn create_schema(conn: &Connection) -> Result<()> {
conn.execute_batch(r#"
CREATE TABLE IF NOT EXISTS auth_tokens (
id INTEGER PRIMARY KEY,
token_hash TEXT NOT NULL UNIQUE,
user_id TEXT NOT NULL,
project TEXT NOT NULL,
scope TEXT NOT NULL CHECK(scope IN ('read','write','admin')),
expires_at INTEGER NOT NULL,
created_at INTEGER NOT NULL,
revoked_at INTEGER DEFAULT 0
);
CREATE INDEX IF NOT EXISTS idx_tok_user ON auth_tokens(user_id);
CREATE INDEX IF NOT EXISTS idx_tok_project ON auth_tokens(project);
"#)?;
Ok(())
}