Added migrations with diesel

This commit is contained in:
Keanu D?lle 2020-02-04 03:34:49 +01:00
parent 99e4a704a1
commit 4e2b0fec49
26 changed files with 357 additions and 0 deletions

39
Cargo.lock generated
View File

@ -123,6 +123,27 @@ dependencies = [
"syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "diesel"
version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"diesel_derives 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"pq-sys 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "diesel_derives"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "env_logger"
version = "0.7.1"
@ -140,6 +161,7 @@ name = "errms"
version = "0.1.0"
dependencies = [
"config 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
"diesel 1.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
@ -355,6 +377,14 @@ name = "percent-encoding"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "pq-sys"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"vcpkg 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "proc-macro2"
version = "0.4.30"
@ -703,6 +733,11 @@ dependencies = [
"percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "vcpkg"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "version_check"
version = "0.1.5"
@ -774,6 +809,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum devise 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "74e04ba2d03c5fa0d954c061fc8c9c288badadffc272ebb87679a89846de3ed3"
"checksum devise_codegen 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "066ceb7928ca93a9bedc6d0e612a8a0424048b0ab1f75971b203d01420c055d7"
"checksum devise_core 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cf41c59b22b5e3ec0ea55c7847e5f358d340f3a8d6d53a5cf4f1564967f96487"
"checksum diesel 1.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "9d7cc03b910de9935007861dce440881f69102aaaedfd4bc5a6f40340ca5840c"
"checksum diesel_derives 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "45f5098f628d02a7a0f68ddba586fb61e80edec3bdc1be3b921f4ceec60858d3"
"checksum env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
"checksum hermit-abi 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "eff2656d88f158ce120947499e971d743c05dbcbed62e5bd2f38f1698bbc3772"
"checksum httparse 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9"
@ -802,6 +839,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum pear 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c26d2b92e47063ffce70d3e3b1bd097af121a9e0db07ca38a6cc1cf0cc85ff25"
"checksum pear_codegen 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "336db4a192cc7f54efeb0c4e11a9245394824cc3bcbd37ba3ff51240c35d7a6e"
"checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831"
"checksum pq-sys 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "6ac25eee5a0582f45a67e837e350d784e7003bd29a5f460796772061ca49ffda"
"checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759"
"checksum proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3acb317c6ff86a4e579dfa00fc5e6cca91ecbb4e7eb2df0468805b674eb88548"
"checksum quick-error 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
@ -846,6 +884,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
"checksum untrusted 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "55cd1f4b4e96b46aeb8d4855db4a7a9bd96eeeb5c6a1ab54593328761642ce2f"
"checksum url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a"
"checksum vcpkg 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3fc439f2794e98976c88a2a2dafce96b930fe8010b0a256b3c2199a773933168"
"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
"checksum version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce"
"checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"

View File

@ -14,3 +14,4 @@ log = "0.4.8"
env_logger = "0.7.1"
lazy_static = "1.4.0"
rocket = "0.4.2"
diesel = { version = "1.4.3", features = ["postgres"] }

0
migrations/.gitkeep Normal file
View File

View File

@ -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();

View File

@ -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;

View File

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
drop table entities;

View File

@ -0,0 +1,7 @@
-- Your SQL goes here
create table entities
(
entity_id uuid default uuid_generate_v1() not null,
constraint entities_pk
primary key (entity_id)
);

View File

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
drop table users;

View File

@ -0,0 +1,8 @@
-- Your SQL goes here
create table users
(
id uuid default uuid_generate_v1() not null,
password text,
constraint pk___users___id
primary key (id)
);

View File

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
drop table members;

View File

@ -0,0 +1,25 @@
-- Your SQL goes here
create table members
(
entity_id uuid default uuid_generate_v1() not null,
users_id uuid,
firstname text not null,
lastname text not null,
date_of_birth date,
sex smallint,
salutation text,
place_of_birth text,
academic_titles text,
personnel_number integer,
ui_language text,
constraint pk___members___id
primary key (entity_id),
constraint fk___members___users_id___users
foreign key (users_id) references users,
constraint members_entities_entity_id_fk
foreign key (entity_id) references entities
on update cascade on delete cascade
);
create unique index members_personnel_number_uindex
on members (personnel_number);

View File

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
DROP TABLE addresses;

View File

@ -0,0 +1,13 @@
-- Your SQL goes here
create table addresses
(
id uuid default uuid_generate_v1() not null,
title text,
street text not null,
number text not null,
zipcode text not null,
city text not null,
geo_location point,
constraint addresses_pk
primary key (id)
);

View File

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
drop table addresses_entities;

View File

@ -0,0 +1,14 @@
-- Your SQL goes here
create table addresses_entities
(
address_id uuid not null,
entitiy_id uuid not null,
constraint addresses_entities_pk
primary key (address_id, entitiy_id),
constraint addresses_entities_addresses_id_fk
foreign key (address_id) references addresses
on update cascade on delete cascade,
constraint addresses_entities_entities_entity_id_fk
foreign key (entitiy_id) references entities
on update cascade on delete cascade
);

View File

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
drop table buildings;

View File

@ -0,0 +1,12 @@
-- Your SQL goes here
create table buildings
(
entity_id uuid default uuid_generate_v1() not null,
name text not null,
description text,
constraint buildings_pk
primary key (entity_id),
constraint buildings_entities_entity_id_fk
foreign key (entity_id) references entities
on update cascade on delete cascade
);

View File

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
drop table vehicle_categories;

View File

@ -0,0 +1,9 @@
-- Your SQL goes here
create table vehicle_categories
(
id uuid default uuid_generate_v1() not null,
name text not null,
description text,
constraint vehicle_categories_pk
primary key (id)
);

View File

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
drop table vehicles;

View File

@ -0,0 +1,21 @@
-- Your SQL goes here
create table vehicles
(
entity_id uuid default uuid_generate_v1() not null,
identifier text not null,
numberplate text,
description text,
vehicle_category uuid default uuid_generate_v1(),
next_inspection date,
is_operational boolean default true not null,
admissible_total_weight real,
required_license uuid,
constraint vehicles_pk
primary key (entity_id),
constraint vehicles_entities_entity_id_fk
foreign key (entity_id) references entities
on update cascade on delete cascade,
constraint vehicles_vehicle_categories_id_fk
foreign key (entity_id) references vehicle_categories
on update cascade on delete set null
);

View File

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
drop table communication_types;

View File

@ -0,0 +1,8 @@
-- Your SQL goes here
create table communication_types
(
id uuid default uuid_generate_v1() not null,
name text not null,
constraint pk___communication_types___id
primary key (id)
);

View File

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
drop table communication_targets;

View File

@ -0,0 +1,18 @@
-- Your SQL goes here
create table communication_targets
(
id uuid default uuid_generate_v1() not null,
entity text not null,
entity_id uuid not null,
type uuid not null,
value text not null,
description text,
visibility boolean default false not null,
constraint pk___communication_targets___id
primary key (id),
constraint fk___communication_target___type___communication_types
foreign key (type) references communication_types,
constraint communication_targets_entities_entity_id_fk
foreign key (id) references entities
on update cascade on delete cascade
);

120
src/schema.rs Normal file
View File

@ -0,0 +1,120 @@
table! {
addresses (id) {
id -> Uuid,
title -> Nullable<Text>,
street -> Text,
number -> Text,
zipcode -> Text,
city -> Text,
geo_location -> Nullable<Point>,
}
}
table! {
addresses_entities (address_id, entitiy_id) {
address_id -> Uuid,
entitiy_id -> Uuid,
}
}
table! {
buildings (entity_id) {
entity_id -> Uuid,
name -> Text,
description -> Nullable<Text>,
}
}
table! {
communication_targets (id) {
id -> Uuid,
entity -> Text,
entity_id -> Uuid,
#[sql_name = "type"]
type_ -> Uuid,
value -> Text,
description -> Nullable<Text>,
visibility -> Bool,
}
}
table! {
communication_types (id) {
id -> Uuid,
name -> Text,
}
}
table! {
entities (entity_id) {
entity_id -> Uuid,
}
}
table! {
members (entity_id) {
entity_id -> Uuid,
users_id -> Nullable<Uuid>,
firstname -> Text,
lastname -> Text,
date_of_birth -> Nullable<Date>,
sex -> Nullable<Int2>,
salutation -> Nullable<Text>,
place_of_birth -> Nullable<Text>,
academic_titles -> Nullable<Text>,
personnel_number -> Nullable<Int4>,
ui_language -> Nullable<Text>,
}
}
table! {
users (id) {
id -> Uuid,
password -> Nullable<Text>,
}
}
table! {
vehicle_categories (id) {
id -> Uuid,
name -> Text,
description -> Nullable<Text>,
}
}
table! {
vehicles (entity_id) {
entity_id -> Uuid,
identifier -> Text,
numberplate -> Nullable<Text>,
description -> Nullable<Text>,
vehicle_category -> Nullable<Uuid>,
next_inspection -> Nullable<Date>,
is_operational -> Bool,
admissible_total_weight -> Nullable<Float4>,
required_license -> Nullable<Uuid>,
}
}
joinable!(addresses_entities -> addresses (address_id));
joinable!(addresses_entities -> entities (entitiy_id));
joinable!(buildings -> entities (entity_id));
joinable!(communication_targets -> communication_types (type));
joinable!(communication_targets -> entities (id));
joinable!(members -> entities (entity_id));
joinable!(members -> users (users_id));
joinable!(vehicles -> entities (entity_id));
joinable!(vehicles -> vehicle_categories (entity_id));
allow_tables_to_appear_in_same_query!(
addresses,
addresses_entities,
buildings,
communication_targets,
communication_types,
entities,
members,
users,
vehicle_categories,
vehicles,
);