🚧 Initial diesel setup
parent
893eefb16a
commit
63cfccdba0
File diff suppressed because it is too large
Load Diff
|
|
@ -6,3 +6,6 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
diesel = "2.1"
|
||||
diesel-async = {version = "0.4", features = ["postgres"]}
|
||||
chrono = { version = "0.4", features = [] }
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
# For documentation on how to configure this file,
|
||||
# see https://diesel.rs/guides/configuring-diesel-cli
|
||||
|
||||
[print_schema]
|
||||
file = "src/schema.rs"
|
||||
custom_type_derives = ["diesel::query_builder::QueryId"]
|
||||
|
||||
[migrations_directory]
|
||||
dir = "migrations"
|
||||
|
|
@ -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();
|
||||
|
|
@ -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;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
-- This file should undo anything in `up.sql`
|
||||
DROP TABLE rustaceans
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
-- Your SQL goes here
|
||||
CREATE TABLE rustaceans (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR NOT NULL,
|
||||
email VARCHAR NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT NOW() NOT NULL
|
||||
)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
-- This file should undo anything in `up.sql`
|
||||
DROP TABLE crates
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
-- Your SQL goes here
|
||||
CREATE TABLE crates (
|
||||
id SERIAL PRIMARY KEY,
|
||||
rustacean_id integer NOT NULL REFERENCES rustaceans(id),
|
||||
code varchar(64) NOT NULL,
|
||||
name varchar(128) NOT NULL,
|
||||
version varchar(64) NOT NULL,
|
||||
description text,
|
||||
created_at TIMESTAMP DEFAULT NOW() NOT NULL
|
||||
)
|
||||
|
|
@ -1,3 +1,6 @@
|
|||
mod models;
|
||||
mod schema;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,39 @@
|
|||
use chrono::NaiveDateTime;
|
||||
use diesel::prelude::*;
|
||||
use crate::schema::*;
|
||||
|
||||
#[derive(Queryable)]
|
||||
pub struct Rustacean {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
pub email: String,
|
||||
pub created_at: NaiveDateTime
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[diesel(table_name=rustaceans)]
|
||||
pub struct NewRustacean {
|
||||
pub name: String,
|
||||
pub email: String,
|
||||
}
|
||||
|
||||
#[derive(Queryable)]
|
||||
pub struct Crate {
|
||||
pub id: i32,
|
||||
pub rustacean_id: i32,
|
||||
pub code: String,
|
||||
pub name: String,
|
||||
pub version: String,
|
||||
pub description: Option<String>,
|
||||
pub created_at: NaiveDateTime
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[diesel(table_name=crates)]
|
||||
pub struct NewCrate {
|
||||
pub rustacean_id: i32,
|
||||
pub code: String,
|
||||
pub name: String,
|
||||
pub version: String,
|
||||
pub description: Option<String>,
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
// @generated automatically by Diesel CLI.
|
||||
|
||||
diesel::table! {
|
||||
crates (id) {
|
||||
id -> Int4,
|
||||
rustacean_id -> Int4,
|
||||
#[max_length = 64]
|
||||
code -> Varchar,
|
||||
#[max_length = 128]
|
||||
name -> Varchar,
|
||||
#[max_length = 64]
|
||||
version -> Varchar,
|
||||
description -> Nullable<Text>,
|
||||
created_at -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
rustaceans (id) {
|
||||
id -> Int4,
|
||||
name -> Varchar,
|
||||
email -> Varchar,
|
||||
created_at -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::joinable!(crates -> rustaceans (rustacean_id));
|
||||
|
||||
diesel::allow_tables_to_appear_in_same_query!(
|
||||
crates,
|
||||
rustaceans,
|
||||
);
|
||||
Loading…
Reference in New Issue