This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "postgres"
|
||||
version = "0.1.0"
|
||||
edition.workspace = true
|
||||
rust-version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
persistence.workspace = true
|
||||
sqlx = { workspace = true, features = ["postgres"] }
|
||||
@@ -0,0 +1,40 @@
|
||||
//! PostgreSQL connection pools and the Bedrock document layout.
|
||||
|
||||
use persistence::{Dialect, Reader, Result, Store, pool};
|
||||
|
||||
const SCHEMA: &[&str] = &[
|
||||
"CREATE TABLE IF NOT EXISTS bedrock_deployments (
|
||||
deployment BYTEA PRIMARY KEY,
|
||||
revision BIGINT NOT NULL DEFAULT 0,
|
||||
retired BIGINT NOT NULL DEFAULT 0
|
||||
)",
|
||||
"CREATE TABLE IF NOT EXISTS bedrock_documents (
|
||||
deployment BYTEA NOT NULL REFERENCES bedrock_deployments(deployment),
|
||||
collection BYTEA NOT NULL,
|
||||
document_id BYTEA NOT NULL,
|
||||
revision BIGINT NOT NULL,
|
||||
payload BYTEA,
|
||||
PRIMARY KEY (deployment, collection, document_id, revision)
|
||||
)",
|
||||
];
|
||||
|
||||
pub async fn connect(url: &str, deployment: &str, max_connections: u32) -> Result<Store> {
|
||||
let pool = pool(Dialect::Postgres, url, max_connections, false)?;
|
||||
Store::initialize(
|
||||
pool,
|
||||
Dialect::Postgres,
|
||||
deployment,
|
||||
SCHEMA,
|
||||
"INSERT INTO bedrock_deployments (deployment) VALUES ($1)
|
||||
ON CONFLICT (deployment) DO NOTHING",
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub fn reader(url: &str, deployment: &str, max_connections: u32) -> Result<Reader> {
|
||||
Reader::new(
|
||||
pool(Dialect::Postgres, url, max_connections, true)?,
|
||||
Dialect::Postgres,
|
||||
deployment,
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user