diff --git a/.env b/.env index 880c44c..6e2ca99 100644 --- a/.env +++ b/.env @@ -1 +1,2 @@ -DATABASE_URL=postgres://fej:fej@fej_db:5432/fej +# This file is read by diesel +DATABASE_URL=postgres://fej:fej@localhost:5432/fej diff --git a/.env.container b/.env.container new file mode 100644 index 0000000..8b395fb --- /dev/null +++ b/.env.container @@ -0,0 +1,2 @@ +# This file is read by the container, and therefore by fej +DATABASE_URL=postgres://fej:fej@fej_db:5432/fej diff --git a/Cargo.lock b/Cargo.lock index 8ab2a9c..0006402 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -321,6 +321,29 @@ dependencies = [ "syn 0.15.44", ] +[[package]] +name = "diesel" +version = "1.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "047bfc4d5c3bd2ef6ca6f981941046113524b9a9f9a7cbdfdd7ff40f58e6f542" +dependencies = [ + "bitflags", + "byteorder", + "diesel_derives", + "pq-sys", +] + +[[package]] +name = "diesel_derives" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45f5098f628d02a7a0f68ddba586fb61e80edec3bdc1be3b921f4ceec60858d3" +dependencies = [ + "proc-macro2 1.0.26", + "quote 1.0.9", + "syn 1.0.69", +] + [[package]] name = "digest" version = "0.9.0" @@ -351,6 +374,7 @@ version = "1.0.1" dependencies = [ "chrono", "chrono-tz", + "diesel", "regex", "reqwest", "rocket", @@ -1119,6 +1143,15 @@ version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" +[[package]] +name = "pq-sys" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ac25eee5a0582f45a67e837e350d784e7003bd29a5f460796772061ca49ffda" +dependencies = [ + "vcpkg", +] + [[package]] name = "proc-macro-hack" version = "0.5.19" diff --git a/Cargo.toml b/Cargo.toml index 83623fa..f359a51 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,11 +25,8 @@ serde = "1.0.124" chrono = "0.4.19" chrono-tz = "0.5.3" regex = "1.4.5" - -[dependencies.reqwest] -version = "0.11.2" -default-features = true -features = ["blocking", "json", "cookies"] +reqwest = { version = "0.11.2", features = ["blocking", "json", "cookies"] } +diesel = { version = "1.4.6", features = ["postgres"] } [dependencies.rocket_contrib] version = "0.4.7" diff --git a/build b/build index 962d14c..271a3d9 100755 --- a/build +++ b/build @@ -81,6 +81,7 @@ elif [[ "$action" = run ]]; then --detach \ --name fej_db \ --network fej \ + -p 5432:5432 \ -e "POSTGRES_DB=fej" \ -e "POSTGRES_USER=fej" \ -e "POSTGRES_PASSWORD=fej" \ @@ -101,7 +102,7 @@ elif [[ "$action" = run ]]; then --tty \ --publish 8000:8000 \ --name fej \ - --env-file .env \ + --env-file .env.container \ --network fej \ "$image$([[ "$mode" != "rel" ]] && echo "-dev"):$tags" "$@" fi diff --git a/diesel.toml b/diesel.toml new file mode 100644 index 0000000..92267c8 --- /dev/null +++ b/diesel.toml @@ -0,0 +1,5 @@ +# For documentation on how to configure this file, +# see diesel.rs/guides/configuring-diesel-cli + +[print_schema] +file = "src/schema.rs" diff --git a/migrations/.gitkeep b/migrations/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/migrations/00000000000000_diesel_initial_setup/down.sql b/migrations/00000000000000_diesel_initial_setup/down.sql new file mode 100644 index 0000000..a9f5260 --- /dev/null +++ b/migrations/00000000000000_diesel_initial_setup/down.sql @@ -0,0 +1,6 @@ +-- This file was automatically created by Diesel to setup helper functions +-- and other internal bookkeeping. This file is safe to edit, any future +-- changes will be added to existing projects as new migrations. + +DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); +DROP FUNCTION IF EXISTS diesel_set_updated_at(); diff --git a/migrations/00000000000000_diesel_initial_setup/up.sql b/migrations/00000000000000_diesel_initial_setup/up.sql new file mode 100644 index 0000000..d68895b --- /dev/null +++ b/migrations/00000000000000_diesel_initial_setup/up.sql @@ -0,0 +1,36 @@ +-- This file was automatically created by Diesel to setup helper functions +-- and other internal bookkeeping. This file is safe to edit, any future +-- changes will be added to existing projects as new migrations. + + + + +-- Sets up a trigger for the given table to automatically set a column called +-- `updated_at` whenever the row is modified (unless `updated_at` was included +-- in the modified columns) +-- +-- # Example +-- +-- ```sql +-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW()); +-- +-- SELECT diesel_manage_updated_at('users'); +-- ``` +CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$ +BEGIN + EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s + FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl); +END; +$$ LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$ +BEGIN + IF ( + NEW IS DISTINCT FROM OLD AND + NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at + ) THEN + NEW.updated_at := current_timestamp; + END IF; + RETURN NEW; +END; +$$ LANGUAGE plpgsql;