A lot of db setup. Can't really use schemas yet, due to changes in how rust loads macros

This commit is contained in:
Timothy Warren 2019-03-27 16:50:54 -04:00
parent 2e9092e049
commit df64ce2c19
13 changed files with 218 additions and 9 deletions

4
.gitignore vendored
View File

@ -121,9 +121,11 @@ Temporary Items
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries # 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 # 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 # These are backup files generated by rustfmt
**/*.rs.bk **/*.rs.bk
# End of https://www.gitignore.io/api/rust,macos,jetbrains+all # End of https://www.gitignore.io/api/rust,macos,jetbrains+all
.env

View File

@ -28,8 +28,9 @@ digraph {
<table border="0" cellborder="1" cellspacing="0"> <table border="0" cellborder="1" cellspacing="0">
<tr><td><b>media_format</b></td></tr> <tr><td><b>media_format</b></td></tr>
<tr><td port="id">id</td></tr> <tr><td port="id">id</td></tr>
<tr><td port="physical">physical</td></tr> <tr><td port="physical">is_physical</td></tr>
<tr><td port="format">format</td></tr> <tr><td port="format_name">format_name</td></tr>
<tr><td port="description">description</td></tr>
</table> </table>
>]; >];
@ -46,7 +47,8 @@ digraph {
<table border="0" cellborder="1" cellspacing="0"> <table border="0" cellborder="1" cellspacing="0">
<tr><td><b>media_type</b></td></tr> <tr><td><b>media_type</b></td></tr>
<tr><td port="id">id</td></tr> <tr><td port="id">id</td></tr>
<tr><td port="type">type</td></tr> <tr><td port="type_name">type_name</td></tr>
<tr><td port="description">description</td></tr>
</table> </table>
>]; >];

7
diesel.toml Normal file
View File

@ -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::*"]

0
migrations/.gitkeep Normal file
View File

View File

@ -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();

View File

@ -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;

View File

@ -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;

View File

@ -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
);

View File

@ -1,5 +1,4 @@
use diesel::prelude::*; use diesel::prelude::*;
use diesel::pg::PgConnection;
use dotenv::dotenv; use dotenv::dotenv;
use std::env; use std::env;

View File

@ -0,0 +1,3 @@
pub mod db;
pub mod models;
// pub mod schema;

View File

@ -1,12 +1,10 @@
#[macro_use] #[macro_use] extern crate diesel;
extern crate mime; #[macro_use] extern crate mime;
use iron::prelude::*; use iron::prelude::*;
use iron::status; use iron::status;
use router::Router; use router::Router;
mod db;
fn hello_world (_request: &mut Request) -> IronResult<Response> { fn hello_world (_request: &mut Request) -> IronResult<Response> {
let mut response = Response::new(); let mut response = Response::new();
@ -37,6 +35,8 @@ fn init_router() -> Router {
} }
fn main() { fn main() {
media_collection_crud::db::establish_connection();
let router = init_router(); let router = init_router();
Iron::new(router) Iron::new(router)
.http("localhost:8000") .http("localhost:8000")

34
src/models.rs Normal file
View File

@ -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,
}

88
src/schema.rs Normal file
View File

@ -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,
);