Skip to content

Commit 18c4a44

Browse files
feat(fortuna): Support Postgres DB backend (#2841)
* use AnyPool for generic SQL cxn, add postgres migrations * migrations * fix sqlite issues * fix sqlite issues * remove old migrations, update migrations!() * bump ver
1 parent 87bc98a commit 18c4a44

20 files changed

+256
-191
lines changed

Cargo.lock

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/fortuna/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
*private-key*
55
.envrc
66
fortuna.db*
7+
.env*

apps/fortuna/.sqlx/query-03901bcfb28b127d99fe8a53e480b88336dd2aab632411114f02ce8dd8fe07e8.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

apps/fortuna/.sqlx/query-4c8c05ec08e128d847faafdd3d79fa50da70066f30b74f354e5d3a843ba6a2c0.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

apps/fortuna/.sqlx/query-b0d9afebb3825c3509ad80e5ebab5d72360326593407518770fe537ac3da1e10.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

apps/fortuna/Cargo.toml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "fortuna"
3-
version = "8.1.0"
3+
version = "8.2.0"
44
edition = "2021"
55

66
[lib]
@@ -46,8 +46,16 @@ chrono = { version = "0.4.38", features = [
4646
backoff = { version = "0.4.0", features = ["futures", "tokio"] }
4747
thiserror = "1.0.61"
4848
futures-locks = "0.7.1"
49-
sqlx = { version = "0.8", features = ["runtime-tokio", "sqlite", "chrono"] }
49+
sqlx = { version = "0.8", features = [
50+
"runtime-tokio",
51+
"tls-rustls",
52+
"sqlite",
53+
"any",
54+
"postgres",
55+
"chrono",
56+
] }
5057
num-traits = "0.2.19"
58+
dotenv = "0.15.0"
5159

5260
[dev-dependencies]
5361
axum-test = "13.1.1"

apps/fortuna/README.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,38 @@ Each blockchain is configured in `config.yaml`.
1010

1111
## Build & Test
1212

13-
We use sqlx query macros to check the SQL queries at compile time. This requires
14-
a database to be available at build time. Create a `.env` file in the root of the project with the following content:
13+
Fortuna uses Cargo for building and dependency management.
14+
Simply run `cargo build` and `cargo test` to build and test the project.
15+
To run Fortuna locally, see the [Local Development](#local-development) section below.
1516

17+
### Connect a database
18+
Fortuna stores request history in a SQL database and serves it from its explorer API.
19+
Any SQLite or Postgres database is supported. The database connection is sourced from the `DATABASE_URL` env var.
20+
Create a `.env` file in the root of the project with a DB connection string.
1621
```
1722
DATABASE_URL="sqlite:fortuna.db?mode=rwc"
1823
```
24+
If not provided, Fortuna will create and use a SQLite file-based database at `./fortuna.db`, as in the example above.
25+
26+
### Database migrations
27+
Fortuna will automatically apply the schema migrations in the `./migrations` directory when connecting to the database.
28+
To manually administer the migrations, use the `sqlx` tool for cargo. The tool automatically uses the
29+
database connection in the `.env` file.
1930

20-
Install sqlx for cargo with:
31+
Install `sqlx`:
2132
```bash
2233
cargo install sqlx
2334
```
2435

25-
Next, you need to create the database and apply the schema migrations. You can do this by running:
26-
36+
To create the database if needed and apply the migrations:
2737
```bash
28-
cargo sqlx migrate run # automatically picks up the .env file
38+
cargo sqlx migrate run
2939
```
30-
This will create a SQLite database file called `fortuna.db` in the root of the project and apply the schema migrations to it.
31-
This will allow `cargo check` to check the queries against the existing database.
32-
33-
Fortuna uses Cargo for building and dependency management.
34-
Simply run `cargo build` and `cargo test` to build and test the project.
35-
36-
If you have changed any queries in the code, you need to update the .sqlx folder with the new queries:
3740

41+
To restore the database to a fresh state (drop, recreate, apply migrations):
3842
```bash
39-
cargo sqlx prepare
43+
cargo sqlx database reset
4044
```
41-
Please add the changed files in the `.sqlx` folder to your git commit.
4245

4346
## Command-Line Interface
4447

@@ -124,7 +127,7 @@ To start an instance of the webserver for local testing, you first need to perfo
124127
1. Run `cargo run -- setup-provider` to register a randomness provider for this service. This command
125128
will update the on-chain contracts such that the configured provider key is a randomness provider,
126129
and its on-chain configuration matches `config.yaml`.
127-
130+
1. Review the [Connect a database](#connect-a-database) section above. The default configuration will create a file-based DB.
128131
Once you've completed the setup, simply run the following command to start the service:
129132

130133
```bash

apps/fortuna/migrations/20250502164500_init.down.sql

Lines changed: 0 additions & 1 deletion
This file was deleted.

apps/fortuna/migrations/20250502164500_init.up.sql

Lines changed: 0 additions & 26 deletions
This file was deleted.

apps/fortuna/migrations/20250521203448_gas.down.sql

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)