Skip to main content

yugendb_sqlite/
schema.rs

1//! SQLite schema helpers for the yugendb storage table.
2//!
3//! The SQLite driver stores yugendb records through the same core model used by
4//! every driver:
5//!
6//! ```text
7//! namespace + collection + key -> serialised typed value
8//! ```
9//!
10//! This module describes the storage schema used by the SQLite driver.
11
12/// Current schema version for the SQLite storage table.
13pub const SCHEMA_VERSION: u32 = 1;
14
15/// Main storage table used by the SQLite backend.
16pub const STORE_TABLE_NAME: &str = "yugendb_store";
17
18/// Metadata table used for SQLite schema migration bookkeeping.
19pub const MIGRATION_METADATA_TABLE_NAME: &str = "yugendb_schema_migrations";
20
21/// SQL statement that creates the main storage table.
22pub const CREATE_STORE_TABLE_SQL: &str = r#"CREATE TABLE IF NOT EXISTS yugendb_store (
23    namespace TEXT NOT NULL,
24    collection TEXT NOT NULL,
25    key TEXT NOT NULL,
26    value BLOB NOT NULL,
27    codec TEXT NOT NULL,
28    created_at TEXT NOT NULL,
29    updated_at TEXT NOT NULL,
30    expires_at TEXT,
31    PRIMARY KEY (namespace, collection, key)
32);"#;
33
34/// SQL statement that creates the prefix scan index.
35pub const CREATE_PREFIX_INDEX_SQL: &str = r#"CREATE INDEX IF NOT EXISTS idx_yugendb_store_prefix
36ON yugendb_store(namespace, collection, key);"#;
37
38/// SQL statement that creates the expiry lookup index.
39pub const CREATE_EXPIRES_AT_INDEX_SQL: &str = r#"CREATE INDEX IF NOT EXISTS idx_yugendb_store_expires_at
40ON yugendb_store(expires_at);"#;
41
42/// SQL statement that creates schema migration bookkeeping.
43pub const CREATE_MIGRATION_METADATA_TABLE_SQL: &str = r#"CREATE TABLE IF NOT EXISTS yugendb_schema_migrations (
44    version INTEGER PRIMARY KEY,
45    applied_at TEXT NOT NULL
46);"#;
47
48/// All SQL statements needed to initialise the current SQLite schema.
49pub const ALL_SCHEMA_STATEMENTS: [&str; 4] = [
50    CREATE_STORE_TABLE_SQL,
51    CREATE_PREFIX_INDEX_SQL,
52    CREATE_EXPIRES_AT_INDEX_SQL,
53    CREATE_MIGRATION_METADATA_TABLE_SQL,
54];
55
56/// Returns all SQL statements needed to initialise the current SQLite schema.
57#[must_use]
58pub const fn schema_statements() -> &'static [&'static str] {
59    &ALL_SCHEMA_STATEMENTS
60}
61
62/// Value object that describes the SQLite storage schema.
63#[derive(Debug, Clone, Copy, PartialEq, Eq)]
64pub struct SqliteStorageSchema {
65    /// Schema version.
66    pub version: u32,
67    /// Main store table name.
68    pub store_table_name: &'static str,
69    /// Migration metadata table name.
70    pub migration_metadata_table_name: &'static str,
71    /// Ordered SQL statements for schema creation.
72    pub statements: &'static [&'static str],
73}
74
75impl SqliteStorageSchema {
76    /// Returns the current SQLite storage schema description.
77    #[must_use]
78    pub const fn current() -> Self {
79        Self {
80            version: SCHEMA_VERSION,
81            store_table_name: STORE_TABLE_NAME,
82            migration_metadata_table_name: MIGRATION_METADATA_TABLE_NAME,
83            statements: schema_statements(),
84        }
85    }
86}