From 6330ef0d00b294a08f851d6b410a5c05db26b81b Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Sun, 29 Dec 2019 01:11:34 +0300 Subject: [PATCH] chore: add example with diesel --- .gitignore | 2 +- Cargo.toml | 3 +- examples/diesel/.env | 1 + examples/diesel/Cargo.toml | 12 +++++ examples/diesel/README.md | 16 +++++++ examples/diesel/diesel.toml | 5 +++ examples/diesel/docker-compose.example.yml | 11 +++++ examples/diesel/migrations/.gitkeep | 0 .../down.sql | 6 +++ .../up.sql | 36 +++++++++++++++ .../2019-12-28-212542_create-posts/down.sql | 2 + .../2019-12-28-212542_create-posts/up.sql | 12 +++++ examples/diesel/src/db.rs | 9 ++++ examples/diesel/src/main.rs | 44 +++++++++++++++++++ examples/diesel/src/models.rs | 7 +++ examples/diesel/src/schema.rs | 8 ++++ 16 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 examples/diesel/.env create mode 100644 examples/diesel/Cargo.toml create mode 100644 examples/diesel/README.md create mode 100644 examples/diesel/diesel.toml create mode 100644 examples/diesel/docker-compose.example.yml create mode 100644 examples/diesel/migrations/.gitkeep create mode 100644 examples/diesel/migrations/00000000000000_diesel_initial_setup/down.sql create mode 100644 examples/diesel/migrations/00000000000000_diesel_initial_setup/up.sql create mode 100644 examples/diesel/migrations/2019-12-28-212542_create-posts/down.sql create mode 100644 examples/diesel/migrations/2019-12-28-212542_create-posts/up.sql create mode 100644 examples/diesel/src/db.rs create mode 100644 examples/diesel/src/main.rs create mode 100644 examples/diesel/src/models.rs create mode 100644 examples/diesel/src/schema.rs diff --git a/.gitignore b/.gitignore index 3472ba2..a46bd15 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ Cargo.lock itconfig/src/main.rs -.env* +/.env* diff --git a/Cargo.toml b/Cargo.toml index ff52a9d..f1e81c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [workspace] members = [ "itconfig", - "itconfig_tests" + "itconfig_tests", + "examples/diesel", ] diff --git a/examples/diesel/.env b/examples/diesel/.env new file mode 100644 index 0000000..aec4333 --- /dev/null +++ b/examples/diesel/.env @@ -0,0 +1 @@ +DATABASE_URL=postgres://user:test@localhost:5534:5432/db \ No newline at end of file diff --git a/examples/diesel/Cargo.toml b/examples/diesel/Cargo.toml new file mode 100644 index 0000000..4afa112 --- /dev/null +++ b/examples/diesel/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "diesel" +version = "0.1.0" +authors = ["Dmitriy Pleshevskiy "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +itconfig = { path = "../../itconfig" } +dotenv = "0.15.0" +diesel = { version = "1.4.3", features = ["postgres"] } \ No newline at end of file diff --git a/examples/diesel/README.md b/examples/diesel/README.md new file mode 100644 index 0000000..bf90170 --- /dev/null +++ b/examples/diesel/README.md @@ -0,0 +1,16 @@ +# Diesel + +This example shows how you can use itconfig with diesel. + + +### Usage + +```bash +cd examples/diesel + +docker-compose -p itconfig-diesel-example -f docker-compose.example.yml up -d + +diesel migration run + +cargo run +``` \ No newline at end of file diff --git a/examples/diesel/diesel.toml b/examples/diesel/diesel.toml new file mode 100644 index 0000000..92267c8 --- /dev/null +++ b/examples/diesel/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/examples/diesel/docker-compose.example.yml b/examples/diesel/docker-compose.example.yml new file mode 100644 index 0000000..a233e30 --- /dev/null +++ b/examples/diesel/docker-compose.example.yml @@ -0,0 +1,11 @@ +version: '3' + +services: + postgresql: + image: postgres:11-alpine + ports: + - 5534:5432 + environment: + POSTGRES_PASSWORD: test + POSTGRES_USER: user + POSTGRES_DB: db \ No newline at end of file diff --git a/examples/diesel/migrations/.gitkeep b/examples/diesel/migrations/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/examples/diesel/migrations/00000000000000_diesel_initial_setup/down.sql b/examples/diesel/migrations/00000000000000_diesel_initial_setup/down.sql new file mode 100644 index 0000000..a9f5260 --- /dev/null +++ b/examples/diesel/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/examples/diesel/migrations/00000000000000_diesel_initial_setup/up.sql b/examples/diesel/migrations/00000000000000_diesel_initial_setup/up.sql new file mode 100644 index 0000000..d68895b --- /dev/null +++ b/examples/diesel/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/examples/diesel/migrations/2019-12-28-212542_create-posts/down.sql b/examples/diesel/migrations/2019-12-28-212542_create-posts/down.sql new file mode 100644 index 0000000..fe33dfa --- /dev/null +++ b/examples/diesel/migrations/2019-12-28-212542_create-posts/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE posts; \ No newline at end of file diff --git a/examples/diesel/migrations/2019-12-28-212542_create-posts/up.sql b/examples/diesel/migrations/2019-12-28-212542_create-posts/up.sql new file mode 100644 index 0000000..47c229b --- /dev/null +++ b/examples/diesel/migrations/2019-12-28-212542_create-posts/up.sql @@ -0,0 +1,12 @@ +-- Your SQL goes here +CREATE TABLE posts ( + id SERIAL PRIMARY KEY, + title VARCHAR NOT NULL, + body TEXT NOT NULL, + published BOOLEAN NOT NULL DEFAULT 'f' +); + +INSERT INTO posts (title, body, published) +VALUES ('First post', 'Interesting body', 't'), + ('Second post', 'Very interesting post', 't'), + ('Draft post', 'This is post will not be shown', 'f'); \ No newline at end of file diff --git a/examples/diesel/src/db.rs b/examples/diesel/src/db.rs new file mode 100644 index 0000000..f216279 --- /dev/null +++ b/examples/diesel/src/db.rs @@ -0,0 +1,9 @@ +use super::cfg; +use diesel::prelude::*; +use diesel::pg::PgConnection; + +pub fn establish_connection() -> PgConnection { + let database_url = cfg::DATABASE_URL(); + PgConnection::establish(&database_url) + .expect(&format!("Error connecting to {}", database_url)) +} diff --git a/examples/diesel/src/main.rs b/examples/diesel/src/main.rs new file mode 100644 index 0000000..4617ac6 --- /dev/null +++ b/examples/diesel/src/main.rs @@ -0,0 +1,44 @@ +#[macro_use] +extern crate itconfig; +#[macro_use] +extern crate diesel; + +mod db; +mod models; +mod schema; + +use dotenv::dotenv; +use diesel::prelude::*; +use crate::models::*; + + +config! { + DATABASE_URL: String, +} + + +fn main() { + dotenv().ok(); + cfg::init(); + + let connection = db::establish_connection(); + let posts = get_posts(&connection); + + println!("Displaying {} posts", posts.len()); + for post in posts { + print!("\n"); + println!("{}", post.title); + println!("----------"); + println!("{}", post.body); + } +} + + +fn get_posts(connection: &PgConnection) -> Vec { + use crate::schema::posts::dsl::*; + + posts.filter(published.eq(true)) + .limit(5) + .get_results::(connection) + .expect("Error loading posts") +} \ No newline at end of file diff --git a/examples/diesel/src/models.rs b/examples/diesel/src/models.rs new file mode 100644 index 0000000..9bee09a --- /dev/null +++ b/examples/diesel/src/models.rs @@ -0,0 +1,7 @@ +#[derive(Queryable)] +pub struct Post { + pub id: i32, + pub title: String, + pub body: String, + pub published: bool, +} \ No newline at end of file diff --git a/examples/diesel/src/schema.rs b/examples/diesel/src/schema.rs new file mode 100644 index 0000000..3124ea5 --- /dev/null +++ b/examples/diesel/src/schema.rs @@ -0,0 +1,8 @@ +table! { + posts (id) { + id -> Int4, + title -> Varchar, + body -> Text, + published -> Bool, + } +}