2026-05-05 18:27:36 +08:00
# aether-data
2026-05-08 00:18:12 +08:00
`aether-data` is the runtime data-access crate. It owns concrete SQL/database
drivers, concrete repository implementations, migration/backfill/export
2026-05-05 18:27:36 +08:00
workflows, and the composition layer that wires those pieces into the rest of
the application.
It does not own the cross-crate DTO contracts. Shared repository records and
errors that are consumed by scheduler, billing, admin, usage runtime, and video
2026-07-15 23:47:19 +08:00
task crates live in the sibling `../contracts` crate (`aether-data-contracts` ).
2026-05-05 18:27:36 +08:00
## Directory Map
| Path | Responsibility |
|---|---|
| `src/database.rs` | Logical SQL driver selection and shared pool configuration. |
2026-05-08 00:18:12 +08:00
| `src/config.rs` | Data-layer config for SQL drivers and repository wiring. |
2026-05-05 18:27:36 +08:00
| `src/maintenance.rs` | Maintenance DTOs and aggregation summaries used by backend dispatch and runtime maintenance entrypoints. |
2026-09-07 00:09:42 +08:00
| `src/driver/postgres.rs` | Thin compatibility facades for the selected adapter crates. Low-level pools, transactions, and leases live in `aether-data-postgres` . |
2026-07-15 23:47:19 +08:00
| `src/repository` | Compatibility modules that re-export contracts and selected SQL adapters, plus concrete in-memory implementations. Driver-specific request-path SQL lives in the adapter crates. |
2026-05-05 18:27:36 +08:00
| `src/backend` | Composition root. Builds concrete driver backends and exposes app-facing read/write/worker/lock/lease handles. |
| `src/backend/{maintenance,stats,wallet,system}.rs` | Backend-owned maintenance, aggregation, wallet ledger, and system config workflows that are not normal request-path repositories. |
2026-07-15 23:47:19 +08:00
| `src/lifecycle/migrate.rs` and `src/lifecycle/migrate/*` | Compatibility entry points and Postgres snapshot bootstrap adapter. |
2026-05-05 18:27:36 +08:00
| `src/lifecycle/backfill.rs` | Backfill entry points and backfill discovery. |
| `src/lifecycle/export.rs` | Cross-database export/import workflows. |
2026-09-07 00:09:42 +08:00
| `../adapters/postgres/migrations` | Executable `sqlx` migrations owned and embedded by each adapter crate. |
2026-05-05 18:27:36 +08:00
| `schema` | Schema maintenance workspace for logical definitions, driver fragments, and generated output. |
| `schema/logical` | Human-maintained logical table definitions used by `aether-data-schema` . |
2026-09-07 00:09:42 +08:00
| `schema/drivers/postgres` | Human-maintained driver fragments that compose back into executable SQL while generation is being promoted. |
2026-05-05 18:27:36 +08:00
| `schema/bootstrap/postgres` | Human-maintained source fragments for the Postgres bootstrap snapshot. `build.rs` composes them into the runtime embedded artifact during crate builds. |
| `schema/generated` | Machine-written SQL generated from logical schema for audit and drift detection only. |
| `schema/overrides` | Rare driver-specific SQL escape hatch. Keep README-only until a real override is needed. |
2026-09-07 00:09:42 +08:00
| `backfills/postgres` | Executable backfill SQL grouped by driver. |
2026-05-05 18:27:36 +08:00
## Layering Rules
The crate is easiest to read as five layers:
1. Contracts: DTOs, input structs, repository traits, and `DataLayerError` .
Prefer `aether-data-contracts` for anything that another crate needs to
compile against.
2026-09-07 00:09:42 +08:00
2. Driver primitives: `aether-data-postgres` connect to infrastructure and expose pools, runners,
2026-07-15 23:47:19 +08:00
and executable migrations;
the matching `src/driver/*.rs` files only preserve the existing import paths.
2026-09-07 00:09:42 +08:00
3. Repository implementations: `aether-data-postgres` translate
2026-07-15 23:47:19 +08:00
contract types to driver-specific SQL. `src/repository/<domain>` keeps the
compatibility import path and owns the in-memory implementation where one
exists.
2026-05-05 18:27:36 +08:00
4. Backend composition: `backend` chooses one SQL driver from config and wires
repository implementations into app-facing handles. Backend-owned runtime
maintenance workflows live in focused backend modules rather than in the
driver pool files.
5. Maintenance workflows: `lifecycle/migrate` , `lifecycle/backfill` ,
`lifecycle/export` , and `schema` manage database lifecycle outside normal
request handling.
Do not add domain queries to low-level pool modules. Do not add driver selection
logic inside individual repository implementations. Keep cross-crate contracts
out of `aether-data` unless they are implementation-only.
## Repository Layout
Most domain repositories use this shape:
```text
src/repository/<domain>/
mod.rs # exports trait/type names and concrete implementations
types.rs # implementation-local DTOs when they are not already in contracts
postgres.rs # Postgres implementation
memory.rs # tests/dev in-memory implementation
```
Use explicit driver filenames for repository implementations. Do not introduce
new generic `sql.rs` modules for driver-specific code.
## SQL Driver Policy
2026-09-07 00:09:42 +08:00
The repository/backend boundary supports PostgreSQL only. Repository contracts
remain independent of SQLx, while physical SQL, pools, and migrations belong to
`aether-data-postgres` .
2026-05-05 18:27:36 +08:00
2026-09-07 00:09:42 +08:00
| Logical type | PostgreSQL |
|---|---|
| `json` | `json` or `jsonb` |
| `bool` | `boolean` |
| `time_unix` | `bigint` or timestamp |
| `money_decimal` | `numeric` / legacy double |
2026-05-05 18:27:36 +08:00
2026-07-15 23:47:19 +08:00
### Feature Matrix
2026-09-07 00:09:42 +08:00
`aether-data` enables PostgreSQL by default. The `all-drivers` feature selects
the same PostgreSQL backend; a no-driver build remains available for pure
in-memory repository tests.
2026-07-15 23:47:19 +08:00
```bash
cargo check -p aether-data
2026-09-07 00:09:42 +08:00
cargo check -p aether-data --no-default-features
cargo check -p aether-data --no-default-features --features postgres
2026-07-15 23:47:19 +08:00
```
2026-09-07 00:09:42 +08:00
Database URLs must use `postgres:` or `postgresql:` . Unsupported drivers and
URL schemes fail explicitly instead of falling back to another backend.
2026-07-15 23:47:19 +08:00
2026-05-05 18:27:36 +08:00
## Schema Maintenance
2026-07-15 23:47:19 +08:00
Executable migrations live under each adapter crate's `migrations/` directory.
Moving their physical files does not change SQLx migration versions or deployed
database history because SQLx records migration version and checksum, not the
workspace source path.
2026-05-05 18:27:36 +08:00
The large baseline SQL files are maintained through `schema` fragments. New
table-structure work should start in `schema/logical/*.toml` and be generated
into driver-specific SQL before it is composed into executable migrations:
```bash
2026-07-15 23:47:19 +08:00
bash crates/aether-data/runtime/schema/compose_schema.sh generate
bash crates/aether-data/runtime/schema/compose_schema.sh compose
bash crates/aether-data/runtime/schema/compose_schema.sh check
2026-05-05 18:27:36 +08:00
```
`schema/generated/**` is machine-written by `aether-data-schema` ; it is checked
in to make generator drift reviewable, not because runtime reads it. Edit
`schema/logical/*.toml` instead. Use `schema/overrides/**` only as an exception
bucket for driver-specific SQL that cannot live cleanly in logical schema or the
normal driver fragments.
`compose_schema.sh check` also verifies that required baseline/portable
table-creation SQL is represented in `schema/logical` . This is the guardrail
2026-09-07 00:09:42 +08:00
that keeps table structure from drifting back into multiple manually maintained
2026-05-05 18:27:36 +08:00
definitions.
For executable fragments that have not been promoted to generated output yet,
2026-09-07 00:09:42 +08:00
edit fragments under `schema/drivers/postgres` directly, run
2026-05-05 18:27:36 +08:00
`compose` , then run `check` . Do not edit baseline executable SQL and fragments
independently.
When adding a table:
1. Add or update `schema/logical/*.toml` first for the table structure.
2. Run `schema/compose_schema.sh generate` and inspect the generated driver SQL.
3. Add or update the executable driver-specific migration/fragments only for
deployment compatibility or generator gaps.
4. Add or update repository contracts in `aether-data-contracts` if other crates
need the new shape.
5. Add driver repository implementations only for the drivers that are actually
supported for that domain.
6. Wire new repositories through `src/backend/read.rs` or `src/backend/write.rs`
only after the implementation exists for each selected driver.
2026-07-15 23:47:19 +08:00
7. Update `docs/architecture/data-layer.md` when ownership, import, or logical
type policy changes.
2026-05-05 18:27:36 +08:00
## Known Cleanup Targets
2026-09-07 00:09:42 +08:00
These are intentionally staged to keep the data-layer refactor reviewable:
2026-05-05 18:27:36 +08:00
2026-07-15 23:47:19 +08:00
1. Retire the compatibility re-export paths once downstream crates use
`aether-data-contracts` for contracts and `aether-data` only as the runtime
composition facade.
2. Group repository domains once file-level names are stable. Likely groups:
2026-05-05 18:27:36 +08:00
identity, auth config, provider catalog, runtime tasks, wallet/billing,
usage, stats, and proxy nodes.
2026-07-15 23:47:19 +08:00
3. Continue shrinking Postgres stats SQL modules where useful by moving shared
2026-05-05 18:27:36 +08:00
SQL fragments and row-mapping helpers behind focused `backend/stats/*`
modules.
2026-07-15 23:47:19 +08:00
4. Split very large `usage` , `wallet` , and stats implementations into focused
query/mapping/read/write/test modules before considering any additional
crates. New crate boundaries should follow stable ownership, not file size
alone.