Skip to content
This repository was archived by the owner on Apr 24, 2024. It is now read-only.

Commit 382fe95

Browse files
horensochr-schr
andcommitted
Add database_documentation and improve schema patch doc.
Co-authored-by: chr-schr <[email protected]>
1 parent 3baff5f commit 382fe95

File tree

7 files changed

+72
-23
lines changed

7 files changed

+72
-23
lines changed

backend/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ reset-database: ## Reset diesel database
3131
.PHONY: install
3232
install: ## Install dependencies within the source repo
3333
rustup component add rustfmt clippy
34-
cargo install diesel_cli@2.0.1 --no-default-features --features postgres && cargo install typeshare-cli
34+
cargo install diesel_cli@2.1.1 --no-default-features --features postgres && cargo install typeshare-cli
3535

3636
.PHONY: uninstall
3737
uninstall: ## Uninstall and clean everything in the backend
3838
-cargo clean
3939
-rustup component remove rustfmt clippy
40-
-cargo uninstall diesel_cli@2.0.1 && cargo install typeshare-cli
40+
-cargo uninstall diesel_cli@2.1.1 && cargo install typeshare-cli
4141
-rm -rf target/*

backend/diesel.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
[print_schema]
22
file = "src/schema.rs"
33
patch_file = "src/schema.patch"
4+
with_docs = "only-database-comments"
45

56
filter = { except_tables = ["spatial_ref_sys"] }
6-
import_types = ["postgis_diesel::sql_types::Geography", "diesel::sql_types::*"]
7+
import_types = [
8+
"postgis_diesel::sql_types::Geography",
9+
"postgis_diesel::sql_types::Geometry",
10+
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
COMMENT ON DATABASE permaplant IS NULL;
2+
3+
--
4+
-- Don't alter the database schema in this file.
5+
--
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--
2+
-- Only add database comments in this migration.
3+
--
4+
5+
COMMENT ON TABLE layers IS 'The layers of a map';
6+
7+
COMMENT ON COLUMN layers.name IS 'The name of the layer
8+
Example: "Base Layer"';
9+
10+
COMMENT ON COLUMN layers.type IS 'The type of the layer (ENUM layer_type)
11+
Examples: base, plants, shade, ...';
12+
13+
COMMENT ON COLUMN layers.type IS 'Whether the layer was created by the user as an alternative to a pre-existing layer';
14+
15+
16+
17+
18+
19+
COMMENT ON COLUMN plantings.id IS 'This is generated by the frontend';

backend/src/schema.patch

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,32 @@
1-
diff --git a/backend/src/schema.rs b/backend/src/schema.rs 2023-07-20
2-
index 54f26f46..68427977 100644
3-
--- a/backend/src/schema.rs
4-
+++ b/backend/src/schema.rs
5-
@@ -10,20 +10,12 @@ pub mod sql_types {
6-
pub struct ExternalSource;
1+
--- src/schema.rs 2024-02-19 18:13:43.582300382 +0000
2+
+++ src/schema_tmp.rs 2024-02-19 18:12:36.849610756 +0000
3+
@@ -15,20 +15,12 @@
74

85
#[derive(diesel::sql_types::SqlType)]
96
#[diesel(postgres_type(name = "fertility"))]
107
pub struct Fertility;
118

12-
- #[derive(diesel::sql_types::SqlType)]
9+
#[derive(diesel::sql_types::SqlType)]
1310
- #[diesel(postgres_type(name = "geography"))]
1411
- pub struct Geography;
1512
-
1613
- #[derive(diesel::sql_types::SqlType)]
1714
- #[diesel(postgres_type(name = "geometry"))]
1815
- pub struct Geometry;
1916
-
20-
#[derive(diesel::sql_types::SqlType)]
17+
- #[derive(diesel::sql_types::SqlType)]
2118
#[diesel(postgres_type(name = "growth_rate"))]
2219
pub struct GrowthRate;
2320

2421
#[derive(diesel::sql_types::SqlType)]
2522
#[diesel(postgres_type(name = "herbaceous_or_woody"))]
26-
@@ -100,16 +92,15 @@ diesel::table! {
27-
is_alternative -> Bool,
28-
}
23+
pub struct HerbaceousOrWoody;
24+
@@ -162,14 +154,12 @@
2925
}
3026

3127
diesel::table! {
3228
use postgis_diesel::sql_types::Geography;
33-
+ use postgis_diesel::sql_types::Geometry;
34-
use diesel::sql_types::*;
29+
use postgis_diesel::sql_types::Geometry;
3530
use super::sql_types::PrivacyOption;
3631
- use super::sql_types::Geography;
3732
- use super::sql_types::Geometry;

doc/backend/06updating_schema_patch.md

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
1-
# Update the `schema.patch` file
1+
# Why do we need the `schema.patch` file
22

3-
This document explains how to update the `schema.patch` file used by diesel.
3+
When executing `diesel migration run` diesel connects to the database and
4+
expresses the current schema of the database in the `schema.rs` file. The
5+
config for this mechanism is in `diesel.toml`. You can use `diesel print-schema`
6+
to output the schema manually. Unfortunately Diesel generates type definitions
7+
for types we import from Postgis. This results in a double use:
48

5-
1. Remove the following line from `diesel.toml`:
9+
```
10+
diesel::table! {
11+
use postgis_diesel::sql_types::Geography; // <-- even though we import using --import-types
12+
use super::sql_types::Geography; // <-- this gets generated by Diesel
13+
```
14+
15+
After generating the `schema.rs` file Diesel applies the patch file to it.
16+
17+
# When to update `schema.patch` file
18+
19+
Whenever you get an error message similar to `error applying hunk #2` you have to update the `schema.patch` file.
20+
This happens when `schema.rs` changes in the diff context of the patch file (the lines before and after the change).
21+
22+
# How to update the `schema.patch` file
23+
24+
1. Comment out the following line from `diesel.toml`:
625

726
```toml
827
patch_file = "src/schema.patch"
@@ -17,7 +36,13 @@ This document explains how to update the `schema.patch` file used by diesel.
1736
You should now have a generated `schema.rs` in the backend src folder.
1837

1938
3. Copy the `schema.rs` file e.g. to `schema_tmp.rs`.
20-
4. Run `` diff src/schema.rs `src/schema_tmp.rs` -U6 `` in the backend folder and save the result to the `src/schema.patch` file.
21-
5. Add `patch_file = "src/schema.patch"` to the `diesel.toml` again.
2239

23-
From now on the newly generated patch file should be used by diesel.
40+
4. Make the necessary changes to `schema_tmp.rs`.
41+
42+
5. In the backend directory run `diff -U6 src/schema.rs src/schema_tmp.rs > src/schema.patch`.
43+
44+
6. Add `patch_file = "src/schema.patch"` to the `diesel.toml` again.
45+
46+
7. Delete `schema_tmp.rs`.
47+
48+
From now on the newly generated patch file should be used by Diesel.

doc/changelog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ Syntax: `- short text describing the change _(Your Name)_`
1010

1111
- needs new migrations
1212
- needs new scraper data (integer for plant spread and height)
13-
- _()_
13+
- Add database documentation. _(Christoph @chr_schr, Jannis @horenso)_
1414
- Remove variety from table seeds _(Jannis Adamek)_
1515
- Refactor react query error handling _(Daniel Steinkogler)_
1616
- Add notes to plantings in backend #1156 _(Jannis Adamek)_
1717
- Fix seed routes and add naming convention guideline _(Daniel Steinkogler)_
1818
- Enable automatic image scaling in base layer _(Moritz)_
1919
- _()_
20+
2021
- Add [guideline](guidelines/frontend-keyhandling.md) and [decisions](decisions/frontend_keyhandling.md) for key handling) _(Daniel Steinkogler)_
2122
- _()_
2223
- Fix ref warning in PlantLabel, center labels as intended _(Paul)_

0 commit comments

Comments
 (0)