diff options
author | tom barrett <spalf0@gmail.com> | 2019-04-02 04:53:54 -0500 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2019-04-02 04:53:54 -0500 |
commit | 9e1cc5ab923492570a84ac35fe573d22c051f86b (patch) | |
tree | 8a4371560b0051920aef2c74f0d91636377b147f | |
parent | 810c77ba30c65215c2d5e4b6f8a73f3b73e2b152 (diff) |
added last_modified to database entry
-rw-r--r-- | migrations/1_create_masses/up.sql | 3 | ||||
-rw-r--r-- | src/bin/server.rs | 5 | ||||
-rw-r--r-- | src/mass.rs | 5 | ||||
-rw-r--r-- | src/schema.rs | 1 | ||||
-rw-r--r-- | tests/tests.rs | 6 |
5 files changed, 13 insertions, 7 deletions
diff --git a/migrations/1_create_masses/up.sql b/migrations/1_create_masses/up.sql index 1f10915..b1db62d 100644 --- a/migrations/1_create_masses/up.sql +++ b/migrations/1_create_masses/up.sql @@ -1,5 +1,6 @@ CREATE TABLE masses ( id SERIAL PRIMARY KEY, name TEXT UNIQUE NOT NULL, - mass TEXT NOT NULL + mass TEXT NOT NULL, + last_modified TIMESTAMP NOT NULL ) diff --git a/src/bin/server.rs b/src/bin/server.rs index 708cd3e..9ea1f0d 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -7,7 +7,7 @@ use std::collections::HashMap; use std::io::Write; use std::net::TcpListener; use std::thread::{sleep, spawn}; -use std::time::{Duration, Instant}; +use std::time::{Duration, Instant, SystemTime}; use space::constants; use space::mass::{Mass, MassEntry}; @@ -28,8 +28,9 @@ fn populate() -> HashMap<String, Mass> { fn backup(masses: HashMap<String, Mass>) { let connection = PgConnection::establish(&get_db_url()).expect("Cannot connect"); + let timestamp = SystemTime::now(); for (name, mass) in masses { - let mass_entry = mass.to_mass_entry(name.to_string()); + let mass_entry = mass.to_mass_entry(name.to_string(), timestamp); diesel::insert_into(db_masses) .values(&mass_entry) .on_conflict(name_column) diff --git a/src/mass.rs b/src/mass.rs index 9629e24..b29c372 100644 --- a/src/mass.rs +++ b/src/mass.rs @@ -3,6 +3,7 @@ extern crate rand; use self::rand::distributions::Uniform; use self::rand::Rng; use std::collections::HashMap; +use std::time::SystemTime; use crate::constants; use crate::item::{Item, ItemType}; @@ -32,6 +33,7 @@ pub struct MassEntry { pub id: Option<i32>, pub name: String, pub mass: String, + pub last_modified: SystemTime, } impl MassEntry { @@ -315,11 +317,12 @@ impl Mass { } } - pub fn to_mass_entry(&self, name: String) -> MassEntry { + pub fn to_mass_entry(&self, name: String, last_modified: SystemTime) -> MassEntry { MassEntry { id: None, name, mass: serde_json::to_string(&self).unwrap(), + last_modified, } } } diff --git a/src/schema.rs b/src/schema.rs index c258603..f6ec1f7 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -3,5 +3,6 @@ table! { id -> Nullable<Integer>, name -> Varchar, mass -> Varchar, + last_modified -> Timestamp, } } diff --git a/tests/tests.rs b/tests/tests.rs index 4c5c96b..6dc34b8 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -6,7 +6,7 @@ extern crate space; mod tests { use std::collections::HashMap; use std::thread::sleep; - use std::time::Duration; + use std::time::{Duration, SystemTime}; use diesel::pg::PgConnection; use diesel::prelude::*; @@ -538,7 +538,7 @@ mod tests { let mut mass = Mass::new_astroid(); diesel::insert_into(db_masses) - .values(&mass.to_mass_entry(name.clone())) + .values(&mass.to_mass_entry(name.clone(), SystemTime::now())) .execute(&connection) .expect("Cannot insert"); @@ -559,7 +559,7 @@ mod tests { mass.process(&mut HashMap::new()); diesel::update(db_masses) - .set(mass.to_mass_entry(name.clone())) + .set(mass.to_mass_entry(name.clone(), SystemTime::now())) .execute(&connection) .expect("Cannot update"); |