diff --git a/.gitignore b/.gitignore index 53af949..36e789a 100644 --- a/.gitignore +++ b/.gitignore @@ -121,9 +121,11 @@ Temporary Items # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html -Cargo.lock +# Cargo.lock # These are backup files generated by rustfmt **/*.rs.bk # End of https://www.gitignore.io/api/rust,macos,jetbrains+all + +.env diff --git a/design/db.gv b/design/db.gv index 8a10fd0..cb5ad8f 100644 --- a/design/db.gv +++ b/design/db.gv @@ -28,8 +28,9 @@ digraph { - - + + +
media_format
id
physical
format
is_physical
format_name
description
>]; @@ -46,7 +47,8 @@ digraph { - + +
media_type
id
type
type_name
description
>]; diff --git a/diesel.toml b/diesel.toml new file mode 100644 index 0000000..f2e022a --- /dev/null +++ b/diesel.toml @@ -0,0 +1,7 @@ +# For documentation on how to configure this file, +# see diesel.rs/guides/configuring-diesel-cli + +[print_schema] +file = "src/schema.rs" +with_docs = true +# import_types = ["diesel::sql_types::*", "diesel::*"] \ No newline at end of file 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/2019-03-27-132202_first_tables/down.sql b/migrations/2019-03-27-132202_first_tables/down.sql new file mode 100644 index 0000000..276ac03 --- /dev/null +++ b/migrations/2019-03-27-132202_first_tables/down.sql @@ -0,0 +1,4 @@ +-- This file should undo anything in `up.sql` +DROP TABLE public.media_type_format_link; +DROP TABLE public.media_type; +DROP TABLE public.media_format; diff --git a/migrations/2019-03-27-132202_first_tables/up.sql b/migrations/2019-03-27-132202_first_tables/up.sql new file mode 100644 index 0000000..551862e --- /dev/null +++ b/migrations/2019-03-27-132202_first_tables/up.sql @@ -0,0 +1,28 @@ +-- Your SQL goes here +CREATE TABLE IF NOT EXISTS public.media_type ( + id serial NOT NULL, + type_name character varying(255) NOT NULL, + description text NOT NULL DEFAULT '', + CONSTRAINT media_type_pkey PRIMARY KEY (id) +); + + +CREATE TABLE IF NOT EXISTS public.media_format ( + id serial NOT NULL, + is_physical boolean NOT NULL DEFAULT false, + format_name character varying(255) NOT NULL, + description text NOT NULL DEFAULT '', + CONSTRAINT media_format_pk PRIMARY KEY (id) +); + +CREATE TABLE IF NOT EXISTS public.media_type_format_link ( + media_type_id integer NOT NULL, + media_format_id integer NOT NULL, + CONSTRAINT media_type_format_link_pk PRIMARY KEY (media_type_id, media_format_id), + CONSTRAINT media_type_format_link_format_fk FOREIGN KEY (media_format_id) + REFERENCES public.media_format (id) MATCH SIMPLE + ON UPDATE CASCADE ON DELETE CASCADE, + CONSTRAINT media_type_format_link_type_fk FOREIGN KEY (media_type_id) + REFERENCES public.media_type (id) MATCH SIMPLE + ON UPDATE CASCADE ON DELETE CASCADE +); \ No newline at end of file diff --git a/src/db.rs b/src/db.rs index 89ec09d..7a4b2d8 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,5 +1,4 @@ use diesel::prelude::*; -use diesel::pg::PgConnection; use dotenv::dotenv; use std::env; diff --git a/src/lib.rs b/src/lib.rs index e69de29..8f2c97a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -0,0 +1,3 @@ +pub mod db; +pub mod models; +// pub mod schema; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 6d1abbe..80e7806 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,10 @@ -#[macro_use] -extern crate mime; +#[macro_use] extern crate diesel; +#[macro_use] extern crate mime; use iron::prelude::*; use iron::status; use router::Router; -mod db; - fn hello_world (_request: &mut Request) -> IronResult { let mut response = Response::new(); @@ -37,6 +35,8 @@ fn init_router() -> Router { } fn main() { + media_collection_crud::db::establish_connection(); + let router = init_router(); Iron::new(router) .http("localhost:8000") diff --git a/src/models.rs b/src/models.rs new file mode 100644 index 0000000..7fb1fdf --- /dev/null +++ b/src/models.rs @@ -0,0 +1,34 @@ +use diesel::deserialize::Queryable; + +// use crate::schema::*; + +#[derive(Queryable)] +// #[table_name="media"] +pub struct Media { + pub id: u32, + pub display_name: String, + pub notes: String, +} + +#[derive(Queryable)] +// #[table_name="media_type"] +pub struct MediaType { + pub id: u32, + pub type_name: String, + pub description: String, +} + +#[derive(Queryable)] +// #[table_name="media_format"] +pub struct MediaFormat { + pub id: u32, + pub is_physical: bool, + pub format_name: String, + pub description: String, +} + +#[derive(Queryable)] +pub struct MediaTypeFormatLink { + pub media_type_id: i32, + pub media_format_id: i32, +} \ No newline at end of file diff --git a/src/schema.rs b/src/schema.rs new file mode 100644 index 0000000..c9e9ba1 --- /dev/null +++ b/src/schema.rs @@ -0,0 +1,88 @@ +use diesel::macros::*; + +table! { + /// Representation of the `media_format` table. + /// + /// (Automatically generated by Diesel.) + media_format (id) { + /// The `id` column of the `media_format` table. + /// + /// Its SQL type is `Int4`. + /// + /// (Automatically generated by Diesel.) + id -> Int4, + /// The `is_physical` column of the `media_format` table. + /// + /// Its SQL type is `Bool`. + /// + /// (Automatically generated by Diesel.) + is_physical -> Bool, + /// The `format_name` column of the `media_format` table. + /// + /// Its SQL type is `Varchar`. + /// + /// (Automatically generated by Diesel.) + format_name -> Varchar, + /// The `description` column of the `media_format` table. + /// + /// Its SQL type is `Text`. + /// + /// (Automatically generated by Diesel.) + description -> Text, + } +} + +table! { + /// Representation of the `media_type` table. + /// + /// (Automatically generated by Diesel.) + media_type (id) { + /// The `id` column of the `media_type` table. + /// + /// Its SQL type is `Int4`. + /// + /// (Automatically generated by Diesel.) + id -> Int4, + /// The `type_name` column of the `media_type` table. + /// + /// Its SQL type is `Varchar`. + /// + /// (Automatically generated by Diesel.) + type_name -> Varchar, + /// The `description` column of the `media_type` table. + /// + /// Its SQL type is `Text`. + /// + /// (Automatically generated by Diesel.) + description -> Text, + } +} + +table! { + /// Representation of the `media_type_format_link` table. + /// + /// (Automatically generated by Diesel.) + media_type_format_link (media_type_id, media_format_id) { + /// The `media_type_id` column of the `media_type_format_link` table. + /// + /// Its SQL type is `Int4`. + /// + /// (Automatically generated by Diesel.) + media_type_id -> Int4, + /// The `media_format_id` column of the `media_type_format_link` table. + /// + /// Its SQL type is `Int4`. + /// + /// (Automatically generated by Diesel.) + media_format_id -> Int4, + } +} + +joinable!(media_type_format_link -> media_format (media_format_id)); +joinable!(media_type_format_link -> media_type (media_type_id)); + +allow_tables_to_appear_in_same_query!( + media_format, + media_type, + media_type_format_link, +);