From f6799d701580e7f1ec51ac70a3b5bfc1fd283ef4 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 22 Oct 2021 20:32:40 +0200 Subject: Setup diesel for database backend implementation Signed-off-by: Matthias Beyer --- service-person/.gitignore | 1 + service-person/Cargo.toml | 1 + service-person/diesel.toml | 5 +++ service-person/migrations/.gitkeep | 0 .../00000000000000_diesel_initial_setup/down.sql | 6 ++++ .../00000000000000_diesel_initial_setup/up.sql | 36 ++++++++++++++++++++++ .../down.sql | 5 +++ .../2021-10-22-182006_create_initial_tables/up.sql | 34 ++++++++++++++++++++ 8 files changed, 88 insertions(+) create mode 100644 service-person/.gitignore create mode 100644 service-person/diesel.toml create mode 100644 service-person/migrations/.gitkeep create mode 100644 service-person/migrations/00000000000000_diesel_initial_setup/down.sql create mode 100644 service-person/migrations/00000000000000_diesel_initial_setup/up.sql create mode 100644 service-person/migrations/2021-10-22-182006_create_initial_tables/down.sql create mode 100644 service-person/migrations/2021-10-22-182006_create_initial_tables/up.sql diff --git a/service-person/.gitignore b/service-person/.gitignore new file mode 100644 index 0000000..0bd66fb --- /dev/null +++ b/service-person/.gitignore @@ -0,0 +1 @@ +src/schema.rs diff --git a/service-person/Cargo.toml b/service-person/Cargo.toml index e38b505..c78dfb8 100644 --- a/service-person/Cargo.toml +++ b/service-person/Cargo.toml @@ -9,3 +9,4 @@ env_logger = "0.8" actix-web = "3.3" anyhow = "1" serde = "1" +diesel = { version = "1.4.4", features = ["postgres"] } diff --git a/service-person/diesel.toml b/service-person/diesel.toml new file mode 100644 index 0000000..92267c8 --- /dev/null +++ b/service-person/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/service-person/migrations/.gitkeep b/service-person/migrations/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/service-person/migrations/00000000000000_diesel_initial_setup/down.sql b/service-person/migrations/00000000000000_diesel_initial_setup/down.sql new file mode 100644 index 0000000..a9f5260 --- /dev/null +++ b/service-person/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/service-person/migrations/00000000000000_diesel_initial_setup/up.sql b/service-person/migrations/00000000000000_diesel_initial_setup/up.sql new file mode 100644 index 0000000..d68895b --- /dev/null +++ b/service-person/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; diff --git a/service-person/migrations/2021-10-22-182006_create_initial_tables/down.sql b/service-person/migrations/2021-10-22-182006_create_initial_tables/down.sql new file mode 100644 index 0000000..d57d59a --- /dev/null +++ b/service-person/migrations/2021-10-22-182006_create_initial_tables/down.sql @@ -0,0 +1,5 @@ +-- This file should undo anything in `up.sql` +DROP TABLE persons; +DROP TABLE address; +DROP TABLE cities; +DROP TABLE countries; diff --git a/service-person/migrations/2021-10-22-182006_create_initial_tables/up.sql b/service-person/migrations/2021-10-22-182006_create_initial_tables/up.sql new file mode 100644 index 0000000..24a214f --- /dev/null +++ b/service-person/migrations/2021-10-22-182006_create_initial_tables/up.sql @@ -0,0 +1,34 @@ +-- Your SQL goes here + +CREATE TABLE countries ( + id SERIAL PRIMARY KEY NOT NULL, + name VARCHAR NOT NULL +); + +CREATE TABLE cities ( + id SERIAL PRIMARY KEY NOT NULL, + name VARCHAR NOT NULL +); + +CREATE TABLE streets ( + id SERIAL PRIMARY KEY NOT NULL, + name VARCHAR NOT NULL +); + +CREATE TABLE address ( + id SERIAL PRIMARY KEY NOT NULL, + + country_id INTEGER REFERENCES countries(id) NOT NULL, + city_id INTEGER REFERENCES cities(id) NOT NULL, + street_id INTEGER REFERENCES streets(id) NOT NULL, + + addr_number INTEGER NOT NULL +); + +CREATE TABLE persons ( + id SERIAL PRIMARY KEY NOT NULL, + name VARCHAR NOT NULL, + age INTEGER NOT NULL, + + address_id INTEGER REFERENCES address(id) NOT NULL +); -- cgit v1.2.3