Skip to main content

rbp_database/
lib.rs

1//! Database pipeline for training artifacts.
2//!
3//! Bulk data movement between Rust structures and PostgreSQL, optimized for
4//! the large-scale writes required during abstraction and blueprint training.
5//!
6//! ## Connectivity
7//!
8//! - [`db()`] — Establishes a database connection from `DB_URL`
9//!
10//! ## Serialization Traits
11//!
12//! - [`Schema`] — Table metadata and DDL generation
13//! - [`Derive`] — INSERT statement generation for enumerable types
14//! - [`Hydrate`] — Binary format decoding from rows
15//! - [`Row`] — Binary row serialization for COPY protocol
16//! - [`Streamable`] — Bulk data upload via COPY
17//!
18//! ## Core Types
19//!
20//! - [`Stage`] — Temporary staging table management
21//! - [`Check`] — Schema validation and migration status
22//!
23//! ## Table Names
24//!
25//! Constants for all persistent entities: abstractions, blueprints,
26//! metrics, hands, sessions, and more.
27mod check;
28mod schema;
29mod stage;
30mod traits;
31
32pub use check::*;
33pub use stage::*;
34// schema module provides trait impls, no items to re-export
35pub use traits::*;
36
37use std::sync::Arc;
38use tokio_postgres::Client;
39
40/// Establishes a database connection.
41///
42/// Connects to PostgreSQL using the `DB_URL` environment variable.
43/// Returns an `Arc<Client>` suitable for sharing across async tasks.
44///
45/// # Environment
46///
47/// Requires `DB_URL` to be set (e.g., `postgres://user:pass@host:port/db`).
48///
49/// # Panics
50///
51/// Panics if `DB_URL` is not set or if connection fails.
52pub async fn db() -> Arc<Client> {
53    log::info!("connecting to database");
54    let tls = tokio_postgres::tls::NoTls;
55    let ref url = std::env::var("DB_URL").expect("DB_URL must be set");
56    let (client, connection) = tokio_postgres::connect(url, tls)
57        .await
58        .expect("database connection failed");
59    tokio::spawn(connection);
60    client
61        .execute("SET client_min_messages TO WARNING", &[])
62        .await
63        .expect("set client_min_messages");
64    Arc::new(client)
65}
66
67/// PostgreSQL error type alias.
68pub type PgErr = tokio_postgres::Error;
69
70/// Table for abstraction bucket definitions.
71#[rustfmt::skip]
72pub const ABSTRACTION: &str = "abstraction";
73/// Table for game actions (bets, raises, folds, etc.).
74#[rustfmt::skip]
75pub const ACTIONS:     &str = "actions";
76/// Table for MCCFR blueprint strategies (policy + regret).
77#[rustfmt::skip]
78pub const BLUEPRINT:   &str = "blueprint";
79/// Table for training epoch metadata and progress.
80#[rustfmt::skip]
81pub const EPOCH:       &str = "epoch";
82/// Table for completed poker hands.
83#[rustfmt::skip]
84pub const HANDS:       &str = "hands";
85/// Table for isomorphism → abstraction mappings.
86#[rustfmt::skip]
87pub const ISOMORPHISM: &str = "isomorphism";
88/// Table for pairwise abstraction distances.
89#[rustfmt::skip]
90pub const METRIC:      &str = "metric";
91/// Table for player participation in hands.
92#[rustfmt::skip]
93pub const PLAYERS:     &str = "players";
94/// Table for active game rooms.
95#[rustfmt::skip]
96pub const ROOMS:       &str = "rooms";
97/// Table for user authentication sessions.
98#[rustfmt::skip]
99pub const SESSIONS:    &str = "sessions";
100/// Table for staging data during bulk operations.
101#[rustfmt::skip]
102pub const STAGING:     &str = "staging";
103/// Table for street-specific metadata.
104#[rustfmt::skip]
105pub const STREET:      &str = "street";
106/// Table for abstraction transition probabilities.
107#[rustfmt::skip]
108pub const TRANSITIONS: &str = "transitions";
109/// Table for registered user accounts.
110#[rustfmt::skip]
111pub const USERS:       &str = "users";