From d60e5eb9207d010b83721001d9330056320df3c4 Mon Sep 17 00:00:00 2001 From: Jake Hillion Date: Thu, 21 Jul 2022 12:55:31 +0100 Subject: [PATCH] added users table --- .gitignore | 2 + Cargo.lock | 45 +++++++++++++++++++ Cargo.toml | 1 + diesel.toml | 5 +++ migrations/.gitkeep | 0 .../down.sql | 6 +++ .../up.sql | 36 +++++++++++++++ .../2022-07-21-115312_create_users/down.sql | 3 ++ .../2022-07-21-115312_create_users/up.sql | 6 +++ src/schema.rs | 6 +++ 10 files changed, 110 insertions(+) create mode 100644 diesel.toml create mode 100644 migrations/.gitkeep create mode 100644 migrations/00000000000000_diesel_initial_setup/down.sql create mode 100644 migrations/00000000000000_diesel_initial_setup/up.sql create mode 100644 migrations/2022-07-21-115312_create_users/down.sql create mode 100644 migrations/2022-07-21-115312_create_users/up.sql create mode 100644 src/schema.rs diff --git a/.gitignore b/.gitignore index ea8c4bf..3d62c7b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /target +.env + diff --git a/Cargo.lock b/Cargo.lock index 004ffa2..ae1526c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -374,6 +374,12 @@ version = "3.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37ccbd214614c6783386c1af30caf03192f17891059cecc394b4fb119e363de3" +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + [[package]] name = "cache-padded" version = "1.2.0" @@ -523,6 +529,29 @@ dependencies = [ "cipher", ] +[[package]] +name = "diesel" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b28135ecf6b7d446b43e27e225622a038cc4e2930a1022f51cdb97ada19b8e4d" +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", + "quote", + "syn", +] + [[package]] name = "digest" version = "0.9.0" @@ -859,6 +888,7 @@ name = "inventory-system" version = "0.1.0" dependencies = [ "async-std", + "diesel", "env_logger", "log", "rust-embed", @@ -1045,6 +1075,15 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" +[[package]] +name = "pq-sys" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b845d6d8ec554f972a2c5298aad68953fd64e7441e846075450b44656a016d1" +dependencies = [ + "vcpkg", +] + [[package]] name = "proc-macro-hack" version = "0.5.19" @@ -1668,6 +1707,12 @@ dependencies = [ "version_check", ] +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index b53e48e..a502a92 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,5 +12,6 @@ env_logger = "0.9" async-std = "1" tide = "0.16.0" +diesel = { version = "1.4", features = ["postgres"] } rust-embed = { version = "6.2.0", features = ["interpolate-folder-path"]} 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; diff --git a/migrations/2022-07-21-115312_create_users/down.sql b/migrations/2022-07-21-115312_create_users/down.sql new file mode 100644 index 0000000..f9bd3a3 --- /dev/null +++ b/migrations/2022-07-21-115312_create_users/down.sql @@ -0,0 +1,3 @@ +-- This file should undo anything in `up.sql` + +DROP TABLE IF EXISTS users; diff --git a/migrations/2022-07-21-115312_create_users/up.sql b/migrations/2022-07-21-115312_create_users/up.sql new file mode 100644 index 0000000..05aef01 --- /dev/null +++ b/migrations/2022-07-21-115312_create_users/up.sql @@ -0,0 +1,6 @@ +-- Your SQL goes here + +CREATE TABLE users ( + id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(), + nickname VARCHAR NOT NULL +); diff --git a/src/schema.rs b/src/schema.rs new file mode 100644 index 0000000..ba98e14 --- /dev/null +++ b/src/schema.rs @@ -0,0 +1,6 @@ +table! { + users (id) { + id -> Uuid, + nickname -> Varchar, + } +}