From 632c3a8882db4605cb79ac924ed10f362b0b98ab Mon Sep 17 00:00:00 2001 From: Codex Bot Date: Mon, 30 Nov 2026 00:26:15 +0800 Subject: [PATCH] Scope SQL verification to main PRs and render deployment configuration --- .gitea/workflows/sql.yml | 4 ++- README.md | 2 ++ deploy/README.md | 11 ++++++ deploy/render-postgres.py | 74 +++++++++++++++++++++++++++++++++++++++ deploy/sqlite.json | 4 +++ 5 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 deploy/README.md create mode 100644 deploy/render-postgres.py create mode 100644 deploy/sqlite.json diff --git a/.gitea/workflows/sql.yml b/.gitea/workflows/sql.yml index fae370a..e5591ac 100644 --- a/.gitea/workflows/sql.yml +++ b/.gitea/workflows/sql.yml @@ -1,8 +1,8 @@ name: Bedrock SQL storage on: - push: pull_request: + branches: [main] jobs: sql: @@ -10,5 +10,7 @@ jobs: timeout-minutes: 30 steps: - uses: actions/checkout@v4 + with: + persist-credentials: false - name: Compile and test SQLite, PostgreSQL and MySQL run: bash ci/check-sql.sh diff --git a/README.md b/README.md index 2d1b1d4..1103162 100644 --- a/README.md +++ b/README.md @@ -24,3 +24,5 @@ The database must already exist for PostgreSQL and MySQL. Bedrock creates its ow 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. 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. + +See [deployment configuration](deploy/README.md) for the PostgreSQL configuration renderer and deployment-only variables. diff --git a/deploy/README.md b/deploy/README.md new file mode 100644 index 0000000..1f5cd60 --- /dev/null +++ b/deploy/README.md @@ -0,0 +1,11 @@ +# Deployment configuration + +Deployments pass the path of a JSON configuration file to the Bedrock executable using `BEDROCK_DB_CONFIG`. The Rust libraries accept structured configuration. + +- **SQLite** uses `deploy/sqlite.json` as a template. Set `sqlite_path` to a persistent, writable volume path. +- **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. The SQL renderer and crates have no S3 dependency. + +The `ces-control` storage checks run on pull requests targeting `main`. They use isolated database containers and generated fixture credentials. The workflow does not deploy the application. diff --git a/deploy/render-postgres.py b/deploy/render-postgres.py new file mode 100644 index 0000000..a3d00e2 --- /dev/null +++ b/deploy/render-postgres.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 +"""Render the private Bedrock PostgreSQL config at deploy time.""" + +import argparse +import ipaddress +import json +import os +from pathlib import Path +import re +import tempfile +from urllib.parse import quote + + +def required(variable): + value = os.environ.get(variable) + if not value: + raise SystemExit(f"Missing {variable}") + return value + + +def database_url(): + host = required("BEDROCK_PG_HOST").removeprefix("[").removesuffix("]") + if ":" in host: + try: + host = f"[{ipaddress.IPv6Address(host)}]" + except ValueError: + raise SystemExit("Invalid BEDROCK_PG_HOST") from None + elif not re.fullmatch(r"[A-Za-z0-9.-]+", host): + raise SystemExit("Invalid BEDROCK_PG_HOST") + + try: + port = int(required("BEDROCK_PG_PORT")) + if not 1 <= port <= 65535: + raise ValueError + except ValueError: + raise SystemExit("Invalid BEDROCK_PG_PORT") from None + + user = quote(required("BEDROCK_PG_USER"), safe="") + password = quote(required("BEDROCK_PG_PASSWORD"), safe="") + database = quote(required("BEDROCK_PG_DATABASE"), safe="") + return f"postgres://{user}:{password}@{host}:{port}/{database}" + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("output", type=Path) + parser.add_argument("--deployment", default="default") + options = parser.parse_args() + if not re.fullmatch(r"[A-Za-z0-9._-]{1,128}", options.deployment): + parser.error("invalid deployment name") + config = { + "deployment": options.deployment, + "cluster": "postgres", + "clusters": { + "postgres": { + "driver": "postgres", + "primary": database_url(), + } + }, + } + + # Write with owner-only permissions, then replace the destination atomically. + file = tempfile.NamedTemporaryFile(mode="w", dir=options.output.parent, delete=False) + try: + with file: + json.dump(config, file, indent=2) + file.write("\n") + os.replace(file.name, options.output) + finally: + Path(file.name).unlink(missing_ok=True) + + +if __name__ == "__main__": + main() diff --git a/deploy/sqlite.json b/deploy/sqlite.json new file mode 100644 index 0000000..6e61a4d --- /dev/null +++ b/deploy/sqlite.json @@ -0,0 +1,4 @@ +{ + "deployment": "default", + "sqlite_path": "bedrock.sqlite3" +}