Skip to content

Commit 345dc64

Browse files
author
Jason Moggridge
committed
DOC: Update backend docs for development database setup
1 parent 17ecd55 commit 345dc64

File tree

2 files changed

+53
-15
lines changed

2 files changed

+53
-15
lines changed

packages/backend/.env.development

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
DATABASE_URL=postgres://postgres-user:password@localhost:5432/catcolab
1+
DATABASE_URL=postgres://catcolab:password@localhost:5432/catcolab
2+
DATABASE_SUPERUSER_URL=postgresql://postgres:password@localhost:5432/postgres
23
FIREBASE_PROJECT_ID=catcolab-next

packages/backend/README.md

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,8 @@ You can find the auto-generated documentation for this Rust crate at [next.catco
99
## Setup
1010

1111
1. Install Rust, say by using [rustup](https://rustup.rs/)
12-
2. Install and run PostgreSQL and create a new database named `catcolab`
13-
- (E.g. by using Docker)
1412

15-
```sh
16-
docker run --name catcolab-postgres -e POSTGRES_USER=postgres-user \
17-
-e POSTGRES_PASSWORD=password -e POSTGRES_DB=catcolab -p 5432:5432 -d postgres:15
18-
```
19-
20-
3. Make sure the required packages are built and installed:
13+
2. Make sure the required packages are built and installed:
2114

2215
```sh
2316
cd packages/notebook-types
@@ -26,10 +19,43 @@ You can find the auto-generated documentation for this Rust crate at [next.catco
2619
pnpm install
2720
```
2821

29-
4. Change to the migrator directory: `cd ../backend`
30-
5. Copy the .env.development to both folders (`cp .env.development .env && cp .env.development ../migrator/.env`) and update the `DATABASE_URL` variable with
31-
database username, password, and port. (If you used the above Docker command _as is_ it should already be correct.)
32-
6. Run the initial database migration: `cargo run -p migrator apply`
22+
3. Set up PostgreSQL (choose one of the options below)
23+
24+
### Option A: Using Docker (Recommended)
25+
26+
Run PostgreSQL in a Docker container:
27+
28+
```sh
29+
docker run --name catcolab-postgres -e POSTGRES_PASSWORD=password \
30+
-e POSTGRES_DB=catcolab -p 5432:5432 -d postgres:15
31+
```
32+
33+
This creates a PostgreSQL instance with:
34+
- Superuser: `postgres` with password `password`
35+
- Initial database: `catcolab` (will be set up in next steps)
36+
37+
The default `.env.development` file is pre-configured to work with this Docker setup.
38+
39+
### Option B: Using local PostgreSQL installation
40+
41+
Install and run PostgreSQL locally. Ensure you have superuser access (typically the `postgres` user).
42+
43+
**Note:** The default `.env.development` assumes the PostgreSQL superuser is `postgres` with password `password`.
44+
If your local PostgreSQL has different superuser credentials, you'll need to update `DATABASE_SUPERUSER_URL`
45+
in your `.env` file (see next step).
46+
47+
### Complete the setup
48+
49+
4. Change to the backend directory: `cd packages/backend`
50+
5. Copy the `.env.development` file: `cp .env.development .env`
51+
- If you're using the Docker command above as-is, the defaults will work perfectly
52+
- If you're using local PostgreSQL with different superuser credentials, update `DATABASE_SUPERUSER_URL` in `.env`
53+
6. Run the database setup: `../../infrastructure/scripts/cc-utils db setup`
54+
- This command connects as the PostgreSQL superuser and automatically creates:
55+
- The `catcolab` database user with appropriate permissions
56+
- The `catcolab` database owned by that user
57+
- It then runs all database migrations
58+
- The command is idempotent and safe to run multiple times
3359
7. Build the backend binary: `cargo build`
3460
8. Run the unit tests: `cargo test`
3561

@@ -72,10 +98,21 @@ pnpm run dev
7298

7399
## Running migrations
74100

75-
This package runs databaes migrations using `migrator` subcommand which uses the
101+
This package runs database migrations using the `migrator` subcommand which uses the
76102
[sqlx_migrator](https://github.com/iamsauravsharma/sqlx_migrator) framework.
77103

78-
### Usage
104+
### Recommended: Using cc-utils
105+
106+
The easiest way to set up and migrate the database is using the `cc-utils` script:
107+
108+
```sh
109+
../../infrastructure/scripts/cc-utils db setup
110+
```
111+
112+
This command handles database creation, permissions, and migrations automatically.
113+
114+
### Advanced: Direct migrator usage
115+
79116
The migrator tool can be run from any directory using the `cargo run -p backend migrator ...` command.
80117
The migrator tool uses the default CLI interface provided by `sqlx_migrator`, which is very similar to
81118
the `sqlx` CLI.

0 commit comments

Comments
 (0)