pub const SCHEMA_VERSION: u32 = 1;
pub const STORE_TABLE_NAME: &str = "yugendb_store";
pub const MIGRATION_METADATA_TABLE_NAME: &str = "yugendb_schema_migrations";
pub const CREATE_STORE_TABLE_SQL: &str = r#"CREATE TABLE IF NOT EXISTS yugendb_store (
namespace TEXT NOT NULL,
collection TEXT NOT NULL,
key TEXT NOT NULL,
value BLOB NOT NULL,
codec TEXT NOT NULL,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL,
expires_at TEXT,
PRIMARY KEY (namespace, collection, key)
);"#;
pub const CREATE_PREFIX_INDEX_SQL: &str = r#"CREATE INDEX IF NOT EXISTS idx_yugendb_store_prefix
ON yugendb_store(namespace, collection, key);"#;
pub const CREATE_EXPIRES_AT_INDEX_SQL: &str = r#"CREATE INDEX IF NOT EXISTS idx_yugendb_store_expires_at
ON yugendb_store(expires_at);"#;
pub const CREATE_MIGRATION_METADATA_TABLE_SQL: &str = r#"CREATE TABLE IF NOT EXISTS yugendb_schema_migrations (
version INTEGER PRIMARY KEY,
applied_at TEXT NOT NULL
);"#;
pub const ALL_SCHEMA_STATEMENTS: [&str; 4] = [
CREATE_STORE_TABLE_SQL,
CREATE_PREFIX_INDEX_SQL,
CREATE_EXPIRES_AT_INDEX_SQL,
CREATE_MIGRATION_METADATA_TABLE_SQL,
];
#[must_use]
pub const fn schema_statements() -> &'static [&'static str] {
&ALL_SCHEMA_STATEMENTS
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SqliteStorageSchema {
pub version: u32,
pub store_table_name: &'static str,
pub migration_metadata_table_name: &'static str,
pub statements: &'static [&'static str],
}
impl SqliteStorageSchema {
#[must_use]
pub const fn current() -> Self {
Self {
version: SCHEMA_VERSION,
store_table_name: STORE_TABLE_NAME,
migration_metadata_table_name: MIGRATION_METADATA_TABLE_NAME,
statements: schema_statements(),
}
}
}