Browse Source

fix: use timestamptz

master
hole-thu 3 years ago
parent
commit
cfe39f7645
  1. 1
      README.md
  2. 4
      migrations/postgres/2022-03-21-155550_create_posts/up.sql
  3. 2
      migrations/postgres/2022-03-21-164718_create_comments/up.sql
  4. 4
      src/api/comment.rs
  5. 6
      src/api/post.rs
  6. 68
      src/models.rs
  7. 6
      src/schema.rs

1
README.md

@ -21,5 +21,4 @@ CREATE EXTENSION pg_trgm;
```
$ diesel run
$ python3 tools/migdb.py
```

4
migrations/postgres/2022-03-21-155550_create_posts/up.sql

@ -8,8 +8,8 @@ CREATE TABLE posts (
is_tmp BOOLEAN NOT NULL DEFAULT FALSE,
n_attentions INTEGER NOT NULL DEFAULT 0,
n_comments INTEGER NOT NULL DEFAULT 0,
create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
last_comment_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
create_time TIMESTAMPTZ NOT NULL DEFAULT now(),
last_comment_time TIMESTAMPTZ NOT NULL DEFAULT now(),
is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
is_reported BOOLEAN NOT NULL DEFAULT FALSE,
hot_score INTEGER NOT NULL DEFAULT 0,

2
migrations/postgres/2022-03-21-164718_create_comments/up.sql

@ -5,7 +5,7 @@ CREATE TABLE comments (
author_title VARCHAR NOT NULL DEFAULT '',
is_tmp BOOLEAN NOT NULL DEFAULT FALSE,
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,
allow_search BOOLEAN NOT NULL DEFAULT FALSE,
post_id INTEGER NOT NULL REFERENCES posts(id)

4
src/api/comment.rs

@ -3,7 +3,7 @@ use crate::db_conn::Db;
use crate::models::*;
use crate::rds_conn::RdsConn;
use crate::rds_models::*;
use chrono::NaiveDateTime;
use chrono::{offset::Utc, DateTime};
use rocket::form::Form;
use rocket::serde::{
json::{json, Value},
@ -27,7 +27,7 @@ pub struct CommentOutput {
can_del: bool,
name_id: i32,
is_tmp: bool,
create_time: NaiveDateTime,
create_time: DateTime<Utc>,
// for old version frontend
timestamp: i64,
}

6
src/api/post.rs

@ -4,7 +4,7 @@ use crate::db_conn::Db;
use crate::models::*;
use crate::rds_conn::RdsConn;
use crate::rds_models::*;
use chrono::NaiveDateTime;
use chrono::{offset::Utc, DateTime};
use rocket::form::Form;
use rocket::futures::future;
use rocket::serde::{json::json, Serialize};
@ -29,8 +29,8 @@ pub struct PostOutput {
is_tmp: bool,
n_attentions: i32,
n_comments: i32,
create_time: NaiveDateTime,
last_comment_time: NaiveDateTime,
create_time: DateTime<Utc>,
last_comment_time: DateTime<Utc>,
allow_search: bool,
is_reported: Option<bool>,
comments: Option<Vec<CommentOutput>>,

68
src/models.rs

@ -1,6 +1,5 @@
#![allow(clippy::all)]
use chrono::NaiveDateTime;
use diesel::{insert_into, ExpressionMethods, QueryDsl, QueryResult, RunQueryDsl};
use crate::db_conn::Db;
@ -21,8 +20,13 @@ macro_rules! get_multi {
($table:ident) => {
pub async fn get_multi(db: &Db, ids: Vec<i32>) -> QueryResult<Vec<Self>> {
// can use eq(any()) for postgres
db.run(move |c| $table::table.filter($table::id.eq_any(ids)).load(c))
.await
db.run(move |c| {
$table::table
.filter($table::id.eq_any(ids))
.order($table::id.desc())
.load(c)
})
.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 id: i32,
pub author_hash: String,
@ -51,14 +69,22 @@ pub struct Post {
pub is_tmp: bool,
pub n_attentions: i32,
pub n_comments: i32,
pub create_time: NaiveDateTime,
pub last_comment_time: NaiveDateTime,
pub create_time: DateTime<Utc>,
pub last_comment_time: DateTime<Utc>,
pub is_deleted: bool,
pub is_reported: bool,
pub hot_score: i32,
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)]
#[table_name = "posts"]
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 {
pub async fn get_by_token(db: &Db, token: &str) -> Option<Self> {
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)]
#[table_name = "comments"]
pub struct NewComment {
@ -200,7 +205,12 @@ impl Comment {
pub async fn gets_by_post_id(db: &Db, post_id: i32) -> QueryResult<Vec<Self>> {
let pid = post_id;
db.run(move |c| comments::table.filter(comments::post_id.eq(pid)).load(c))
.await
db.run(move |c| {
comments::table
.filter(comments::post_id.eq(pid))
.order(comments::id)
.load(c)
})
.await
}
}

6
src/schema.rs

@ -5,7 +5,7 @@ table! {
author_title -> Varchar,
is_tmp -> Bool,
content -> Text,
create_time -> Timestamp,
create_time -> Timestamptz,
is_deleted -> Bool,
allow_search -> Bool,
post_id -> Int4,
@ -22,8 +22,8 @@ table! {
is_tmp -> Bool,
n_attentions -> Int4,
n_comments -> Int4,
create_time -> Timestamp,
last_comment_time -> Timestamp,
create_time -> Timestamptz,
last_comment_time -> Timestamptz,
is_deleted -> Bool,
is_reported -> Bool,
hot_score -> Int4,

Loading…
Cancel
Save