From e93bb3a21f561a57f16faa361c1140cea64afc91 Mon Sep 17 00:00:00 2001 From: Bat Date: Mon, 30 Apr 2018 18:46:27 +0100 Subject: [PATCH] Add creation timestamps --- Cargo.lock | 1 + Cargo.toml | 2 +- migrations/2018-04-30-170445_timestamps/down.sql | 5 +++++ migrations/2018-04-30-170445_timestamps/up.sql | 5 +++++ src/models/blogs.rs | 4 +++- src/models/instance.rs | 4 +++- src/models/posts.rs | 4 +++- src/models/users.rs | 4 +++- src/schema.rs | 4 ++++ 9 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 migrations/2018-04-30-170445_timestamps/down.sql create mode 100644 migrations/2018-04-30-170445_timestamps/up.sql diff --git a/Cargo.lock b/Cargo.lock index 29534203..513e105b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -183,6 +183,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "chrono 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "diesel_derives 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "pq-sys 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index b90d62a8..0817ab7d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ serde_derive = "1.0" serde_json = "1.0" [dependencies.diesel] -features = ["postgres", "r2d2"] +features = ["postgres", "r2d2", "chrono"] version = "*" [dependencies.rocket_contrib] diff --git a/migrations/2018-04-30-170445_timestamps/down.sql b/migrations/2018-04-30-170445_timestamps/down.sql new file mode 100644 index 00000000..6c688b74 --- /dev/null +++ b/migrations/2018-04-30-170445_timestamps/down.sql @@ -0,0 +1,5 @@ +-- This file should undo anything in `up.sql` +ALTER TABLE posts DROP COLUMN creation_date; +ALTER TABLE blogs DROP COLUMN creation_date; +ALTER TABLE users DROP COLUMN creation_date; +ALTER TABLE instances DROP COLUMN creation_date; diff --git a/migrations/2018-04-30-170445_timestamps/up.sql b/migrations/2018-04-30-170445_timestamps/up.sql new file mode 100644 index 00000000..23ad535e --- /dev/null +++ b/migrations/2018-04-30-170445_timestamps/up.sql @@ -0,0 +1,5 @@ +-- Your SQL goes here +ALTER TABLE posts ADD COLUMN creation_date TIMESTAMP NOT NULL DEFAULT now(); +ALTER TABLE blogs ADD COLUMN creation_date TIMESTAMP NOT NULL DEFAULT now(); +ALTER TABLE users ADD COLUMN creation_date TIMESTAMP NOT NULL DEFAULT now(); +ALTER TABLE instances ADD COLUMN creation_date TIMESTAMP NOT NULL DEFAULT now(); diff --git a/src/models/blogs.rs b/src/models/blogs.rs index fd247baa..f6a03128 100644 --- a/src/models/blogs.rs +++ b/src/models/blogs.rs @@ -1,3 +1,4 @@ +use chrono::NaiveDateTime; use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection}; use activity_pub::activity::Activity; @@ -16,7 +17,8 @@ pub struct Blog { pub summary: String, pub outbox_url: String, pub inbox_url: String, - pub instance_id: i32 + pub instance_id: i32, + pub creation_date: NaiveDateTime } #[derive(Insertable)] diff --git a/src/models/instance.rs b/src/models/instance.rs index 0194d870..476e525a 100644 --- a/src/models/instance.rs +++ b/src/models/instance.rs @@ -1,3 +1,4 @@ +use chrono::NaiveDateTime; use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection}; use std::iter::Iterator; @@ -11,7 +12,8 @@ pub struct Instance { pub public_domain: String, pub name: String, pub local: bool, - pub blocked: bool + pub blocked: bool, + pub creation_date: NaiveDateTime } #[derive(Insertable)] diff --git a/src/models/posts.rs b/src/models/posts.rs index d9f3711f..1dfc0eb8 100644 --- a/src/models/posts.rs +++ b/src/models/posts.rs @@ -1,3 +1,4 @@ +use chrono::NaiveDateTime; use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods, BelongingToDsl}; use diesel::dsl::any; use serde_json; @@ -17,7 +18,8 @@ pub struct Post { pub title: String, pub content: String, pub published: bool, - pub license: String + pub license: String, + pub creation_date: NaiveDateTime } #[derive(Insertable)] diff --git a/src/models/users.rs b/src/models/users.rs index ac2430ea..5f4509d4 100644 --- a/src/models/users.rs +++ b/src/models/users.rs @@ -1,4 +1,5 @@ use bcrypt; +use chrono::NaiveDateTime; use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, BelongingToDsl, PgConnection}; use diesel::dsl::any; use rocket::request::{self, FromRequest, Request}; @@ -27,7 +28,8 @@ pub struct User { pub summary: String, pub email: Option, pub hashed_password: Option, - pub instance_id: i32 + pub instance_id: i32, + pub creation_date: NaiveDateTime } #[derive(Insertable)] diff --git a/src/schema.rs b/src/schema.rs index 182e7450..f61f6bc5 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -16,6 +16,7 @@ table! { outbox_url -> Varchar, inbox_url -> Varchar, instance_id -> Int4, + creation_date -> Timestamp, } } @@ -27,6 +28,7 @@ table! { name -> Varchar, local -> Bool, blocked -> Bool, + creation_date -> Timestamp, } } @@ -47,6 +49,7 @@ table! { content -> Text, published -> Bool, license -> Varchar, + creation_date -> Timestamp, } } @@ -62,6 +65,7 @@ table! { email -> Nullable, hashed_password -> Nullable, instance_id -> Int4, + creation_date -> Timestamp, } }