Skip to content

Commit

Permalink
db: fix TIMESTAMP on PostgresQL
Browse files Browse the repository at this point in the history
  • Loading branch information
xtexChooser committed Jan 31, 2024
1 parent df1bcfa commit 8d4c293
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/db/model/issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Model {
#[sea_orm(column_type = "Uuid", indexed)]
pub page: Uuid,
#[sea_orm(column_type = "Timestamp")]
pub found_at: DateTimeUtc,
pub found_at: DateTime,
#[sea_orm(column_type = "String(None)", indexed)]
pub issue_type: String,
#[sea_orm(column_type = "JsonBinary")]
Expand Down
4 changes: 2 additions & 2 deletions src/db/model/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ pub struct Model {
#[sea_orm(column_type = "String(Some(255))")]
pub title: String,
#[sea_orm(column_type = "Timestamp")]
pub last_checked: DateTimeUtc,
pub last_checked: DateTime,
#[sea_orm(column_type = "Timestamp", nullable, default_value = "None")]
pub need_check: Option<DateTimeUtc>,
pub need_check: Option<DateTime>,
#[sea_orm(column_type = "Unsigned", default_value = "0")]
pub check_errors: u32,
#[sea_orm(column_type = "Unsigned", default_value = "0")]
Expand Down
2 changes: 1 addition & 1 deletion src/db/model/rcsyncer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub struct Model {
#[sea_orm(primary_key, auto_increment = false, unique, indexed)]
pub id: Uuid,
#[sea_orm(column_type = "Timestamp")]
pub last_synced_at: DateTimeUtc,
pub last_synced_at: DateTime,
#[sea_orm(column_type = "Unsigned", default_value = 0)]
pub last_rc_id: u32,
}
Expand Down
2 changes: 1 addition & 1 deletion src/db/model/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct Model {
#[sea_orm(column_type = "Boolean", default_value = "false")]
pub sysop: bool,
#[sea_orm(column_type = "Timestamp", nullable, default_value = "None")]
pub blocked: Option<DateTimeUtc>,
pub blocked: Option<DateTime>,
}

impl Display for Model {
Expand Down
16 changes: 8 additions & 8 deletions src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl Page {
id: ActiveValue::Set(Self::get_page_id(lang, title)),
lang: ActiveValue::Set(lang.to_owned()),
title: ActiveValue::Set(title.to_owned()),
need_check: ActiveValue::Set(Some(Utc::now())),
need_check: ActiveValue::Set(Some(Utc::now().naive_utc())),
..Default::default()
};
Ok(Some(Self(new.insert(&*db::get()).await?)))
Expand All @@ -110,15 +110,15 @@ impl Page {
}

pub fn last_checked(&self) -> Option<DateTime<Utc>> {
if self.0.last_checked == DateTime::UNIX_EPOCH {
if self.0.last_checked.and_utc() == DateTime::UNIX_EPOCH {
None
} else {
Some(self.0.last_checked.to_owned())
Some(self.0.last_checked.and_utc())
}
}

pub fn check_requested_time(&self) -> Option<DateTime<Utc>> {
self.0.need_check.map(|t|t)
self.0.need_check.map(|ts| ts.and_utc())
}

pub fn check_errors(&self) -> u32 {
Expand All @@ -135,7 +135,7 @@ impl Page {

pub async fn mark_check(self) -> Result<()> {
let mut model = self.0.into_active_model();
model.need_check = ActiveValue::Set(Some(Utc::now()));
model.need_check = ActiveValue::Set(Some(Utc::now().naive_utc()));
model.check_errors = ActiveValue::Set(0);
model.update(&*db::get()).await?;
App::get().linter_notify.notify_one();
Expand Down Expand Up @@ -168,7 +168,7 @@ impl Page {
false
};
let mut model = self.0.into_active_model();
model.last_checked = ActiveValue::Set(Utc::now());
model.last_checked = ActiveValue::Set(Utc::now().naive_utc());
if !drop_result {
model.need_check = ActiveValue::Set(None);
}
Expand All @@ -180,7 +180,7 @@ impl Page {
}

pub async fn defer_check(self) -> Result<()> {
let check_time = self.check_requested_time();
let check_time = self.check_requested_time().map(|ts| ts.naive_utc());
let check_errors = self.check_errors();
let mut model = self.0.into_active_model();
model.need_check = ActiveValue::Set(Some(
Expand All @@ -202,7 +202,7 @@ impl Page {
info!("marking all pages for check");
db::page::Entity::update_many()
.set(db::page::ActiveModel {
need_check: ActiveValue::Set(Some(Utc::now())),
need_check: ActiveValue::Set(Some(Utc::now().naive_utc())),
..Default::default()
})
.exec(&*db::get())
Expand Down
8 changes: 4 additions & 4 deletions src/rcsyncer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl RcSyncerState {
} else {
let new = db::rcsyncer::ActiveModel {
id: ActiveValue::Set(Page::get_lang_id(lang)),
last_synced_at: ActiveValue::Set(Utc::now()),
last_synced_at: ActiveValue::Set(Utc::now().naive_utc()),
last_rc_id: ActiveValue::Set(0),
};
Ok(Self(new.insert(&*db::get()).await?))
Expand All @@ -61,8 +61,8 @@ impl RcSyncerState {
&self.0.id
}

pub fn last_synced_at(&self) -> &DateTime<Utc> {
&self.0.last_synced_at
pub fn last_synced_at(&self) -> DateTime<Utc> {
self.0.last_synced_at.and_utc()
}

pub fn last_rc_id(&self) -> u32 {
Expand Down Expand Up @@ -122,7 +122,7 @@ pub async fn sync_rc(lang: &str) -> Result<()> {
}

let mut state = state.0.into_active_model();
state.last_synced_at = ActiveValue::Set(end_time);
state.last_synced_at = ActiveValue::Set(end_time.naive_utc());
state.last_rc_id = ActiveValue::Set(last_rcid);
state.update(&*db::get()).await?;

Expand Down
6 changes: 3 additions & 3 deletions src/web/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ async fn auth_handler(auth: AuthResult, Query(params): Query<AuthParams>) -> Web
}
};
if let Some(blocked) = user.blocked {
if blocked <= Utc::now() {
if blocked.and_utc() <= Utc::now() {
user = {
let mut model = user.into_active_model();
model.blocked = ActiveValue::Set(None);
Expand Down Expand Up @@ -195,7 +195,7 @@ async fn auth_handler(auth: AuthResult, Query(params): Query<AuthParams>) -> Web
title: "Login Blocked",
message: &format!(
"You are blocked until {}",
blocked.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
blocked.and_utc().to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
),
auto_return: false,
},
Expand Down Expand Up @@ -233,7 +233,7 @@ pub async fn login(token: &str) -> Option<AuthInfo> {
&& validate_token(&user.salt, token)
{
if let Some(blocked) = user.blocked {
if blocked <= Utc::now() {
if blocked.and_utc() <= Utc::now() {
let mut user = user.into_active_model();
user.blocked = ActiveValue::Set(None);
match user.update(&*db::get()).await {
Expand Down
6 changes: 3 additions & 3 deletions src/web/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async fn get_user_handler(auth: AuthResult, Path(id): Path<String>) -> WebResult
let is_blocked = user.blocked.is_some();
let is_permanent_block = user
.blocked
.map(|t| t == DateTime::<Utc>::MAX_UTC)
.map(|t| t == DateTime::<Utc>::MAX_UTC.naive_utc())
.unwrap_or(false);
return Ok(InfoPage {
auth,
Expand Down Expand Up @@ -196,7 +196,7 @@ async fn block_handler(
let time = params.time;
info!(target = %user, user = %auth, %time, "block user");
let mut user = user.into_active_model();
user.blocked = ActiveValue::Set(Some(Utc::now() + time));
user.blocked = ActiveValue::Set(Some(Utc::now().naive_utc() + time));
let user = user.update(&*db::get()).await?;
App::get().login_lru.write().clear();
Ok(MessagePage {
Expand All @@ -220,7 +220,7 @@ async fn permanent_block_handler(
)?;
info!(target = %user, user = %auth, "permanently block user");
let mut user = user.into_active_model();
user.blocked = ActiveValue::Set(Some(DateTime::<Utc>::MAX_UTC));
user.blocked = ActiveValue::Set(Some(DateTime::<Utc>::MAX_UTC.naive_utc()));
let user = user.update(&*db::get()).await?;
App::get().login_lru.write().clear();
Ok(MessagePage {
Expand Down
2 changes: 1 addition & 1 deletion templates/sysop/rcsyncer.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<tr>
<th scope="row">{{ lang }}</th>
<td>{{ state.id }}</td>
<td>{{ state.last_synced_at.to_rfc3339_opts(chrono::SecondsFormat::Secs, true) }}</td>
<td>{{ state.last_synced_at.and_utc().to_rfc3339_opts(chrono::SecondsFormat::Secs, true) }}</td>
<td>{{ state.last_rc_id }}</td>
</tr>
{%- endfor %}
Expand Down
2 changes: 1 addition & 1 deletion templates/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ <h1><span>{{ user.name }}</span>
<p class="text-secondary">{{ user.id }}</p>

{%- if let Some(until) = user.blocked -%}
<p>This user has been blocked until {{ until.to_rfc3339_opts(chrono::SecondsFormat::Secs, true) }}</p>
<p>This user has been blocked until {{ until.and_utc().to_rfc3339_opts(chrono::SecondsFormat::Secs, true) }}</p>
{%- endif -%}

<div>
Expand Down

0 comments on commit 8d4c293

Please sign in to comment.