diff --git a/Cargo.toml b/Cargo.toml index f9a6506..7b8d1b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,5 +9,8 @@ license = "MIT" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -diesel = { version = "1.4.6", features = ["postgres"] } +diesel = { version = "1.4.6", features = ["postgres", "serde_json", "chrono", "uuidv07"] } dotenv = "0.15.0" +uuid = { version = "0.8", features = ["v4"] } +chrono = "0.4" +serde_json = "1.0" diff --git a/migrations/2021-06-05-112912_create_fang_tasks/down.sql b/migrations/2021-06-05-112912_create_fang_tasks/down.sql index 291a97c..3cd3345 100644 --- a/migrations/2021-06-05-112912_create_fang_tasks/down.sql +++ b/migrations/2021-06-05-112912_create_fang_tasks/down.sql @@ -1 +1 @@ --- This file should undo anything in `up.sql` \ No newline at end of file +DROP TABLE fang_tasks; diff --git a/migrations/2021-06-05-112912_create_fang_tasks/up.sql b/migrations/2021-06-05-112912_create_fang_tasks/up.sql index 8536d1f..f0fb4d8 100644 --- a/migrations/2021-06-05-112912_create_fang_tasks/up.sql +++ b/migrations/2021-06-05-112912_create_fang_tasks/up.sql @@ -1,6 +1,8 @@ +CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; + CREATE TABLE fang_tasks ( - id BIGSERIAL primary key, - metadata jsonb, + id uuid PRIMARY KEY DEFAULT uuid_generate_v4(), + metadata jsonb NOT NULL, created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() ); diff --git a/src/lib.rs b/src/lib.rs index 1bfd76a..a524d7c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,6 @@ +#[macro_use] +extern crate diesel; + // pub trait JobQueue { // type Job; // type Error; @@ -11,6 +14,10 @@ // pub trait Storage { // fn save() -> // } + +pub mod postgres; +mod schema; + #[cfg(test)] mod tests { #[test] diff --git a/src/postgres.rs b/src/postgres.rs new file mode 100644 index 0000000..f77f99b --- /dev/null +++ b/src/postgres.rs @@ -0,0 +1,57 @@ +use crate::schema::fang_tasks; +use chrono::{DateTime, Utc}; +use diesel::pg::PgConnection; +use diesel::prelude::*; +use diesel::result::Error; +use dotenv::dotenv; +use std::env; +use uuid::Uuid; + +#[derive(Queryable, Identifiable, Debug, Eq, PartialEq)] +#[table_name = "fang_tasks"] +pub struct Task { + pub id: Uuid, + pub metadata: serde_json::Value, + pub created_at: DateTime, + pub updated_at: DateTime, +} + +#[derive(Insertable)] +#[table_name = "fang_tasks"] +pub struct NewTask { + pub metadata: serde_json::Value, +} + +pub struct Postgres { + pub database_url: String, + pub connection: PgConnection, +} + +impl Postgres { + pub fn new(database_url: Option) -> Self { + dotenv().ok(); + + let url = match database_url { + Some(string_url) => string_url, + None => { + let url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); + + url + } + }; + + let connection = + PgConnection::establish(&url).expect(&format!("Error connecting to {}", url)); + + Self { + connection, + database_url: url, + } + } + + pub fn insert(&self, params: &NewTask) -> Result { + diesel::insert_into(fang_tasks::table) + .values(params) + .get_result::(&self.connection) + } +} diff --git a/src/schema.rs b/src/schema.rs index 2749e58..e4499b8 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -1,7 +1,7 @@ table! { fang_tasks (id) { - id -> Int8, - metadata -> Nullable, + id -> Uuid, + metadata -> Jsonb, created_at -> Timestamptz, updated_at -> Timestamptz, }