From 8f1f1cc7fa68ccf2c6a21935910e3d57a5a14146 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Sat, 5 Jun 2021 14:39:19 +0300 Subject: [PATCH] start working on storage level --- .env | 1 + Cargo.toml | 2 ++ diesel.toml | 5 +++ migrations/.gitkeep | 0 .../down.sql | 6 ++++ .../up.sql | 36 +++++++++++++++++++ .../down.sql | 1 + .../up.sql | 6 ++++ src/lib.rs | 13 +++++++ src/queue.rs | 7 ++++ src/schema.rs | 8 +++++ 11 files changed, 85 insertions(+) create mode 100644 .env 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/2021-06-05-112912_create_fang_tasks/down.sql create mode 100644 migrations/2021-06-05-112912_create_fang_tasks/up.sql create mode 100644 src/queue.rs create mode 100644 src/schema.rs diff --git a/.env b/.env new file mode 100644 index 0000000..6d0480c --- /dev/null +++ b/.env @@ -0,0 +1 @@ +DATABASE_URL=postgres://postgres:postgres@localhost/fang diff --git a/Cargo.toml b/Cargo.toml index e6b7362..f9a6506 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,5 @@ license = "MIT" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +diesel = { version = "1.4.6", features = ["postgres"] } +dotenv = "0.15.0" 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/2021-06-05-112912_create_fang_tasks/down.sql b/migrations/2021-06-05-112912_create_fang_tasks/down.sql new file mode 100644 index 0000000..291a97c --- /dev/null +++ b/migrations/2021-06-05-112912_create_fang_tasks/down.sql @@ -0,0 +1 @@ +-- This file should undo anything in `up.sql` \ No newline at end of file diff --git a/migrations/2021-06-05-112912_create_fang_tasks/up.sql b/migrations/2021-06-05-112912_create_fang_tasks/up.sql new file mode 100644 index 0000000..8536d1f --- /dev/null +++ b/migrations/2021-06-05-112912_create_fang_tasks/up.sql @@ -0,0 +1,6 @@ +CREATE TABLE fang_tasks ( + id BIGSERIAL primary key, + metadata jsonb, + created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), + updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() +); diff --git a/src/lib.rs b/src/lib.rs index 31e1bb2..1bfd76a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,16 @@ +// pub trait JobQueue { +// type Job; +// type Error; + +// fn push(job: Self::Job) -> Result<(), Self::Error> { + +// } +// fn pop(job: Self::Job) -> Result<(), Self::Error>; +// } + +// pub trait Storage { +// fn save() -> +// } #[cfg(test)] mod tests { #[test] diff --git a/src/queue.rs b/src/queue.rs new file mode 100644 index 0000000..fbb80f9 --- /dev/null +++ b/src/queue.rs @@ -0,0 +1,7 @@ +pub trait JobQueue { + type Job; + type Error; + + fn push(job: Self::Job) -> Result<(), Self::Error>; + fn pop(job: Self::Job) -> Result<(), Self::Error>; +} diff --git a/src/schema.rs b/src/schema.rs new file mode 100644 index 0000000..2749e58 --- /dev/null +++ b/src/schema.rs @@ -0,0 +1,8 @@ +table! { + fang_tasks (id) { + id -> Int8, + metadata -> Nullable, + created_at -> Timestamptz, + updated_at -> Timestamptz, + } +}