diff --git a/Cargo.lock b/Cargo.lock index 32586c3..a2ca10a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -39,8 +39,8 @@ checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06" name = "bedrock" version = "0.1.0" dependencies = [ + "db", "serde_json", - "sql", "tokio", ] @@ -154,6 +154,18 @@ dependencies = [ "typenum", ] +[[package]] +name = "db" +version = "0.1.0" +dependencies = [ + "clusters", + "serde_json", + "sqlx", + "tempfile", + "thiserror", + "tokio", +] + [[package]] name = "der" version = "0.7.10" @@ -1118,18 +1130,6 @@ dependencies = [ "der", ] -[[package]] -name = "sql" -version = "0.1.0" -dependencies = [ - "clusters", - "serde_json", - "sqlx", - "tempfile", - "thiserror", - "tokio", -] - [[package]] name = "sqlx" version = "0.8.6" diff --git a/Cargo.toml b/Cargo.toml index 7b54633..08da929 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ rust-version = "1.91" [workspace.dependencies] clusters = { path = "crates/clusters" } -sql = { path = "crates/sql" } +db = { path = "crates/db" } serde = { version = "1", features = ["derive"] } serde_json = "1" sqlx = { version = "0.8.6", default-features = false, features = ["any", "runtime-tokio", "tls-rustls-ring-webpki"] } @@ -24,6 +24,6 @@ edition.workspace = true rust-version.workspace = true [dependencies] -sql.workspace = true +db.workspace = true serde_json.workspace = true tokio.workspace = true diff --git a/README.md b/README.md index 413704e..259e9f2 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ # Bedrock storage -Bedrock stores JSON documents in SQLite, PostgreSQL or MySQL. Each backend has its own module in the `sql` crate. SQLite is the default and creates `bedrock.sqlite3` in the current directory on first start. +Bedrock stores JSON documents in SQLite, PostgreSQL or MySQL. Each backend has its own module in the `db` crate. SQLite is the default and creates `bedrock.sqlite3` in the current directory on first start. ## Storage boundaries -`sql` owns the transactional document data, revisions, and snapshots used by the application. `clusters` resolves deployment settings. The `sql` connection module selects the SQL driver and read endpoints. +`db` owns the transactional document data, revisions, and snapshots used by the application. `clusters` resolves deployment settings. The `db` 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 `sql::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 `db::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. diff --git a/crates/sql/Cargo.toml b/crates/db/Cargo.toml similarity index 96% rename from crates/sql/Cargo.toml rename to crates/db/Cargo.toml index 65cf2b4..9717099 100644 --- a/crates/sql/Cargo.toml +++ b/crates/db/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "sql" +name = "db" version = "0.1.0" edition.workspace = true rust-version.workspace = true diff --git a/crates/sql/src/connection.rs b/crates/db/src/connection.rs similarity index 100% rename from crates/sql/src/connection.rs rename to crates/db/src/connection.rs diff --git a/crates/sql/src/lib.rs b/crates/db/src/lib.rs similarity index 100% rename from crates/sql/src/lib.rs rename to crates/db/src/lib.rs diff --git a/crates/sql/src/mysql.rs b/crates/db/src/mysql.rs similarity index 100% rename from crates/sql/src/mysql.rs rename to crates/db/src/mysql.rs diff --git a/crates/sql/src/postgres.rs b/crates/db/src/postgres.rs similarity index 100% rename from crates/sql/src/postgres.rs rename to crates/db/src/postgres.rs diff --git a/crates/sql/src/sqlite.rs b/crates/db/src/sqlite.rs similarity index 100% rename from crates/sql/src/sqlite.rs rename to crates/db/src/sqlite.rs diff --git a/crates/sql/tests/storage.rs b/crates/db/tests/storage.rs similarity index 99% rename from crates/sql/tests/storage.rs rename to crates/db/tests/storage.rs index 96580a8..baea0f5 100644 --- a/crates/sql/tests/storage.rs +++ b/crates/db/tests/storage.rs @@ -1,10 +1,10 @@ use std::{collections::BTreeMap, env, time::Duration}; -use serde_json::json; -use sql::{ +use db::{ Cluster, Connection, DeploymentConfig, Driver, Mutation, ReadPreference, StorageError, Tls, connect, }; +use serde_json::json; fn change(id: &str, text: &str) -> Mutation { Mutation { diff --git a/deploy/README.md b/deploy/README.md index 756d0ab..45d8eaa 100644 --- a/deploy/README.md +++ b/deploy/README.md @@ -6,7 +6,7 @@ Deployments pass the path of a JSON configuration file to the Bedrock executable - **PostgreSQL** injects the Gitea Actions variable values `BEDROCK_PG_HOST`, `BEDROCK_PG_PORT`, `BEDROCK_PG_DATABASE` and `BEDROCK_PG_USER`, and the secret `BEDROCK_PG_PASSWORD`, into the deployment step. Run `python3 deploy/render-postgres.py /path/to/private/bedrock.json --deployment production`. The renderer writes a private file and does not print its contents. Mount that file for the Bedrock service and set `BEDROCK_DB_CONFIG` to the mounted path. The configured database must already exist and the server must present a trusted certificate matching the database address. - **MySQL** accepts the same cluster shape described in the [storage configuration](../README.md) with `"driver": "mysql"` and a `mysql://` endpoint. -Keep `BEDROCK_S3_SECRET_KEY`, `BEDROCK_S3_ENDPOINT`, and `BEDROCK_S3_ACCESS_KEY` scoped to the deployment that consumes object storage. S3 integration is pending. The PostgreSQL renderer and `sql` use only SQL configuration. +Keep `BEDROCK_S3_SECRET_KEY`, `BEDROCK_S3_ENDPOINT`, and `BEDROCK_S3_ACCESS_KEY` scoped to the deployment that consumes object storage. S3 integration is pending. The PostgreSQL renderer and `db` use only SQL configuration. Pull requests currently run without an automatic storage check. The optional `ci/check-sql.sh` script uses isolated database containers and generated fixture credentials when run on an approved remote runner. diff --git a/deploy/bedrock-deployment.svg b/deploy/bedrock-deployment.svg index 9733078..2c7feab 100644 --- a/deploy/bedrock-deployment.svg +++ b/deploy/bedrock-deployment.svg @@ -72,11 +72,11 @@ Rust Bedrock 读取配置 -通过 sql::connect 选择 postgres。 +通过 db::connect 选择 postgres。 配置由 BEDROCK_DB_CONFIG 指定。 在已有 PostgreSQL 数据库内初始化 -postgres 模块提供 SQL,sql 在事务里执行。 +postgres 模块提供 SQL,db 在事务里执行。 创建 Bedrock 表并登记 production 部署。 进入 Store::initialize() 成功 diff --git a/src/main.rs b/src/main.rs index 9404e66..5036bf6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use std::{env, error::Error, fs}; -use sql::{DeploymentConfig, connect}; +use db::{DeploymentConfig, connect}; #[tokio::main] async fn main() -> Result<(), Box> {