Skip to content

migrate timestamp fields to timestamptz #1223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/db/add_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub(crate) fn add_package_into_database(
&[
&crate_id,
&metadata_pkg.version,
&registry_data.release_time.naive_utc(),
&registry_data.release_time,
&serde_json::to_value(&dependencies)?,
&metadata_pkg.package_name(),
&registry_data.yanked,
Expand Down
43 changes: 43 additions & 0 deletions src/db/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,49 @@ pub fn migrate(version: Option<Version>, conn: &mut Client) -> CratesfyiResult<(
ADD COLUMN github_last_update TIMESTAMP;
"
),
migration!(
context,
24,
"migrate timestamp to be timezone aware",
// upgrade
"
ALTER TABLE builds
ALTER build_time TYPE timestamptz USING build_time AT TIME ZONE 'UTC';

ALTER TABLE files
ALTER date_added TYPE timestamptz USING date_added AT TIME ZONE 'UTC',
ALTER date_updated TYPE timestamptz USING date_updated AT TIME ZONE 'UTC';

ALTER TABLE github_repos
ALTER updated_at TYPE timestamptz USING updated_at AT TIME ZONE 'UTC',
ALTER last_commit TYPE timestamptz USING last_commit AT TIME ZONE 'UTC';

ALTER TABLE queue
ALTER date_added TYPE timestamptz USING date_added AT TIME ZONE 'UTC';

ALTER TABLE releases
ALTER release_time TYPE timestamptz USING release_time AT TIME ZONE 'UTC';
",
// downgrade
"
ALTER TABLE builds
ALTER build_time TYPE timestamp USING build_time AT TIME ZONE 'UTC';

ALTER TABLE files
ALTER date_added TYPE timestamp USING date_added AT TIME ZONE 'UTC',
ALTER date_updated TYPE timestamp USING date_updated AT TIME ZONE 'UTC';

ALTER TABLE github_repos
ALTER updated_at TYPE timestamp USING updated_at AT TIME ZONE 'UTC',
ALTER last_commit TYPE timestamp USING last_commit AT TIME ZONE 'UTC';

ALTER TABLE queue
ALTER date_added TYPE timestamp USING date_added AT TIME ZONE 'UTC';

ALTER TABLE releases
ALTER release_time TYPE timestamp USING release_time AT TIME ZONE 'UTC';
",
),
];

for migration in migrations {
Expand Down
3 changes: 1 addition & 2 deletions src/storage/database.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::{Blob, StorageTransaction};
use crate::db::Pool;
use crate::Metrics;
use chrono::{DateTime, NaiveDateTime, Utc};
use failure::Error;
use postgres::Transaction;
use std::sync::Arc;
Expand Down Expand Up @@ -61,7 +60,7 @@ impl DatabaseBackend {
Ok(Blob {
path: row.get("path"),
mime: row.get("mime"),
date_updated: DateTime::from_utc(row.get::<_, NaiveDateTime>("date_updated"), Utc),
date_updated: row.get("date_updated"),
content: row.get("content"),
compression,
})
Expand Down
4 changes: 2 additions & 2 deletions src/web/builds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
impl_webpage,
web::{page::WebPage, MetaData},
};
use chrono::{DateTime, NaiveDateTime, Utc};
use chrono::{DateTime, Utc};
use iron::{
headers::{
AccessControlAllowOrigin, CacheControl, CacheDirective, ContentType, Expires, HttpDate,
Expand Down Expand Up @@ -80,7 +80,7 @@ pub fn build_list_handler(req: &mut Request) -> IronResult<Response> {
rustc_version: row.get("rustc_version"),
docsrs_version: row.get("cratesfyi_version"),
build_status: row.get("build_status"),
build_time: DateTime::from_utc(row.get::<_, NaiveDateTime>("build_time"), Utc),
build_time: row.get("build_time"),
output: row.get("output"),
};

Expand Down
4 changes: 2 additions & 2 deletions src/web/crate_details.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{match_version, redirect_base, render_markdown, MatchSemver, MetaData};
use crate::{db::Pool, impl_webpage, web::page::WebPage};
use chrono::{DateTime, NaiveDateTime, Utc};
use chrono::{DateTime, Utc};
use iron::prelude::*;
use iron::Url;
use postgres::Client;
Expand Down Expand Up @@ -181,7 +181,7 @@ impl CrateDetails {
dependencies: krate.get("dependencies"),
readme: krate.get("readme"),
rustdoc: krate.get("description_long"),
release_time: DateTime::from_utc(krate.get::<_, NaiveDateTime>("release_time"), Utc),
release_time: krate.get("release_time"),
build_status: krate.get("build_status"),
last_successful_build: None,
rustdoc_status: krate.get("rustdoc_status"),
Expand Down
10 changes: 5 additions & 5 deletions src/web/releases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
web::{error::Nope, match_version, page::WebPage, redirect_base},
BuildQueue,
};
use chrono::{DateTime, NaiveDateTime, Utc};
use chrono::{DateTime, Utc};
use iron::{
headers::{ContentType, Expires, HttpDate},
mime::{Mime, SubLevel, TopLevel},
Expand Down Expand Up @@ -102,7 +102,7 @@ pub(crate) fn get_releases(conn: &mut Client, page: i64, limit: i64, order: Orde
version: row.get(1),
description: row.get(2),
target_name: row.get(3),
release_time: DateTime::from_utc(row.get::<_, NaiveDateTime>(4), Utc),
release_time: row.get(4),
rustdoc_status: row.get(5),
stars: row.get::<_, Option<i32>>(6).unwrap_or(0),
})
Expand Down Expand Up @@ -149,7 +149,7 @@ fn get_releases_by_author(
version: row.get(1),
description: row.get(2),
target_name: row.get(3),
release_time: DateTime::from_utc(row.get::<_, NaiveDateTime>(4), Utc),
release_time: row.get(4),
rustdoc_status: row.get(5),
stars: row.get::<_, Option<i32>>(6).unwrap_or(0),
}
Expand Down Expand Up @@ -203,7 +203,7 @@ fn get_releases_by_owner(
version: row.get(1),
description: row.get(2),
target_name: row.get(3),
release_time: DateTime::from_utc(row.get::<_, NaiveDateTime>(4), Utc),
release_time: row.get(4),
rustdoc_status: row.get(5),
stars: row.get::<_, Option<i32>>(6).unwrap_or(0),
}
Expand Down Expand Up @@ -286,7 +286,7 @@ fn get_search_results(
version: row.get("version"),
description: row.get("description"),
target_name: row.get("target_name"),
release_time: DateTime::from_utc(row.get("release_time"), Utc),
release_time: row.get("release_time"),
rustdoc_status: row.get("rustdoc_status"),
stars: row.get::<_, Option<i32>>("github_stars").unwrap_or(0),
})
Expand Down
6 changes: 2 additions & 4 deletions src/web/sitemap.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{db::Pool, docbuilder::Limits, impl_webpage, web::page::WebPage};
use chrono::{DateTime, NaiveDateTime, Utc};
use chrono::{DateTime, Utc};
use iron::{
headers::ContentType,
mime::{Mime, SubLevel, TopLevel},
Expand Down Expand Up @@ -37,9 +37,7 @@ pub fn sitemap_handler(req: &mut Request) -> IronResult<Response> {
let releases = query
.into_iter()
.map(|row| {
let time = DateTime::<Utc>::from_utc(row.get::<_, NaiveDateTime>(1), Utc)
.format("%+")
.to_string();
let time = row.get::<_, DateTime<Utc>>(1).format("%+").to_string();

(row.get(0), time)
})
Expand Down