Skip to content

Commit 7a3f394

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

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+
-p 5432:5432 -d postgres:15
31+
```
32+
33+
This creates a PostgreSQL instance with superuser `postgres` and password `password`. The `catcolab` database and user will be created automatically by the setup script in step 6.
34+
35+
The default `.env.development` file is pre-configured to work with this Docker setup.
36+
37+
- **Option B: Using local PostgreSQL installation**
38+
39+
Install and run PostgreSQL locally. Ensure you have superuser access (typically the `postgres` user).
40+
41+
**Note:** The default `.env.development` assumes the PostgreSQL superuser is `postgres` with password `password`.
42+
If your local PostgreSQL has different superuser credentials, you'll need to update `DATABASE_SUPERUSER_URL`
43+
in your `.env` file (see next step).
44+
45+
4. Change to the backend directory: `cd packages/backend`
46+
5. Copy the `.env.development` file: `cp .env.development .env`
47+
- If you're using the Docker command above as-is, the defaults will work perfectly
48+
- If you're using local PostgreSQL with different superuser credentials, you can either:
49+
- Update `DATABASE_SUPERUSER_URL` in `.env` to match your postgres superuser credentials, OR
50+
- Remove `DATABASE_SUPERUSER_URL` from `.env` and you'll be prompted for your postgres password when running the setup
51+
- **Note:** If you're using the Nix development shell (via `nix develop`), the environment is automatically configured and you don't need to copy `.env` files manually
52+
6. Run the database setup:
53+
- If using Nix shell: `cc-utils db setup` (the script is in your PATH)
54+
- Otherwise: `../../infrastructure/scripts/cc-utils db setup`
55+
- This command connects as the PostgreSQL superuser and automatically creates:
56+
- The `catcolab` database user with appropriate permissions
57+
- The `catcolab` database owned by that user
58+
- It then runs all database migrations
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)