Skip to main content

yugendb_postgres/
schema.rs

1//! PostgreSQL storage schema helpers for yugendb.
2//!
3//! The schema maps yugendb's namespace, collection, and key contract to one
4//! SQL storage table used by the PostgreSQL driver.
5
6/// Storage table used by the PostgreSQL driver.
7pub const STORE_TABLE_NAME: &str = "yugendb_store";
8/// Current schema version for the PostgreSQL storage table.
9pub const SCHEMA_VERSION: u32 = 1;
10/// Metadata table used for migration tracking.
11pub const MIGRATION_METADATA_TABLE_NAME: &str = "yugendb_migrations";
12/// Binary value column type for this backend.
13pub const VALUE_COLUMN_TYPE: &str = "BYTEA";
14
15/// Table creation statement for the yugendb storage table.
16pub const CREATE_STORE_TABLE_SQL: &str = "CREATE TABLE IF NOT EXISTS yugendb_store (
17    namespace TEXT NOT NULL,
18    collection TEXT NOT NULL,
19    key TEXT NOT NULL,
20    value BYTEA NOT NULL,
21    codec TEXT NOT NULL,
22    created_at TEXT NOT NULL,
23    updated_at TEXT NOT NULL,
24    expires_at TEXT,
25    PRIMARY KEY (namespace, collection, key)
26);";
27
28/// Prefix index used by prefix scans.
29pub const CREATE_PREFIX_INDEX_SQL: &str = "CREATE INDEX IF NOT EXISTS idx_yugendb_store_prefix ON yugendb_store(namespace, collection, key);";
30/// Expiry index used by TTL filtering.
31pub const CREATE_EXPIRES_AT_INDEX_SQL: &str =
32    "CREATE INDEX IF NOT EXISTS idx_yugendb_store_expires_at ON yugendb_store(expires_at);";
33/// Ordered schema statements for initial setup.
34pub const ALL_SCHEMA_STATEMENTS: [&str; 3] = [
35    CREATE_STORE_TABLE_SQL,
36    CREATE_PREFIX_INDEX_SQL,
37    CREATE_EXPIRES_AT_INDEX_SQL,
38];
39
40/// Returns all schema statements in execution order.
41#[must_use]
42pub const fn schema_statements() -> [&'static str; 3] {
43    ALL_SCHEMA_STATEMENTS
44}
45
46/// Structured schema description.
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub struct PostgresDriverStorageSchema {
49    /// Schema version.
50    pub version: u32,
51    /// Main storage table.
52    pub store_table_name: &'static str,
53    /// Migration metadata table.
54    pub migration_metadata_table_name: &'static str,
55    /// Binary value column type.
56    pub value_column_type: &'static str,
57}
58
59impl PostgresDriverStorageSchema {
60    /// Returns the current schema description.
61    #[must_use]
62    pub const fn current() -> Self {
63        Self {
64            version: SCHEMA_VERSION,
65            store_table_name: STORE_TABLE_NAME,
66            migration_metadata_table_name: MIGRATION_METADATA_TABLE_NAME,
67            value_column_type: VALUE_COLUMN_TYPE,
68        }
69    }
70}