Skip to content

Commit 3b8cd66

Browse files
feat(labrinth): database seed data fixtures for better installation and testing (#4132)
* feat(labrinth): database seed data fixtures for better installation and testing * chore(labrinth): simplify and fixup seed data fixture * docs(contributing/labrinth): enable all useful features for `sqlx-cli` install * chore(docs/labrinth): fix typo * chore(docs/labrinth): fix `cargo fmt` parameter typo * chore: replace Labrinth -> labrinth
1 parent 8af65f5 commit 3b8cd66

File tree

11 files changed

+1137
-48
lines changed

11 files changed

+1137
-48
lines changed

.github/workflows/turbo-ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ jobs:
6868
- name: ⚙️ Start services
6969
run: docker compose up --wait
7070

71-
- name: ⚙️ Setup Labrinth environment and database
71+
- name: ⚙️ Setup labrinth environment and database
7272
working-directory: apps/labrinth
7373
run: |
7474
cp .env.local .env

apps/docs/src/content/docs/contributing/labrinth.md

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,41 @@ title: Labrinth (API)
33
description: Guide for contributing to Modrinth's backend
44
---
55

6-
This project is part of our [monorepo](https://github.com/modrinth/code). You can find it in the `apps/labrinth` directory.
6+
This project is part of our [monorepo](https://github.com/modrinth/code). You can find it in the `apps/labrinth` directory. The instructions below assume that you have switched your working directory to the `apps/labrinth` subdirectory.
77

88
[labrinth] is the Rust-based backend serving Modrinth's API with the help of the [Actix](https://actix.rs) framework. To get started with a labrinth instance, install docker, docker-compose (which comes with Docker), and [Rust]. The initial startup can be done simply with the command `docker-compose up`, or with `docker compose up` (Compose V2 and later). That will deploy a PostgreSQL database on port 5432 and a MeiliSearch instance on port 7700. To run the API itself, you'll need to use the `cargo run` command, this will deploy the API on port 8000.
99

1010
To get a basic configuration, copy the `.env.local` file to `.env`. Now, you'll have to install the sqlx CLI, which can be done with cargo:
1111

12-
```bash
13-
cargo install --git https://github.com/launchbadge/sqlx sqlx-cli --no-default-features --features postgres,rustls
12+
```sh
13+
cargo install sqlx-cli --no-default-features --features mysql,sqlite,postgres,rustls,completions
1414
```
1515

16-
From there, you can create the database and perform all database migrations with one simple command:
16+
From there, you can create the database and set up its schema with one simple command:
1717

18-
```bash
19-
sqlx database setup
18+
```sh
19+
cargo sqlx database setup
2020
```
2121

22-
To enable labrinth to create a project, you need to add two things.
22+
To enable labrinth to create projects and serve useful metadata to the frontend build scripts, you'll need to seed the database with several key entities:
2323

24-
1. An entry in the `loaders` table.
25-
2. An entry in the `loaders_project_types` table.
24+
1. Categories, in the `categories` table.
25+
2. Loaders and their fields, in the `loaders`, `loader_fields`, `loader_field_enums`, `loader_field_enum_values`, and `loader_fields_loaders` tables.
26+
3. Project types and their allowed loaders and games, in the `project_types`, `loaders_project_types`, and `loaders_project_types_games` tables.
27+
4. Optionally, to moderate projects from the frontend, an admin user, in the `users` table.
2628

27-
A minimal setup can be done from the command line with [psql](https://www.postgresql.org/docs/current/app-psql.html):
29+
The most convenient way to do this seeding is with the [psql](https://www.postgresql.org/docs/current/app-psql.html) command line tool and the pre-existing seed data fixture. This fixture was generated by dumping the official staging environment database at a specific point in time, and defines an admin user with email `admin@modrinth.invalid` and password `admin`:
2830

29-
```bash
30-
psql --host=localhost --port=5432 -U <username, default is labrinth> -W
31+
```sh
32+
source .env
33+
psql "$DATABASE_URL" < fixtures/labrinth-seed-data-202508052143.sql
3134
```
3235

33-
The default password for the database is `labrinth`. Once you've connected, run
34-
35-
```sql
36-
INSERT INTO loaders VALUES (0, 'placeholder_loader');
37-
INSERT INTO loaders_project_types VALUES (0, 1); -- modloader id, supported type id
38-
INSERT INTO categories VALUES (0, 'placeholder_category', 1); -- category id, category, project type id
39-
```
40-
41-
This will initialize your database with a modloader called 'placeholder_loader', with id 0, and marked as supporting mods only. It will also create a category called 'placeholder_category' that is marked as supporting mods only
42-
If you would like 'placeholder_loader' to be marked as supporting modpacks too, run
43-
44-
```sql
45-
INSERT INTO loaders_project_types VALUES (0, 2); -- modloader id, supported type id
46-
```
47-
48-
If you would like 'placeholder_category' to be marked as supporting modpacks too, run
49-
50-
```sql
51-
INSERT INTO categories VALUES (0, 'placeholder_category', 2); -- modloader id, supported type id
52-
```
53-
54-
You can find more example SQL statements for seeding the database in the `apps/labrinth/tests/files/dummy_data.sql` file.
36+
You can find more example SQL statements for seeding the database in the `tests/files/dummy_data.sql` file.
5537

5638
The majority of configuration is done at runtime using [dotenvy](https://crates.io/crates/dotenvy) and the `.env` file. Each of the variables and what they do can be found in the dropdown below. Additionally, there are three command line options that can be used to specify to MeiliSearch what you want to do.
5739

58-
During development, you might notice that changes made directly to entities in the PostgreSQL database do not seem to take effect. This is often because the Redis cache still holds outdated data. To ensure your updates are reflected, clear the cache by e.g. running `redis-cli FLUSHALL`, which will force Labrinth to fetch the latest data from the database the next time it is needed.
40+
During development, you might notice that changes made directly to entities in the PostgreSQL database do not seem to take effect. This is often because the Redis cache still holds outdated data. To ensure your updates are reflected, clear the cache by e.g. running `redis-cli FLUSHALL`, which will force labrinth to fetch the latest data from the database the next time it is needed.
5941

6042
<details>
6143
<summary>.env variables & command line options</summary>
@@ -109,14 +91,13 @@ The OAuth configuration options are fairly self-explanatory. For help setting up
10991

11092
If you're prepared to contribute by submitting a pull request, ensure you have met the following criteria:
11193

112-
- `cargo fmt` has been run.
113-
- `cargo clippy` has been run.
114-
- `cargo check` has been run.
94+
- `cargo fmt --all` has been run.
95+
- `cargo clippy --all-targets` has been run.
11596
- `cargo sqlx prepare` has been run.
11697

11798
> Note: If you encounter issues with `sqlx` saying 'no queries found' after running `cargo sqlx prepare`, you may need to ensure the installed version of `sqlx-cli` matches the current version of `sqlx` used [in labrinth](https://github.com/modrinth/labrinth/blob/master/Cargo.toml).
11899
119100
[Discord]: https://discord.modrinth.com
120101
[GitHub]: https://github.com/modrinth
121-
[labrinth]: https://github.com/modrinth/labrinth
102+
[labrinth]: https://github.com/modrinth/code/tree/main/apps/labrinth
122103
[Rust]: https://www.rust-lang.org/tools/install

0 commit comments

Comments
 (0)