Skip to content

Commit

Permalink
upgrade diesel
Browse files Browse the repository at this point in the history
  • Loading branch information
ramsayleung committed Aug 5, 2018
1 parent 111a345 commit dd9ee56
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 15 deletions.
10 changes: 3 additions & 7 deletions Cargo.toml
Expand Up @@ -7,8 +7,8 @@ version = "0.1.1"
dotenv = "0.9.0"
error-chain = "0.10.0"
lazy_static = "0.2.8"
r2d2 = "0.7.3"
r2d2-diesel = "0.14.0"
r2d2 = "0.8.2"
r2d2-diesel = "1.0.0"
rocket = "0.3.6"
rocket_codegen = "0.3.6"
serde = "1.0"
Expand All @@ -26,11 +26,7 @@ version = "0.4"

[dependencies.diesel]
features = ["postgres", "chrono","network-address"]
version = "0.14.0"

[dependencies.diesel_codegen]
features = ["postgres"]
version = "0.14.0"
version = "1.3.2"

[dependencies.rocket_contrib]
default-features = false
Expand Down
5 changes: 5 additions & 0 deletions 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/dal/schema.rs"
6 changes: 6 additions & 0 deletions 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();
36 changes: 36 additions & 0 deletions 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;
6 changes: 3 additions & 3 deletions src/dal/diesel_pool.rs
Expand Up @@ -9,7 +9,7 @@ use dotenv::dotenv;

// DB item
use diesel::pg::PgConnection;
use r2d2::{Pool, Config, PooledConnection, GetTimeout};
use r2d2::{Pool, PooledConnection};
use r2d2_diesel::ConnectionManager;

// Std Imports
Expand All @@ -24,9 +24,9 @@ pub fn create_db_pool() -> Pool<ConnectionManager<PgConnection>> {
dotenv().ok();

let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let config = Config::default();
// let config = Config::default();
let manager = ConnectionManager::<PgConnection>::new(database_url);
Pool::new(config, manager).expect("Failed to create pool.")
Pool::new( manager).expect("Failed to create pool.")
}

// DB Items
Expand Down
4 changes: 2 additions & 2 deletions src/dal/models/post.rs
Expand Up @@ -124,8 +124,8 @@ pub struct NewPost {
impl NewPost {
pub fn insert(new_post: &NewPost, conn: &PgConnection) -> bool {
// use dal::schema::post;
diesel::insert(new_post)
.into(post::table)
diesel::insert_into(post)
.values(new_post)
.execute(conn)
.is_ok()
}
Expand Down
43 changes: 42 additions & 1 deletion src/dal/schema.rs
@@ -1 +1,42 @@
infer_schema!("dotenv:DATABASE_URL");
table! {
post (id) {
id -> Int4,
title -> Text,
subtitle -> Text,
raw_content -> Text,
rendered_content -> Text,
create_time -> Timestamp,
modify_time -> Timestamp,
post_type -> Int4,
hit_time -> Int4,
published -> Bool,
slug_url -> Varchar,
}
}

table! {
user (id) {
id -> Int4,
username -> Varchar,
hashed_password -> Varchar,
create_time -> Timestamp,
modify_time -> Timestamp,
email -> Varchar,
avatar_url -> Nullable<Varchar>,
}
}

table! {
visitor_log (id) {
id -> Int4,
ip -> Inet,
access_time -> Timestamp,
user_id -> Int4,
}
}

allow_tables_to_appear_in_same_query!(
post,
user,
visitor_log,
);
4 changes: 2 additions & 2 deletions src/main.rs
Expand Up @@ -13,8 +13,8 @@ extern crate serde_json;
#[macro_use]
extern crate serde_derive;

#[macro_use]
extern crate diesel_codegen;
// #[macro_use]
// extern crate diesel_codegen;
#[macro_use]
extern crate diesel;
extern crate dotenv;
Expand Down

0 comments on commit dd9ee56

Please sign in to comment.