feat: time of models' of creation and update #22

Merged
matous-volf merged 2 commits from feat/model-created-updated-at into main 2024-09-06 16:09:10 +00:00
4 changed files with 25 additions and 0 deletions
Showing only changes of commit 32e25a5f8b - Show all commits

View File

@ -0,0 +1,5 @@
-- This file should undo anything in `up.sql`
ALTER TABLE "projects" DROP COLUMN "created_at";
ALTER TABLE "projects" DROP COLUMN "updated_at";

View File

@ -0,0 +1,7 @@
-- Your SQL goes here
ALTER TABLE "projects" ADD COLUMN "created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE "projects" ADD COLUMN "updated_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
coderabbitai[bot] commented 2024-09-06 16:01:59 +00:00 (Migrated from github.com)
Review

Change suggested: Default value for updated_at.

The SQL command adds the updated_at column with CURRENT_TIMESTAMP as the default. Consider using a different strategy for the default value, as this column should ideally reflect the last update time, not the creation time.

**Change suggested: Default value for `updated_at`.** The SQL command adds the `updated_at` column with `CURRENT_TIMESTAMP` as the default. Consider using a different strategy for the default value, as this column should ideally reflect the last update time, not the creation time. <!-- This is an auto-generated comment by CodeRabbit -->
SELECT diesel_manage_updated_at('projects');

View File

@ -1,3 +1,4 @@
use chrono::NaiveDateTime;
use crate::schema::projects; use crate::schema::projects;
use diesel::prelude::*; use diesel::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -12,6 +13,8 @@ const TITLE_LENGTH_MAX: u64 = 255;
pub struct Project { pub struct Project {
id: i32, id: i32,
title: String, title: String,
created_at: NaiveDateTime,
updated_at: NaiveDateTime,
} }
impl Project { impl Project {
@ -22,6 +25,14 @@ impl Project {
pub fn title(&self) -> &str { pub fn title(&self) -> &str {
&self.title &self.title
} }
pub fn created_at(&self) -> NaiveDateTime {
self.created_at
}
pub fn updated_at(&self) -> NaiveDateTime {
self.updated_at
}
} }
#[derive(Insertable, Serialize, Deserialize, Validate, Clone, Debug)] #[derive(Insertable, Serialize, Deserialize, Validate, Clone, Debug)]

View File

@ -4,6 +4,8 @@ diesel::table! {
projects (id) { projects (id) {
id -> Int4, id -> Int4,
title -> Text, title -> Text,
created_at -> Timestamp,
updated_at -> Timestamp,
} }
} }