Move SQL connection routing into sql_storage

This commit is contained in:
2026-11-30 03:50:39 +08:00
parent bb15a7043c
commit 36184b07a0
10 changed files with 29 additions and 42 deletions
Generated
+4 -13
View File
@@ -39,8 +39,8 @@ checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06"
name = "bedrock"
version = "0.1.0"
dependencies = [
"db_connection",
"serde_json",
"sql_storage",
"tokio",
]
@@ -154,18 +154,6 @@ dependencies = [
"typenum",
]
[[package]]
name = "db_connection"
version = "0.1.0"
dependencies = [
"clusters",
"serde_json",
"sql_storage",
"tempfile",
"thiserror",
"tokio",
]
[[package]]
name = "der"
version = "0.7.10"
@@ -1134,9 +1122,12 @@ dependencies = [
name = "sql_storage"
version = "0.1.0"
dependencies = [
"clusters",
"serde_json",
"sqlx",
"tempfile",
"thiserror",
"tokio",
]
[[package]]
+1 -2
View File
@@ -8,7 +8,6 @@ rust-version = "1.91"
[workspace.dependencies]
clusters = { path = "crates/clusters" }
db_connection = { path = "crates/db_connection" }
sql_storage = { path = "crates/sql_storage" }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
@@ -25,6 +24,6 @@ edition.workspace = true
rust-version.workspace = true
[dependencies]
db_connection.workspace = true
sql_storage.workspace = true
serde_json.workspace = true
tokio.workspace = true
+2 -2
View File
@@ -4,7 +4,7 @@ Bedrock stores JSON documents in SQLite, PostgreSQL or MySQL. Each backend has i
## Storage boundaries
`sql_storage` owns the transactional document data, revisions, and snapshots used by the application. `clusters` resolves deployment settings and `db_connection` selects the SQL driver and read endpoints.
`sql_storage` owns the transactional document data, revisions, and snapshots used by the application. `clusters` resolves deployment settings. The `sql_storage` connection module selects the SQL driver and read endpoints.
Notion describes sharding online PostgreSQL block data by workspace and feeding a downstream S3 data lake from PostgreSQL changes. Bedrock currently supports selecting a configured SQL cluster and using its read replicas. Workspace sharding, S3 object storage, and data-lake ingestion are future work. [Notion's PostgreSQL sharding](https://www.notion.com/blog/sharding-postgres-at-notion) · [Notion's data lake](https://www.notion.com/blog/building-and-scaling-notions-data-lake)
@@ -29,7 +29,7 @@ Set `BEDROCK_DB_CONFIG` to the path of a private JSON deployment configuration t
The database must already exist for PostgreSQL and MySQL. Bedrock creates its own tables and deployment row on a writable startup. Network endpoints use TLS with certificate and hostname verification by default. For a private test database without TLS, set the cluster's `tls` to `"disabled"`. A top-level `"read_only": true` opens an existing deployment without initializing it. A SQLite deployment can set `sqlite_path` to use another file.
Applications connect with `db_connection::connect`. A write batch commits at one new revision if its expected deployment revision still matches. Reads can target the primary or prefer replicas. Use an explicit revision to require that snapshot and fall back when replicas have fallen behind. A tombstone removes a document from subsequent snapshots while preserving earlier revisions. JSON null remains a stored value.
Applications connect with `sql_storage::connect`. A write batch commits at one new revision if its expected deployment revision still matches. Reads can target the primary or prefer replicas. Use an explicit revision to require that snapshot and fall back when replicas have fallen behind. A tombstone removes a document from subsequent snapshots while preserving earlier revisions. JSON null remains a stored value.
For MySQL, use `Connection::mysql_writer` to acquire, renew or release a writer lease. An active lease blocks ordinary writes. Use `commit_with_lease` while holding it. `delete_deployment` requires an active lease, deletes that deployment's documents and permanently retires its ID in the database.
-15
View File
@@ -1,15 +0,0 @@
[package]
name = "db_connection"
version = "0.1.0"
edition.workspace = true
rust-version.workspace = true
[dependencies]
clusters.workspace = true
sql_storage.workspace = true
thiserror.workspace = true
[dev-dependencies]
serde_json.workspace = true
tempfile.workspace = true
tokio.workspace = true
+5
View File
@@ -5,6 +5,11 @@ edition.workspace = true
rust-version.workspace = true
[dependencies]
clusters.workspace = true
serde_json.workspace = true
sqlx = { workspace = true, features = ["mysql", "postgres", "sqlite"] }
thiserror.workspace = true
[dev-dependencies]
tempfile.workspace = true
tokio.workspace = true
@@ -2,14 +2,15 @@
use std::sync::atomic::{AtomicUsize, Ordering};
pub use clusters::{Address, Cluster, ConfigError, DeploymentConfig, Driver, Tls};
pub use sql_storage::mysql::{Lease, MySql};
pub use sql_storage::{Document, Error as StorageError, Mutation, Revision, Snapshot};
use clusters::ResolvedDeployment;
use sql_storage::{Reader, Store, mysql, postgres, sqlite};
use clusters::{Address, ConfigError, DeploymentConfig, Driver, ResolvedDeployment};
use thiserror::Error;
use crate::{
Error as StorageError, Mutation, Reader, Revision, Snapshot, Store,
mysql::{self, MySql},
postgres, sqlite,
};
#[derive(Debug, Error)]
pub enum ConnectError {
#[error(transparent)]
+6
View File
@@ -1,9 +1,15 @@
//! Bedrock's transactional document storage for SQL databases.
mod connection;
pub mod mysql;
pub mod postgres;
pub mod sqlite;
pub use Error as StorageError;
pub use clusters::{Address, Cluster, ConfigError, DeploymentConfig, Driver, Tls};
pub use connection::{ConnectError, Connection, ReadPreference, connect};
pub use mysql::{Lease, MySql};
use std::{borrow::Cow, collections::HashSet, fmt::Write, time::Duration};
use serde_json::Value;
@@ -1,10 +1,10 @@
use std::{collections::BTreeMap, env, time::Duration};
use db_connection::{
use serde_json::json;
use sql_storage::{
Cluster, Connection, DeploymentConfig, Driver, Mutation, ReadPreference, StorageError, Tls,
connect,
};
use serde_json::json;
fn change(id: &str, text: &str) -> Mutation {
Mutation {
+1 -1
View File
@@ -72,7 +72,7 @@
<rect x="84" y="663" width="452" height="77" rx="9" fill="#e0eee0" stroke="#8eaf98"/>
<path d="M93 675v53" stroke="#8bac94" stroke-width="2" stroke-linecap="round"/>
<text x="104" y="692" class="headline">Rust Bedrock 读取配置</text>
<text x="104" y="713" class="detail">通过 db_connection 选择 postgres。</text>
<text x="104" y="713" class="detail">通过 sql_storage::connect 选择 postgres。</text>
<text x="104" y="732" class="detail">配置由 BEDROCK_DB_CONFIG 指定。</text>
<rect x="84" y="821" width="452" height="75" rx="9" fill="#e0eee0" stroke="#8eaf98"/>
<text x="104" y="847" class="headline">在已有 PostgreSQL 数据库内初始化</text>

Before

Width:  |  Height:  |  Size: 7.4 KiB

After

Width:  |  Height:  |  Size: 7.4 KiB

+1 -1
View File
@@ -1,6 +1,6 @@
use std::{env, error::Error, fs};
use db_connection::{DeploymentConfig, connect};
use sql_storage::{DeploymentConfig, connect};
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {