From 5cdfd2ef70bf35c5c39ef6218e13a84559b09316 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Mon, 6 Dec 2021 22:42:36 +0000 Subject: [PATCH] switch chrono for time fixes #191 --- .github/workflows/ci.yml | 14 +++++++------- CHANGELOG.md | 4 ++++ refinery/Cargo.toml | 2 +- refinery/tests/mysql.rs | 17 +++++++++++++---- refinery/tests/mysql_async.rs | 17 +++++++++++++---- refinery/tests/postgres.rs | 17 +++++++++++++---- refinery/tests/rusqlite.rs | 17 +++++++++++++---- refinery/tests/tiberius.rs | 17 +++++++++++++---- refinery/tests/tokio_postgres.rs | 17 +++++++++++++---- refinery_core/Cargo.toml | 2 +- refinery_core/src/drivers/config.rs | 2 +- refinery_core/src/drivers/mysql.rs | 8 ++++---- refinery_core/src/drivers/mysql_async.rs | 8 ++++---- refinery_core/src/drivers/postgres.rs | 9 +++++---- refinery_core/src/drivers/rusqlite.rs | 8 ++++---- refinery_core/src/drivers/tiberius.rs | 8 ++++---- refinery_core/src/drivers/tokio_postgres.rs | 8 ++++---- refinery_core/src/runner.rs | 10 +++++----- refinery_core/src/traits/async.rs | 9 +++++---- refinery_core/src/traits/sync.rs | 13 ++++++++----- 20 files changed, 135 insertions(+), 72 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9ecb85d1..a5b27493 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,7 +23,7 @@ jobs: strategy: matrix: os: [windows-latest, ubuntu-latest] - rust: [stable, nightly, 1.55.0] + rust: [stable, nightly, 1.56.0] steps: - uses: actions/checkout@v2 - uses: actions-rs/toolchain@v1 @@ -45,7 +45,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - rust: [stable, nightly, 1.55.0] + rust: [stable, nightly, 1.56.0] steps: - uses: actions/checkout@v2 - uses: actions-rs/toolchain@v1 @@ -59,7 +59,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - rust: [stable, nightly, 1.55.0] + rust: [stable, nightly, 1.56.0] services: postgres: image: postgres:9.6.13-alpine @@ -78,7 +78,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - rust: [stable, nightly, 1.55.0] + rust: [stable, nightly, 1.56.0] services: postgres: image: postgres:9.6.13-alpine @@ -96,7 +96,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - rust: [stable, nightly, 1.55.0] + rust: [stable, nightly, 1.56.0] services: postgres: image: mysql:latest @@ -120,7 +120,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - rust: [stable, nightly, 1.55.0] + rust: [stable, nightly, 1.56.0] services: postgres: image: mysql:latest @@ -143,7 +143,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - rust: [stable, nightly, 1.55.0] + rust: [stable, nightly, 1.56.0] services: mssql: image: mcr.microsoft.com/mssql/server:2017-latest diff --git a/CHANGELOG.md b/CHANGELOG.md index b4f5afdc..9ecb397f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased - 2021-10-16 +### Changed +- replace `chrono` with `time`, fixes [191](https://github.com/rust-db/refinery/issues/191) + ## [0.7.0] - 2021-10-16 ### Added - Add `Target::Fake` and `Target::FakeVersion` to allow users to only update refinery's schema migration table without actually running the migration files diff --git a/refinery/Cargo.toml b/refinery/Cargo.toml index cf701d9d..35d77830 100644 --- a/refinery/Cargo.toml +++ b/refinery/Cargo.toml @@ -32,6 +32,6 @@ futures = "0.3" assert_cmd = "2.0" predicates = "2" tempfile = "3" -chrono = "0.4" +time = "0.3.5" tokio-util = { version = "0.6.7", features = ["compat"] } tokio = { version = "1.9.0", features = ["full"] } diff --git a/refinery/tests/mysql.rs b/refinery/tests/mysql.rs index 6b3e3f3d..600950dd 100644 --- a/refinery/tests/mysql.rs +++ b/refinery/tests/mysql.rs @@ -3,7 +3,6 @@ use barrel::backend::MySql as Sql; #[cfg(feature = "mysql")] mod mysql { use assert_cmd::prelude::*; - use chrono::Local; use mysql::prelude::TextQuery; use predicates::str::contains; use refinery::embed_migrations; @@ -14,6 +13,7 @@ mod mysql { }; use refinery_core::mysql; use std::process::Command; + use time::OffsetDateTime; mod embedded { use refinery::embed_migrations; @@ -210,7 +210,10 @@ mod mysql { let current = conn.get_last_applied_migration().unwrap().unwrap(); assert_eq!(4, current.version()); - assert_eq!(Local::today(), current.applied_on().unwrap().date()); + assert_eq!( + OffsetDateTime::now_utc().date(), + current.applied_on().unwrap().date() + ); }); } @@ -231,7 +234,10 @@ mod mysql { let current = conn.get_last_applied_migration().unwrap().unwrap(); assert_eq!(4, current.version()); - assert_eq!(Local::today(), current.applied_on().unwrap().date()); + assert_eq!( + OffsetDateTime::now_utc().date(), + current.applied_on().unwrap().date() + ); }); } @@ -253,7 +259,10 @@ mod mysql { let migrations = get_migrations(); let applied_migrations = err.report().unwrap().applied_migrations(); - assert_eq!(Local::today(), current.applied_on().unwrap().date()); + assert_eq!( + OffsetDateTime::now_utc().date(), + current.applied_on().unwrap().date() + ); assert_eq!(2, current.version()); assert_eq!(2, applied_migrations.len()); diff --git a/refinery/tests/mysql_async.rs b/refinery/tests/mysql_async.rs index a2a2a8fd..1c027128 100644 --- a/refinery/tests/mysql_async.rs +++ b/refinery/tests/mysql_async.rs @@ -2,7 +2,6 @@ use barrel::backend::MySql as Sql; #[cfg(feature = "mysql_async")] mod mysql_async { - use chrono::Local; use futures::FutureExt; use refinery::{ config::{Config, ConfigDbType}, @@ -13,6 +12,7 @@ mod mysql_async { use refinery_core::mysql_async; use refinery_core::mysql_async::prelude::Queryable; use std::panic::AssertUnwindSafe; + use time::OffsetDateTime; fn get_migrations() -> Vec { embed_migrations!("./tests/migrations"); @@ -237,7 +237,10 @@ mod mysql_async { let current = pool.get_last_applied_migration().await.unwrap().unwrap(); assert_eq!(4, current.version()); - assert_eq!(Local::today(), current.applied_on().unwrap().date()); + assert_eq!( + OffsetDateTime::now_utc().date(), + current.applied_on().unwrap().date() + ); }) .await } @@ -257,7 +260,10 @@ mod mysql_async { let current = pool.get_last_applied_migration().await.unwrap().unwrap(); assert_eq!(4, current.version()); - assert_eq!(Local::today(), current.applied_on().unwrap().date()); + assert_eq!( + OffsetDateTime::now_utc().date(), + current.applied_on().unwrap().date() + ); }) .await } @@ -276,7 +282,10 @@ mod mysql_async { let migrations = get_migrations(); let applied_migrations = err.report().unwrap().applied_migrations(); - assert_eq!(Local::today(), current.applied_on().unwrap().date()); + assert_eq!( + OffsetDateTime::now_utc().date(), + current.applied_on().unwrap().date() + ); assert_eq!(2, current.version()); assert_eq!(2, applied_migrations.len()); diff --git a/refinery/tests/postgres.rs b/refinery/tests/postgres.rs index 4c1859b9..34deb747 100644 --- a/refinery/tests/postgres.rs +++ b/refinery/tests/postgres.rs @@ -3,7 +3,6 @@ use barrel::backend::Pg as Sql; #[cfg(feature = "postgres")] mod postgres { use assert_cmd::prelude::*; - use chrono::Local; use predicates::str::contains; use refinery::{ config::{Config, ConfigDbType}, @@ -13,6 +12,7 @@ mod postgres { }; use refinery_core::postgres::{Client, NoTls}; use std::process::Command; + use time::OffsetDateTime; mod embedded { use refinery::embed_migrations; @@ -214,7 +214,10 @@ mod postgres { let current = client.get_last_applied_migration().unwrap().unwrap(); assert_eq!(4, current.version()); - assert_eq!(Local::today(), current.applied_on().unwrap().date()); + assert_eq!( + OffsetDateTime::now_utc().date(), + current.applied_on().unwrap().date() + ); }); } @@ -231,7 +234,10 @@ mod postgres { let current = client.get_last_applied_migration().unwrap().unwrap(); assert_eq!(4, current.version()); - assert_eq!(Local::today(), current.applied_on().unwrap().date()); + assert_eq!( + OffsetDateTime::now_utc().date(), + current.applied_on().unwrap().date() + ); }); } @@ -252,7 +258,10 @@ mod postgres { let migrations = get_migrations(); let applied_migrations = err.report().unwrap().applied_migrations(); - assert_eq!(Local::today(), current.applied_on().unwrap().date()); + assert_eq!( + OffsetDateTime::now_utc().date(), + current.applied_on().unwrap().date() + ); assert_eq!(2, current.version()); assert_eq!(2, applied_migrations.len()); diff --git a/refinery/tests/rusqlite.rs b/refinery/tests/rusqlite.rs index 866e8ec5..14330532 100644 --- a/refinery/tests/rusqlite.rs +++ b/refinery/tests/rusqlite.rs @@ -3,7 +3,6 @@ use barrel::backend::Sqlite as Sql; #[cfg(feature = "rusqlite")] mod rusqlite { use assert_cmd::prelude::*; - use chrono::Local; use predicates::str::contains; use refinery::{ config::{Config, ConfigDbType}, @@ -15,6 +14,7 @@ mod rusqlite { use refinery_core::rusqlite::{Connection, OptionalExtension}; use std::fs::{self, File}; use std::process::Command; + use time::OffsetDateTime; mod embedded { use refinery::embed_migrations; @@ -188,7 +188,10 @@ mod rusqlite { assert_eq!(4, current.version()); - assert_eq!(Local::today(), current.applied_on().unwrap().date()); + assert_eq!( + OffsetDateTime::now_utc().date(), + current.applied_on().unwrap().date() + ); } #[test] @@ -204,7 +207,10 @@ mod rusqlite { assert_eq!(4, current.version()); - assert_eq!(Local::today(), current.applied_on().unwrap().date()); + assert_eq!( + OffsetDateTime::now_utc().date(), + current.applied_on().unwrap().date() + ); } #[test] @@ -220,7 +226,10 @@ mod rusqlite { let migrations = get_migrations(); let applied_migrations = err.report().unwrap().applied_migrations(); - assert_eq!(Local::today(), current.applied_on().unwrap().date()); + assert_eq!( + OffsetDateTime::now_utc().date(), + current.applied_on().unwrap().date() + ); assert_eq!(2, current.version()); assert_eq!(2, applied_migrations.len()); diff --git a/refinery/tests/tiberius.rs b/refinery/tests/tiberius.rs index 0fa65c91..fc9150aa 100644 --- a/refinery/tests/tiberius.rs +++ b/refinery/tests/tiberius.rs @@ -3,7 +3,6 @@ use barrel::backend::MsSql as Sql; #[cfg(all(feature = "tiberius-config"))] mod tiberius { use assert_cmd::prelude::*; - use chrono::Local; use futures::FutureExt; use predicates::str::contains; use refinery::{ @@ -14,6 +13,7 @@ mod tiberius { use std::panic::AssertUnwindSafe; use std::process::Command; use std::str::FromStr; + use time::OffsetDateTime; use tokio_util::compat::TokioAsyncWriteCompatExt; const CONFIG: &'static str = "mssql://SA:Passw0rd@localhost:1433/refinery_test?trust_cert=true"; @@ -512,7 +512,10 @@ mod tiberius { let current = client.get_last_applied_migration().await.unwrap().unwrap(); assert_eq!(4, current.version()); - assert_eq!(Local::today(), current.applied_on().unwrap().date()); + assert_eq!( + OffsetDateTime::now_utc().date(), + current.applied_on().unwrap().date() + ); }) .await } @@ -543,7 +546,10 @@ mod tiberius { let current = client.get_last_applied_migration().await.unwrap().unwrap(); assert_eq!(4, current.version()); - assert_eq!(Local::today(), current.applied_on().unwrap().date()); + assert_eq!( + OffsetDateTime::now_utc().date(), + current.applied_on().unwrap().date() + ); }) .await } @@ -574,7 +580,10 @@ mod tiberius { let migrations = get_migrations(); let applied_migrations = err.report().unwrap().applied_migrations(); - assert_eq!(Local::today(), current.applied_on().unwrap().date()); + assert_eq!( + OffsetDateTime::now_utc().date(), + current.applied_on().unwrap().date() + ); assert_eq!(2, current.version()); assert_eq!(2, applied_migrations.len()); diff --git a/refinery/tests/tokio_postgres.rs b/refinery/tests/tokio_postgres.rs index d0c2a9ec..7b9da773 100644 --- a/refinery/tests/tokio_postgres.rs +++ b/refinery/tests/tokio_postgres.rs @@ -2,7 +2,6 @@ use barrel::backend::Pg as Sql; #[cfg(feature = "tokio-postgres")] mod tokio_postgres { - use chrono::Local; use futures::FutureExt; use refinery::{ config::{Config, ConfigDbType}, @@ -13,6 +12,7 @@ mod tokio_postgres { use refinery_core::tokio_postgres; use refinery_core::tokio_postgres::NoTls; use std::panic::AssertUnwindSafe; + use time::OffsetDateTime; fn get_migrations() -> Vec { embed_migrations!("./tests/migrations"); @@ -294,7 +294,10 @@ mod tokio_postgres { let current = client.get_last_applied_migration().await.unwrap().unwrap(); assert_eq!(4, current.version()); - assert_eq!(Local::today(), current.applied_on().unwrap().date()); + assert_eq!( + OffsetDateTime::now_utc().date(), + current.applied_on().unwrap().date() + ); }) .await } @@ -320,7 +323,10 @@ mod tokio_postgres { let current = client.get_last_applied_migration().await.unwrap().unwrap(); assert_eq!(4, current.version()); - assert_eq!(Local::today(), current.applied_on().unwrap().date()); + assert_eq!( + OffsetDateTime::now_utc().date(), + current.applied_on().unwrap().date() + ); }) .await } @@ -346,7 +352,10 @@ mod tokio_postgres { let migrations = get_migrations(); let applied_migrations = err.report().unwrap().applied_migrations(); - assert_eq!(Local::today(), current.applied_on().unwrap().date()); + assert_eq!( + OffsetDateTime::now_utc().date(), + current.applied_on().unwrap().date() + ); assert_eq!(2, current.version()); assert_eq!(2, applied_migrations.len()); diff --git a/refinery_core/Cargo.toml b/refinery_core/Cargo.toml index 4d4ef4af..d910afa5 100644 --- a/refinery_core/Cargo.toml +++ b/refinery_core/Cargo.toml @@ -19,7 +19,6 @@ mysql_async = ["mysql-async-driver", "tokio"] [dependencies] async-trait = "0.1" cfg-if = "1.0" -chrono = "0.4" lazy_static = "1" log = "0.4" regex = "1" @@ -39,6 +38,7 @@ tokio = { version = "1.0", features = ["full"], optional = true } tiberius-driver = { package = "tiberius", version = "0.6", optional = true } futures = { version = "0.3.16", optional = true } tokio-util = { version = "0.6.7", features = ["compat"], optional = true } +time = { version = "0.3.5", features = ["parsing", "formatting"] } [dev-dependencies] barrel = { git = "https://github.com/jxs/barrel", features = ["sqlite3", "pg", "mysql", "mssql"] } diff --git a/refinery_core/src/drivers/config.rs b/refinery_core/src/drivers/config.rs index 9c437781..ab6241c3 100644 --- a/refinery_core/src/drivers/config.rs +++ b/refinery_core/src/drivers/config.rs @@ -90,7 +90,7 @@ macro_rules! with_connection { ConfigDbType::Mssql => { panic!("tried to synchronously migrate from config for a mssql database, but tiberius is an async driver"); } - }; + } } } diff --git a/refinery_core/src/drivers/mysql.rs b/refinery_core/src/drivers/mysql.rs index 3a784fa4..33148d94 100644 --- a/refinery_core/src/drivers/mysql.rs +++ b/refinery_core/src/drivers/mysql.rs @@ -1,10 +1,11 @@ use crate::traits::sync::{Migrate, Query, Transaction}; use crate::Migration; -use chrono::{DateTime, Local}; use mysql::{ error::Error as MError, prelude::Queryable, Conn, IsolationLevel, PooledConn, Transaction as MTransaction, TxOpts, }; +use time::format_description::well_known::Rfc3339; +use time::OffsetDateTime; fn get_tx_opts() -> TxOpts { TxOpts::default() @@ -23,9 +24,8 @@ fn query_applied_migrations( let row = row?; let version = row.get(0).unwrap(); let applied_on: String = row.get(2).unwrap(); - let applied_on = DateTime::parse_from_rfc3339(&applied_on) - .unwrap() - .with_timezone(&Local); + // Safe to call unwrap, as we stored it in RFC3339 format on the database + let applied_on = OffsetDateTime::parse(&applied_on, &Rfc3339).unwrap(); let checksum: String = row.get(3).unwrap(); applied.push(Migration::applied( diff --git a/refinery_core/src/drivers/mysql_async.rs b/refinery_core/src/drivers/mysql_async.rs index 5e959266..ebaa8085 100644 --- a/refinery_core/src/drivers/mysql_async.rs +++ b/refinery_core/src/drivers/mysql_async.rs @@ -1,10 +1,11 @@ use crate::traits::r#async::{AsyncMigrate, AsyncQuery, AsyncTransaction}; use crate::Migration; use async_trait::async_trait; -use chrono::{DateTime, Local}; use mysql_async_driver::{ prelude::Queryable, Error as MError, IsolationLevel, Pool, Transaction as MTransaction, TxOpts, }; +use time::format_description::well_known::Rfc3339; +use time::OffsetDateTime; async fn query_applied_migrations<'a>( mut transaction: MTransaction<'a>, @@ -18,9 +19,8 @@ async fn query_applied_migrations<'a>( let (version, name, applied_on, checksum): (i32, String, String, String) = mysql_async_driver::from_row(row); - let applied_on = DateTime::parse_from_rfc3339(&applied_on) - .unwrap() - .with_timezone(&Local); + // Safe to call unwrap, as we stored it in RFC3339 format on the database + let applied_on = OffsetDateTime::parse(&applied_on, &Rfc3339).unwrap(); Migration::applied( version, name, diff --git a/refinery_core/src/drivers/postgres.rs b/refinery_core/src/drivers/postgres.rs index 10c57903..3d177509 100644 --- a/refinery_core/src/drivers/postgres.rs +++ b/refinery_core/src/drivers/postgres.rs @@ -1,7 +1,8 @@ use crate::traits::sync::{Migrate, Query, Transaction}; use crate::Migration; -use chrono::{DateTime, Local}; use postgres::{Client as PgClient, Error as PgError, Transaction as PgTransaction}; +use time::format_description::well_known::Rfc3339; +use time::OffsetDateTime; fn query_applied_migrations( transaction: &mut PgTransaction, @@ -12,9 +13,9 @@ fn query_applied_migrations( for row in rows.into_iter() { let version = row.get(0); let applied_on: String = row.get(2); - let applied_on = DateTime::parse_from_rfc3339(&applied_on) - .unwrap() - .with_timezone(&Local); + // Safe to call unwrap, as we stored it in RFC3339 format on the database + let applied_on = OffsetDateTime::parse(&applied_on, &Rfc3339).unwrap(); + let checksum: String = row.get(3); applied.push(Migration::applied( diff --git a/refinery_core/src/drivers/rusqlite.rs b/refinery_core/src/drivers/rusqlite.rs index d5b35d41..9547ba48 100644 --- a/refinery_core/src/drivers/rusqlite.rs +++ b/refinery_core/src/drivers/rusqlite.rs @@ -1,7 +1,8 @@ use crate::traits::sync::{Migrate, Query, Transaction}; use crate::Migration; -use chrono::{DateTime, Local}; use rusqlite::{Connection as RqlConnection, Error as RqlError}; +use time::format_description::well_known::Rfc3339; +use time::OffsetDateTime; fn query_applied_migrations( transaction: &RqlConnection, @@ -13,9 +14,8 @@ fn query_applied_migrations( while let Some(row) = rows.next()? { let version = row.get(0)?; let applied_on: String = row.get(2)?; - let applied_on = DateTime::parse_from_rfc3339(&applied_on) - .unwrap() - .with_timezone(&Local); + // Safe to call unwrap, as we stored it in RFC3339 format on the database + let applied_on = OffsetDateTime::parse(&applied_on, &Rfc3339).unwrap(); let checksum: String = row.get(3)?; applied.push(Migration::applied( diff --git a/refinery_core/src/drivers/tiberius.rs b/refinery_core/src/drivers/tiberius.rs index 03d75258..b78f1691 100644 --- a/refinery_core/src/drivers/tiberius.rs +++ b/refinery_core/src/drivers/tiberius.rs @@ -2,12 +2,13 @@ use crate::traits::r#async::{AsyncMigrate, AsyncQuery, AsyncTransaction}; use crate::Migration; use async_trait::async_trait; -use chrono::{DateTime, Local}; use futures::{ io::{AsyncRead, AsyncWrite}, TryStreamExt, }; use tiberius_driver::{error::Error, Client, QueryItem}; +use time::format_description::well_known::Rfc3339; +use time::OffsetDateTime; async fn query_applied_migrations( client: &mut Client, @@ -20,9 +21,8 @@ async fn query_applied_migrations( if let QueryItem::Row(row) = item { let version = row.get::(0).unwrap(); let applied_on: &str = row.get::<&str, usize>(2).unwrap(); - let applied_on = DateTime::parse_from_rfc3339(applied_on) - .unwrap() - .with_timezone(&Local); + // Safe to call unwrap, as we stored it in RFC3339 format on the database + let applied_on = OffsetDateTime::parse(applied_on, &Rfc3339).unwrap(); let checksum: String = row.get::<&str, usize>(3).unwrap().to_string(); applied.push(Migration::applied( diff --git a/refinery_core/src/drivers/tokio_postgres.rs b/refinery_core/src/drivers/tokio_postgres.rs index e414a76b..8fcc389d 100644 --- a/refinery_core/src/drivers/tokio_postgres.rs +++ b/refinery_core/src/drivers/tokio_postgres.rs @@ -1,7 +1,8 @@ use crate::traits::r#async::{AsyncMigrate, AsyncQuery, AsyncTransaction}; use crate::Migration; use async_trait::async_trait; -use chrono::{DateTime, Local}; +use time::format_description::well_known::Rfc3339; +use time::OffsetDateTime; use tokio_postgres_driver::error::Error as PgError; use tokio_postgres_driver::{Client, Transaction as PgTransaction}; @@ -14,9 +15,8 @@ async fn query_applied_migrations( for row in rows.into_iter() { let version = row.get(0); let applied_on: String = row.get(2); - let applied_on = DateTime::parse_from_rfc3339(&applied_on) - .unwrap() - .with_timezone(&Local); + // Safe to call unwrap, as we stored it in RFC3339 format on the database + let applied_on = OffsetDateTime::parse(&applied_on, &Rfc3339).unwrap(); let checksum: String = row.get(3); applied.push(Migration::applied( diff --git a/refinery_core/src/runner.rs b/refinery_core/src/runner.rs index 64078397..c2b4b699 100644 --- a/refinery_core/src/runner.rs +++ b/refinery_core/src/runner.rs @@ -1,6 +1,6 @@ -use chrono::{DateTime, Local}; use regex::Regex; use siphasher::sip::SipHasher13; +use time::OffsetDateTime; use std::cmp::Ordering; use std::fmt; @@ -76,7 +76,7 @@ pub struct Migration { version: i32, prefix: Type, sql: Option, - applied_on: Option>, + applied_on: Option, } impl Migration { @@ -127,7 +127,7 @@ impl Migration { pub(crate) fn applied( version: i32, name: String, - applied_on: DateTime, + applied_on: OffsetDateTime, checksum: u64, ) -> Migration { Migration { @@ -144,7 +144,7 @@ impl Migration { // convert the Unapplied into an Applied Migration pub(crate) fn set_applied(&mut self) { - self.applied_on = Some(Local::now()); + self.applied_on = Some(OffsetDateTime::now_utc()); self.state = State::Applied; } @@ -169,7 +169,7 @@ impl Migration { } /// Get the Migration Name - pub fn applied_on(&self) -> Option<&DateTime> { + pub fn applied_on(&self) -> Option<&OffsetDateTime> { self.applied_on.as_ref() } diff --git a/refinery_core/src/traits/async.rs b/refinery_core/src/traits/async.rs index 6c04f076..1b28cb87 100644 --- a/refinery_core/src/traits/async.rs +++ b/refinery_core/src/traits/async.rs @@ -7,6 +7,7 @@ use crate::{Error, Migration, Report, Target}; use async_trait::async_trait; use std::string::ToString; +use time::format_description::well_known::Rfc3339; #[async_trait] pub trait AsyncTransaction { @@ -42,8 +43,8 @@ async fn migrate( migration.set_applied(); let update_query = &format!( "INSERT INTO refinery_schema_history (version, name, applied_on, checksum) VALUES ({}, '{}', '{}', '{}')", - // safe to call unwrap as we just converted it to applied - migration.version(), migration.name(), migration.applied_on().unwrap().to_rfc3339(), migration.checksum().to_string()); + // safe to call unwrap as we just converted it to applied, and we are sure it can be formatted according to RFC 33339 + migration.version(), migration.name(), migration.applied_on().unwrap().format(&Rfc3339).unwrap(), migration.checksum().to_string()); transaction .execute(&[ migration.sql().as_ref().expect("sql must be Some!"), @@ -77,8 +78,8 @@ async fn migrate_grouped( migration.set_applied(); let query = format!( "INSERT INTO refinery_schema_history (version, name, applied_on, checksum) VALUES ({}, '{}', '{}', '{}')", - // safe to call unwrap as we just converted migration to applied - migration.version(), migration.name(), migration.applied_on().unwrap().to_rfc3339(), migration.checksum().to_string() + // safe to call unwrap as we just converted it to applied, and we are sure it can be formatted according to RFC 33339 + migration.version(), migration.name(), migration.applied_on().unwrap().format(&Rfc3339).unwrap(), migration.checksum().to_string() ); let sql = migration.sql().expect("sql must be Some!").to_string(); diff --git a/refinery_core/src/traits/sync.rs b/refinery_core/src/traits/sync.rs index 905cb4e8..1926c40d 100644 --- a/refinery_core/src/traits/sync.rs +++ b/refinery_core/src/traits/sync.rs @@ -1,10 +1,11 @@ +use time::format_description::well_known::Rfc3339; + use crate::error::WrapMigrationError; use crate::traits::{ verify_migrations, ASSERT_MIGRATIONS_TABLE_QUERY, GET_APPLIED_MIGRATIONS_QUERY, GET_LAST_APPLIED_MIGRATION_QUERY, }; use crate::{Error, Migration, Report, Target}; -use chrono::Local; pub trait Transaction { type Error: std::error::Error + Send + Sync + 'static; @@ -38,8 +39,8 @@ fn migrate( migration.set_applied(); let update_query = &format!( "INSERT INTO refinery_schema_history (version, name, applied_on, checksum) VALUES ({}, '{}', '{}', '{}')", - // safe to call unwrap as we just converted it to applied - migration.version(), migration.name(), migration.applied_on().unwrap().to_rfc3339(), migration.checksum()); + // safe to call unwrap as we just converted it to applied, and we are sure it can be formatted according to RFC 33339 + migration.version(), migration.name(), migration.applied_on().unwrap().format(&Rfc3339).unwrap(), migration.checksum()); let sql = migration.sql().expect("sql must be Some!"); transaction.execute(&[sql, update_query]).migration_err( @@ -59,16 +60,18 @@ fn migrate_grouped( let mut grouped_migrations = Vec::new(); let mut applied_migrations = Vec::new(); - for migration in migrations.into_iter() { + for mut migration in migrations.into_iter() { if let Target::Version(input_target) | Target::FakeVersion(input_target) = target { if input_target < migration.version() { break; } } + migration.set_applied(); let query = format!( "INSERT INTO refinery_schema_history (version, name, applied_on, checksum) VALUES ({}, '{}', '{}', '{}')", - migration.version(), migration.name(), Local::now().to_rfc3339(), migration.checksum().to_string() + // safe to call unwrap as we just converted it to applied, and we are sure it can be formatted according to RFC 33339 + migration.version(), migration.name(), migration.applied_on().unwrap().format(&Rfc3339).unwrap(), migration.checksum().to_string() ); let sql = migration.sql().expect("sql must be Some!").to_string();