Skip to content

Commit

Permalink
Example of using CockroachDB with diesel
Browse files Browse the repository at this point in the history
This is almost verbatim the example from

https://github.com/diesel-rs/diesel/tree/3ae353c3aff8a7ea64ed6cb39b3a045ac86cd60e/examples/postgres

with minor adjustments:

1. after `diesel setup`, edit the created `up.sql` and `down.sql` to contain
   only a trivial statement (like `SELECT 1`). This is necessary because by
   default they contain a [random trigger] that CockroachDB can't handle.
2. `schema.rs` was manually spelled out using the `table!` macro. The tutorial
   uses `infer_schema!` for which we don't have all the [internals]. Note that
   we use `BigInt` to properly support CockroachDB's `SERIAL` type. In turn,
   `struct Post` has `id: i64` instead of `i32`.
3. Some adjustments in `show_posts` to list the ID which is otherwise impossible
   to guess.
4. Pinned the diesel dependency for no good reason (to hopefully have this go
   stale later).

There are likely more problems not discovered by this toy example. See the
[tracking issue].

[random trigger]: https://github.com/diesel-rs/diesel/blob/master/diesel_cli/src/setup_sql/postgres/initial_setup/up.sql
[internals]: cockroachdb/cockroach#8675
[tracking issue]: cockroachdb/cockroach#13787
  • Loading branch information
tbg committed Oct 8, 2017
0 parents commit df347a5
Show file tree
Hide file tree
Showing 16 changed files with 239 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE_URL=postgresql://root@localhost:26257/diesel?application_name=cockroach&sslmode=disable
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
Cargo.lock
16 changes: 16 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
authors = ["Tobias Schottdorf <[email protected]>"]
name = "dieselroach"
version = "0.1.0"

[dependencies]
dotenv = "*"

[dependencies.diesel_codegen]
features = ["postgres"]
version = "*"

[dependencies.diesel]
features = ["postgres"]
git = "https://github.com/diesel-rs/diesel"
rev = "0ce9179facd7defaef70b3b3563ba1c69dd27d48"
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Example of using CockroachDB with diesel

This is almost verbatim the example from

https://github.com/diesel-rs/diesel/tree/3ae353c3aff8a7ea64ed6cb39b3a045ac86cd60e/examples/postgres

with minor adjustments:

1. after `diesel setup`, edit the created `up.sql` and `down.sql` to contain
only a trivial statement (like `SELECT 1`). This is necessary because by
default they contain a [random trigger] that CockroachDB can't handle.
2. `schema.rs` was manually spelled out using the `table!` macro. The tutorial
uses `infer_schema!` for which we don't have all the [internals]. Note that
we use `BigInt` to properly support CockroachDB's `SERIAL` type. In turn,
`struct Post` has `id: i64` instead of `i32`.
3. Some adjustments in `show_posts` to list the ID which is otherwise impossible
to guess.
4. Pinned the diesel dependency for no good reason (to hopefully have this go
stale later).

There are likely more problems not discovered by this toy example. See the
[tracking issue].

[random trigger]: https://github.com/diesel-rs/diesel/blob/master/diesel_cli/src/setup_sql/postgres/initial_setup/up.sql
[internals]: https://github.com/cockroachdb/cockroach/issues/8675
[tracking issue]: https://github.com/cockroachdb/cockroach/issues/13787
Empty file added migrations/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions migrations/00000000000000_diesel_initial_setup/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT 1;
37 changes: 37 additions & 0 deletions migrations/00000000000000_diesel_initial_setup/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-- 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;
SELECT 1;
1 change: 1 addition & 0 deletions migrations/2017-10-08-005753_trivialtest/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE posts;
7 changes: 7 additions & 0 deletions migrations/2017-10-08-005753_trivialtest/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- 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'
);
20 changes: 20 additions & 0 deletions src/bin/delete_post.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
extern crate dieselroach;
extern crate diesel;

use self::diesel::prelude::*;
use self::dieselroach::*;
use std::env::args;

fn main() {
use dieselroach::schema::posts::dsl::*;

let target = args().nth(1).expect("Expected a target to match against");
let pattern = format!("%{}%", target);

let connection = establish_connection();
let num_deleted = diesel::delete(posts.filter(title.like(pattern)))
.execute(&connection)
.expect("Error deleting posts");

println!("Deleted {} posts", num_deleted);
}
21 changes: 21 additions & 0 deletions src/bin/publish_post.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
extern crate dieselroach;
extern crate diesel;

use self::diesel::prelude::*;
use self::dieselroach::*;
use self::dieselroach::models::Post;
use std::env::args;

fn main() {
use dieselroach::schema::posts::dsl::{posts, published};

let id = args().nth(1).expect("publish_post requires a post id")
.parse::<i64>().expect("Invalid ID");
let connection = establish_connection();

let post = diesel::update(posts.find(id))
.set(published.eq(true))
.get_result::<Post>(&connection)
.expect(&format!("Unable to find post {}", id));
println!("Published post {}", post.title);
}
21 changes: 21 additions & 0 deletions src/bin/show_posts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
extern crate dieselroach;
extern crate diesel;

use self::dieselroach::*;
use self::dieselroach::models::*;
use self::diesel::prelude::*;

fn main() {
use dieselroach::schema::posts::dsl::*;

let connection = establish_connection();
let results = posts.load::<Post>(&connection)
.expect("Error loading posts");

println!("Displaying {} posts", results.len());
for post in results {
println!("{}: {}{}", post.id, post.title, if post.published { "" } else { " (unpublished)" });
println!("----------\n");
println!("{}", post.body);
}
}
26 changes: 26 additions & 0 deletions src/bin/write_post.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
extern crate dieselroach;
extern crate diesel;

use self::dieselroach::*;
use std::io::{stdin, Read};

fn main() {
let connection = establish_connection();

println!("What would you like your title to be?");
let mut title = String::new();
stdin().read_line(&mut title).unwrap();
let title = &title[..(title.len() - 1)]; // Drop the newline character
println!("\nOk! Let's write {} (Press {} when finished)\n", title, EOF);
let mut body = String::new();
stdin().read_to_string(&mut body).unwrap();

let post = create_post(&connection, title, &body);
println!("\nSaved draft {} with id {}", title, post.id);
}

#[cfg(not(windows))]
const EOF: &'static str = "CTRL+D";

#[cfg(windows)]
const EOF: &'static str = "CTRL+Z";
36 changes: 36 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#[macro_use] extern crate diesel_codegen;
pub mod schema;
pub mod models;
#[macro_use] extern crate diesel;
extern crate dotenv;

use diesel::prelude::*;
use diesel::pg::PgConnection;
use dotenv::dotenv;
use std::env;

use self::models::{Post, NewPost};

pub fn establish_connection() -> PgConnection {
dotenv().ok();

let database_url = env::var("DATABASE_URL")
.expect("DATABASE_URL must be set");
PgConnection::establish(&database_url)
.expect(&format!("Error connecting to {}", database_url))
}


pub fn create_post<'a>(conn: &PgConnection, title: &'a str, body: &'a str) -> Post {
use schema::posts;

let new_post = NewPost {
title: title,
body: body,
};

diesel::insert_into(posts::table)
.values(&new_post)
.get_result(conn)
.expect("Error saving new post")
}
16 changes: 16 additions & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#[derive(Queryable)]
pub struct Post {
pub id: i64,
pub title: String,
pub body: String,
pub published: bool,
}

use super::schema::posts;

#[derive(Insertable)]
#[table_name="posts"]
pub struct NewPost<'a> {
pub title: &'a str,
pub body: &'a str,
}
8 changes: 8 additions & 0 deletions src/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
table! {
posts {
id -> BigInt,
title -> VarChar,
body -> Text,
published -> Bool,
}
}

0 comments on commit df347a5

Please sign in to comment.