From 2b4d62b086802ece28f1288bd09cbf5f252c45d7 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Tue, 15 May 2018 18:12:32 -0700 Subject: progress on database lookup --- rust/Cargo.lock | 3 +- rust/Cargo.toml | 5 +- rust/src/api_server.rs | 59 ++++++++---- rust/src/database_models.rs | 222 ++++++++++++++++++++++++++++++++++++++++++++ rust/src/lib.rs | 6 +- 5 files changed, 274 insertions(+), 21 deletions(-) create mode 100644 rust/src/database_models.rs (limited to 'rust') diff --git a/rust/Cargo.lock b/rust/Cargo.lock index b9b52098..8ac2eb79 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -212,7 +212,7 @@ dependencies = [ "num-traits 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "pq-sys 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 0.8.6 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", "uuid 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -339,6 +339,7 @@ dependencies = [ "slog-async 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "slog-term 2.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "swagger 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "uuid 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 9883a18b..e4963b67 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -8,11 +8,12 @@ members = ["fatcat-api"] [dependencies] fatcat-api = {version = "*", path = "fatcat-api"} -diesel = { version = "1.2", features = ["postgres", "extras"] } +diesel = { version = "1.2", features = ["postgres", "extras", "uuid"] } dotenv = "0.9.0" clap = "*" error-chain = "0.11" -r2d2 = "*" +r2d2 = "0.8" +uuid = "0.5" # API server chrono = { version = "0.4", features = ["serde"] } diff --git a/rust/src/api_server.rs b/rust/src/api_server.rs index d9273186..aa13023d 100644 --- a/rust/src/api_server.rs +++ b/rust/src/api_server.rs @@ -1,13 +1,16 @@ //! API endpoint handlers use ConnectionPool; -use database_schema::container_rev::table as container_rev; +use database_models::*; +use database_schema::{container_rev, container_ident, container_edit, + creator_rev, creator_ident, creator_edit, + file_rev, file_ident, file_edit, + release_rev, release_ident, release_edit, + work_rev, work_ident, work_edit, +}; +use uuid; use diesel::prelude::*; use futures::{self, Future}; -//use database_schema::creator_rev::table as creator_rev; -//use database_schema::file_rev::table as file_rev; -//use database_schema::release_rev::table as release_rev; -//use database_schema::work_rev::table as work_rev; use fatcat_api::models; use fatcat_api::models::*; use fatcat_api::{Api, ApiError, ContainerIdGetResponse, ContainerLookupGetResponse, @@ -26,20 +29,26 @@ pub struct Server { impl Api for Server { fn container_id_get( &self, - _id: String, + id: String, _context: &Context, ) -> Box + Send> { let conn = self.db_pool.get().expect("db_pool error"); - let c: i64 = container_rev.count().first(&*conn).expect("DB Error"); - println!("container count: {}", c); + let id = uuid::Uuid::parse_str(&id).unwrap(); + let c: ContainerIdentRow = container_ident::table + .find(id) + .first(&conn) + .expect("error loading container"); + //let c: i64 = container_rev::table.count().first(&conn).expect("DB Error"); + println!("container count: {:?}", c); + let ce = ContainerEntity { issn: None, publisher: Some("Hello!".into()), parent: None, name: None, state: None, - ident: None, - revision: None, + ident: Some(c.id.to_string()), + revision: c.rev_id.map(|v| v as isize), redirect: None, editgroup: None, }; @@ -81,13 +90,29 @@ impl Api for Server { id: String, context: &Context, ) -> Box + Send> { - let context = context.clone(); - println!( - "creator_id_get(\"{}\") - X-Span-ID: {:?}", - id, - context.x_span_id.unwrap_or(String::from("")).clone() - ); - Box::new(futures::failed("Generic failure".into())) + let conn = self.db_pool.get().expect("db_pool error"); + /* + let first_thing: (Uuid, bool, Option, Option) = creator_ident::table + .first(&conn) + .unwrap(); + let entity_table = creator_ident::table.left_join(creator_rev::table); + let thing: (creator_ident::SqlType, creator_rev::SqlType) = creator_ident::table + .inner_join(creator_rev::table) + .first(&conn) + .expect("Error loading creator"); + */ + let ce = CreatorEntity { + orcid: None, + name: None, + state: None, + ident: None, + revision: None, + redirect: None, + editgroup: None, + }; + Box::new(futures::done(Ok( + CreatorIdGetResponse::FetchASingleCreatorById(ce), + ))) } fn creator_lookup_get( diff --git a/rust/src/database_models.rs b/rust/src/database_models.rs new file mode 100644 index 00000000..c0118dda --- /dev/null +++ b/rust/src/database_models.rs @@ -0,0 +1,222 @@ + +use uuid::Uuid; +//use diesel::prelude::*; + +use database_schema::*; + +// Ugh. I thought the whole point was to *not* do this, but: +// https://github.com/diesel-rs/diesel/issues/1589 + +/* +table! { + changelog (id) { + id -> Int8, + editgroup_id -> Int8, + timestamp -> Nullable, + } +} +*/ + +#[derive(Debug, Queryable, Identifiable, Associations)] // AsChangeset +#[table_name = "container_edit"] +pub struct ContainerEditRow { + pub id: i64, + pub ident_id: Uuid, + pub rev_id: Option, + pub redirect_id: Option, + pub editgroup_id: i64, + //pub extra_json: Option, +} + +#[derive(Debug, Queryable, Identifiable, Associations)] // AsChangeset +#[table_name = "container_ident"] +pub struct ContainerIdentRow { + pub id: Uuid, + pub is_live: bool, + pub rev_id: Option, + pub redirect_id: Option, +} + +#[derive(Debug, Queryable, Identifiable, Associations)] // AsChangeset +#[table_name = "container_rev"] +pub struct ContainerRevRow { + pub id: i64, + //extra_json: Option, + pub name: Option, + pub parent_ident_id: Option, + pub publisher: Option, + pub issn: Option, +} + +/* +table! { + creator_edit (id) { + id -> Int8, + extra_json -> Nullable, + ident_id -> Uuid, + rev_id -> Nullable, + redirect_id -> Nullable, + editgroup_id -> Int8, + } +} + +table! { + creator_ident (id) { + id -> Uuid, + is_live -> Bool, + rev_id -> Nullable, + redirect_id -> Nullable, + } +} + +table! { + creator_rev (id) { + id -> Int8, + extra_json -> Nullable, + name -> Nullable, + orcid -> Nullable, + } +} + +table! { + editgroup (id) { + id -> Int8, + extra_json -> Nullable, + editor_id -> Int8, + description -> Nullable, + } +} + +table! { + editor (id) { + id -> Int8, + username -> Text, + is_admin -> Bool, + active_editgroup_id -> Nullable, + } +} + +table! { + file_edit (id) { + id -> Int8, + extra_json -> Nullable, + ident_id -> Uuid, + rev_id -> Nullable, + redirect_id -> Nullable, + editgroup_id -> Int8, + } +} + +table! { + file_ident (id) { + id -> Uuid, + is_live -> Bool, + rev_id -> Nullable, + redirect_id -> Nullable, + } +} + +table! { + file_release (id) { + id -> Int8, + file_rev -> Int8, + target_release_ident_id -> Uuid, + } +} + +table! { + file_rev (id) { + id -> Int8, + extra_json -> Nullable, + size -> Nullable, + sha1 -> Nullable, + url -> Nullable, + } +} + +table! { + release_contrib (id) { + id -> Int8, + release_rev -> Int8, + creator_ident_id -> Nullable, + stub -> Nullable, + contrib_type -> Nullable, + } +} + +table! { + release_edit (id) { + id -> Int8, + extra_json -> Nullable, + ident_id -> Uuid, + rev_id -> Nullable, + redirect_id -> Nullable, + editgroup_id -> Int8, + } +} + +table! { + release_ident (id) { + id -> Uuid, + is_live -> Bool, + rev_id -> Nullable, + redirect_id -> Nullable, + } +} + +table! { + release_ref (id) { + id -> Int8, + release_rev -> Int8, + target_release_ident_id -> Nullable, + index -> Nullable, + stub -> Nullable, + } +} + +table! { + release_rev (id) { + id -> Int8, + extra_json -> Nullable, + work_ident_id -> Nullable, + container_ident_id -> Nullable, + title -> Nullable, + license -> Nullable, + release_type -> Nullable, + date -> Nullable, + doi -> Nullable, + volume -> Nullable, + pages -> Nullable, + issue -> Nullable, + } +} + +table! { + work_edit (id) { + id -> Int8, + extra_json -> Nullable, + ident_id -> Uuid, + rev_id -> Nullable, + redirect_id -> Nullable, + editgroup_id -> Int8, + } +} + +table! { + work_ident (id) { + id -> Uuid, + is_live -> Bool, + rev_id -> Nullable, + redirect_id -> Nullable, + } +} + +table! { + work_rev (id) { + id -> Int8, + extra_json -> Nullable, + work_type -> Nullable, + primary_release_id -> Nullable, + } +} +*/ diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 56364be0..196dee36 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -3,6 +3,7 @@ extern crate fatcat_api; extern crate chrono; #[macro_use] extern crate diesel; +extern crate uuid; extern crate dotenv; extern crate futures; #[macro_use] @@ -15,6 +16,7 @@ extern crate r2d2; pub mod api_server; pub mod database_schema; +pub mod database_models; mod errors { error_chain!{} @@ -31,6 +33,8 @@ use std::env; pub type ConnectionPool = r2d2::Pool>; +/// Establish a direct database connection. Not currently used, but could be helpful for +/// single-threaded tests or utilities. pub fn establish_connection() -> PgConnection { dotenv().ok(); @@ -38,7 +42,7 @@ pub fn establish_connection() -> PgConnection { PgConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url)) } -/// Instantiate a new server. +/// Instantiate a new API server with a pooled database connection pub fn server() -> Result { dotenv().ok(); let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); -- cgit v1.2.3