From bdec37a84ed949d647965ee682b0f5d26b8d18e7 Mon Sep 17 00:00:00 2001 From: kolena Date: Mon, 10 Dec 2018 22:03:19 +0100 Subject: [PATCH] Keep created and mtime timestamps for file versions separately --- resources/db-scheme.sql | 1 + src/dao.rs | 13 ++++++++----- src/lib.rs | 3 ++- src/structs.rs | 1 + 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/resources/db-scheme.sql b/resources/db-scheme.sql index fd06932..abe62c7 100644 --- a/resources/db-scheme.sql +++ b/resources/db-scheme.sql @@ -24,6 +24,7 @@ CREATE TABLE IF NOT EXISTS `DBNAME`.`files_versions` ( `id` bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY, `file_id` bigint(20) NOT NULL, `created` datetime(3) NOT NULL, + `mtime` datetime(3) NOT NULL, `size` int(11) NOT NULL, `hash` char(64) NOT NULL, `storage_name` char(64) NOT NULL, diff --git a/src/dao.rs b/src/dao.rs index 845e063..c4bd3e8 100644 --- a/src/dao.rs +++ b/src/dao.rs @@ -111,9 +111,10 @@ impl Dao { let stopwatch = Stopwatch::start_new(); let r = self.pool.prep_exec( - format!("insert into `{}`.files_versions (file_id, created, size, hash, storage_name) values (:file_id, :created, :size, :hash, :storage_name)", self.db_name), + format!("insert into `{}`.files_versions (file_id, created, mtime, size, hash, storage_name) values (:file_id, :created, :mtime, :size, :hash, :storage_name)", self.db_name), params! {"file_id" => file.id, "created" => &new_file_version.created, + "mtime" => &new_file_version.mtime, "size" => &new_file_version.size, "hash" => &new_file_version.hash, "storage_name" => &new_file_version.storage_name @@ -143,7 +144,7 @@ impl Dao { let stopwatch = Stopwatch::start_new(); let result = self.pool.prep_exec( - format!("select files.id, device_id, original_name, files_versions.id, size, hash, created, storage_name from `{}`.files join `{}`.files_versions on `{}`.files_versions.file_id = `{}`.files.id where identity_hash=:identity_hash", + format!("select files.id, device_id, original_name, files_versions.id, size, hash, created, mtime, storage_name from `{}`.files join `{}`.files_versions on `{}`.files_versions.file_id = `{}`.files.id where identity_hash=:identity_hash", self.db_name, self.db_name, self.db_name, self.db_name), params! { "identity_hash" => identity_hash} )?; @@ -152,7 +153,7 @@ impl Dao { // TODO optimize let file_with_versions = result.map(|x| x.unwrap()).map(|row| { - let (id, device_id, original_name, versionid, size, hash, created, storage_name) = mysql::from_row(row); + let (id, device_id, original_name, versionid, size, hash, created, mtime, storage_name) = mysql::from_row(row); ( (id, device_id, original_name), @@ -161,6 +162,7 @@ impl Dao { size, hash, created, + mtime, storage_name } ) @@ -244,13 +246,13 @@ impl Dao { let stopwatch = Stopwatch::start_new(); self.pool.prep_exec( - format!("select files.id, device_id, original_name, files_versions.id, size, hash, created, storage_name from `{}`.files join `{}`.files_versions on `{}`.files_versions.file_id = `{}`.files.id where account_id=:account_id and device_id=:device_id", + format!("select files.id, device_id, original_name, files_versions.id, size, hash, created, mtime, storage_name from `{}`.files join `{}`.files_versions on `{}`.files_versions.file_id = `{}`.files.id where account_id=:account_id and device_id=:device_id", self.db_name, self.db_name, self.db_name, self.db_name), params! { "device_id" => device_id, "account_id" => account_id} ).and_then(|result| { self.report_timer("list_files", stopwatch); let files: Vec = result.map(|x| x.unwrap()).map(|row| { - let (id, device_id, original_name, versionid, size, hash, created, storage_name) = mysql::from_row(row); + let (id, device_id, original_name, versionid, size, hash, created, mtime, storage_name) = mysql::from_row(row); ( (id, device_id, original_name), @@ -259,6 +261,7 @@ impl Dao { size, hash, created, + mtime, storage_name } ) diff --git a/src/lib.rs b/src/lib.rs index 0e53df0..776d4e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -236,7 +236,8 @@ pub fn save(logger: &Logger, statsd_client: StatsdClient, repo: &Repo, dao: &Dao version: 0, // cannot know now, will be filled in after DB insertion size, hash, - created: uploaded_file.mtime, + created: time_stamp, + mtime: uploaded_file.mtime, storage_name }; diff --git a/src/structs.rs b/src/structs.rs index 2f4c98d..23bdf2a 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -20,6 +20,7 @@ pub struct FileVersion { pub size: u64, pub hash: String, pub created: NaiveDateTime, + pub mtime: NaiveDateTime, pub storage_name: String }