fix: use timestamptz
This commit is contained in:
@@ -21,5 +21,4 @@ CREATE EXTENSION pg_trgm;
|
|||||||
|
|
||||||
```
|
```
|
||||||
$ diesel run
|
$ diesel run
|
||||||
$ python3 tools/migdb.py
|
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ CREATE TABLE posts (
|
|||||||
is_tmp BOOLEAN NOT NULL DEFAULT FALSE,
|
is_tmp BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
n_attentions INTEGER NOT NULL DEFAULT 0,
|
n_attentions INTEGER NOT NULL DEFAULT 0,
|
||||||
n_comments INTEGER NOT NULL DEFAULT 0,
|
n_comments INTEGER NOT NULL DEFAULT 0,
|
||||||
create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
create_time TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||||
last_comment_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
last_comment_time TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||||
is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
is_reported BOOLEAN NOT NULL DEFAULT FALSE,
|
is_reported BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
hot_score INTEGER NOT NULL DEFAULT 0,
|
hot_score INTEGER NOT NULL DEFAULT 0,
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ CREATE TABLE comments (
|
|||||||
author_title VARCHAR NOT NULL DEFAULT '',
|
author_title VARCHAR NOT NULL DEFAULT '',
|
||||||
is_tmp BOOLEAN NOT NULL DEFAULT FALSE,
|
is_tmp BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
content TEXT NOT NULL,
|
content TEXT NOT NULL,
|
||||||
create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
create_time TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||||
is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
allow_search BOOLEAN NOT NULL DEFAULT FALSE,
|
allow_search BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
post_id INTEGER NOT NULL REFERENCES posts(id)
|
post_id INTEGER NOT NULL REFERENCES posts(id)
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use crate::db_conn::Db;
|
|||||||
use crate::models::*;
|
use crate::models::*;
|
||||||
use crate::rds_conn::RdsConn;
|
use crate::rds_conn::RdsConn;
|
||||||
use crate::rds_models::*;
|
use crate::rds_models::*;
|
||||||
use chrono::NaiveDateTime;
|
use chrono::{offset::Utc, DateTime};
|
||||||
use rocket::form::Form;
|
use rocket::form::Form;
|
||||||
use rocket::serde::{
|
use rocket::serde::{
|
||||||
json::{json, Value},
|
json::{json, Value},
|
||||||
@@ -27,7 +27,7 @@ pub struct CommentOutput {
|
|||||||
can_del: bool,
|
can_del: bool,
|
||||||
name_id: i32,
|
name_id: i32,
|
||||||
is_tmp: bool,
|
is_tmp: bool,
|
||||||
create_time: NaiveDateTime,
|
create_time: DateTime<Utc>,
|
||||||
// for old version frontend
|
// for old version frontend
|
||||||
timestamp: i64,
|
timestamp: i64,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ use crate::db_conn::Db;
|
|||||||
use crate::models::*;
|
use crate::models::*;
|
||||||
use crate::rds_conn::RdsConn;
|
use crate::rds_conn::RdsConn;
|
||||||
use crate::rds_models::*;
|
use crate::rds_models::*;
|
||||||
use chrono::NaiveDateTime;
|
use chrono::{offset::Utc, DateTime};
|
||||||
use rocket::form::Form;
|
use rocket::form::Form;
|
||||||
use rocket::futures::future;
|
use rocket::futures::future;
|
||||||
use rocket::serde::{json::json, Serialize};
|
use rocket::serde::{json::json, Serialize};
|
||||||
@@ -29,8 +29,8 @@ pub struct PostOutput {
|
|||||||
is_tmp: bool,
|
is_tmp: bool,
|
||||||
n_attentions: i32,
|
n_attentions: i32,
|
||||||
n_comments: i32,
|
n_comments: i32,
|
||||||
create_time: NaiveDateTime,
|
create_time: DateTime<Utc>,
|
||||||
last_comment_time: NaiveDateTime,
|
last_comment_time: DateTime<Utc>,
|
||||||
allow_search: bool,
|
allow_search: bool,
|
||||||
is_reported: Option<bool>,
|
is_reported: Option<bool>,
|
||||||
comments: Option<Vec<CommentOutput>>,
|
comments: Option<Vec<CommentOutput>>,
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#![allow(clippy::all)]
|
#![allow(clippy::all)]
|
||||||
|
|
||||||
use chrono::NaiveDateTime;
|
|
||||||
use diesel::{insert_into, ExpressionMethods, QueryDsl, QueryResult, RunQueryDsl};
|
use diesel::{insert_into, ExpressionMethods, QueryDsl, QueryResult, RunQueryDsl};
|
||||||
|
|
||||||
use crate::db_conn::Db;
|
use crate::db_conn::Db;
|
||||||
@@ -21,7 +20,12 @@ macro_rules! get_multi {
|
|||||||
($table:ident) => {
|
($table:ident) => {
|
||||||
pub async fn get_multi(db: &Db, ids: Vec<i32>) -> QueryResult<Vec<Self>> {
|
pub async fn get_multi(db: &Db, ids: Vec<i32>) -> QueryResult<Vec<Self>> {
|
||||||
// can use eq(any()) for postgres
|
// can use eq(any()) for postgres
|
||||||
db.run(move |c| $table::table.filter($table::id.eq_any(ids)).load(c))
|
db.run(move |c| {
|
||||||
|
$table::table
|
||||||
|
.filter($table::id.eq_any(ids))
|
||||||
|
.order($table::id.desc())
|
||||||
|
.load(c)
|
||||||
|
})
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -41,7 +45,21 @@ macro_rules! set_deleted {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Queryable, Identifiable)]
|
use chrono::{offset::Utc, DateTime};
|
||||||
|
#[derive(Queryable, Insertable)]
|
||||||
|
pub struct Comment {
|
||||||
|
pub id: i32,
|
||||||
|
pub author_hash: String,
|
||||||
|
pub author_title: String,
|
||||||
|
pub is_tmp: bool,
|
||||||
|
pub content: String,
|
||||||
|
pub create_time: DateTime<Utc>,
|
||||||
|
pub is_deleted: bool,
|
||||||
|
pub allow_search: bool,
|
||||||
|
pub post_id: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Queryable, Insertable)]
|
||||||
pub struct Post {
|
pub struct Post {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
pub author_hash: String,
|
pub author_hash: String,
|
||||||
@@ -51,14 +69,22 @@ pub struct Post {
|
|||||||
pub is_tmp: bool,
|
pub is_tmp: bool,
|
||||||
pub n_attentions: i32,
|
pub n_attentions: i32,
|
||||||
pub n_comments: i32,
|
pub n_comments: i32,
|
||||||
pub create_time: NaiveDateTime,
|
pub create_time: DateTime<Utc>,
|
||||||
pub last_comment_time: NaiveDateTime,
|
pub last_comment_time: DateTime<Utc>,
|
||||||
pub is_deleted: bool,
|
pub is_deleted: bool,
|
||||||
pub is_reported: bool,
|
pub is_reported: bool,
|
||||||
pub hot_score: i32,
|
pub hot_score: i32,
|
||||||
pub allow_search: bool,
|
pub allow_search: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Queryable, Insertable)]
|
||||||
|
pub struct User {
|
||||||
|
pub id: i32,
|
||||||
|
pub name: String,
|
||||||
|
pub token: String,
|
||||||
|
pub is_admin: bool,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Insertable)]
|
#[derive(Insertable)]
|
||||||
#[table_name = "posts"]
|
#[table_name = "posts"]
|
||||||
pub struct NewPost {
|
pub struct NewPost {
|
||||||
@@ -144,14 +170,6 @@ impl Post {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Queryable, Identifiable)]
|
|
||||||
pub struct User {
|
|
||||||
pub id: i32,
|
|
||||||
pub name: String,
|
|
||||||
pub token: String,
|
|
||||||
pub is_admin: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl User {
|
impl User {
|
||||||
pub async fn get_by_token(db: &Db, token: &str) -> Option<Self> {
|
pub async fn get_by_token(db: &Db, token: &str) -> Option<Self> {
|
||||||
let token = token.to_string();
|
let token = token.to_string();
|
||||||
@@ -161,19 +179,6 @@ impl User {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Queryable, Identifiable)]
|
|
||||||
pub struct Comment {
|
|
||||||
pub id: i32,
|
|
||||||
pub author_hash: String,
|
|
||||||
pub author_title: String,
|
|
||||||
pub is_tmp: bool,
|
|
||||||
pub content: String,
|
|
||||||
pub create_time: NaiveDateTime,
|
|
||||||
pub is_deleted: bool,
|
|
||||||
pub allow_search: bool,
|
|
||||||
pub post_id: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Insertable)]
|
#[derive(Insertable)]
|
||||||
#[table_name = "comments"]
|
#[table_name = "comments"]
|
||||||
pub struct NewComment {
|
pub struct NewComment {
|
||||||
@@ -200,7 +205,12 @@ impl Comment {
|
|||||||
|
|
||||||
pub async fn gets_by_post_id(db: &Db, post_id: i32) -> QueryResult<Vec<Self>> {
|
pub async fn gets_by_post_id(db: &Db, post_id: i32) -> QueryResult<Vec<Self>> {
|
||||||
let pid = post_id;
|
let pid = post_id;
|
||||||
db.run(move |c| comments::table.filter(comments::post_id.eq(pid)).load(c))
|
db.run(move |c| {
|
||||||
|
comments::table
|
||||||
|
.filter(comments::post_id.eq(pid))
|
||||||
|
.order(comments::id)
|
||||||
|
.load(c)
|
||||||
|
})
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ table! {
|
|||||||
author_title -> Varchar,
|
author_title -> Varchar,
|
||||||
is_tmp -> Bool,
|
is_tmp -> Bool,
|
||||||
content -> Text,
|
content -> Text,
|
||||||
create_time -> Timestamp,
|
create_time -> Timestamptz,
|
||||||
is_deleted -> Bool,
|
is_deleted -> Bool,
|
||||||
allow_search -> Bool,
|
allow_search -> Bool,
|
||||||
post_id -> Int4,
|
post_id -> Int4,
|
||||||
@@ -22,8 +22,8 @@ table! {
|
|||||||
is_tmp -> Bool,
|
is_tmp -> Bool,
|
||||||
n_attentions -> Int4,
|
n_attentions -> Int4,
|
||||||
n_comments -> Int4,
|
n_comments -> Int4,
|
||||||
create_time -> Timestamp,
|
create_time -> Timestamptz,
|
||||||
last_comment_time -> Timestamp,
|
last_comment_time -> Timestamptz,
|
||||||
is_deleted -> Bool,
|
is_deleted -> Bool,
|
||||||
is_reported -> Bool,
|
is_reported -> Bool,
|
||||||
hot_score -> Int4,
|
hot_score -> Int4,
|
||||||
|
|||||||
Reference in New Issue
Block a user