From 5f2af2123af4a6a5b6dca63988516228cf682cc2 Mon Sep 17 00:00:00 2001 From: Louis Knight-Webb Date: Mon, 16 Jun 2025 17:24:48 -0400 Subject: [PATCH] Timestamps in PG --- backend/src/models/project.rs | 15 ++++----------- backend/src/models/task.rs | 17 +++++------------ backend/src/models/task_attempt.rs | 10 +++------- backend/src/models/task_attempt_activity.rs | 18 ++++++------------ backend/src/models/user.rs | 14 ++++---------- 5 files changed, 22 insertions(+), 52 deletions(-) diff --git a/backend/src/models/project.rs b/backend/src/models/project.rs index 61674ba8..f8833bc7 100644 --- a/backend/src/models/project.rs +++ b/backend/src/models/project.rs @@ -74,32 +74,25 @@ impl Project { } pub async fn create(pool: &PgPool, data: &CreateProject, owner_id: Uuid, project_id: Uuid) -> Result { - let now = Utc::now(); - sqlx::query_as!( Project, - "INSERT INTO projects (id, name, git_repo_path, owner_id, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id, name, git_repo_path, owner_id, created_at, updated_at", + "INSERT INTO projects (id, name, git_repo_path, owner_id) VALUES ($1, $2, $3, $4) RETURNING id, name, git_repo_path, owner_id, created_at, updated_at", project_id, data.name, data.git_repo_path, - owner_id, - now, - now + owner_id ) .fetch_one(pool) .await } pub async fn update(pool: &PgPool, id: Uuid, name: String, git_repo_path: String) -> Result { - let now = Utc::now(); - sqlx::query_as!( Project, - "UPDATE projects SET name = $2, git_repo_path = $3, updated_at = $4 WHERE id = $1 RETURNING id, name, git_repo_path, owner_id, created_at, updated_at", + "UPDATE projects SET name = $2, git_repo_path = $3 WHERE id = $1 RETURNING id, name, git_repo_path, owner_id, created_at, updated_at", id, name, - git_repo_path, - now + git_repo_path ) .fetch_one(pool) .await diff --git a/backend/src/models/task.rs b/backend/src/models/task.rs index b1842e98..ebab2bb3 100644 --- a/backend/src/models/task.rs +++ b/backend/src/models/task.rs @@ -84,40 +84,33 @@ impl Task { } pub async fn create(pool: &PgPool, data: &CreateTask, task_id: Uuid) -> Result { - let now = Utc::now(); - sqlx::query_as!( Task, - r#"INSERT INTO tasks (id, project_id, title, description, status, created_at, updated_at) - VALUES ($1, $2, $3, $4, $5, $6, $7) + r#"INSERT INTO tasks (id, project_id, title, description, status) + VALUES ($1, $2, $3, $4, $5) RETURNING id, project_id, title, description, status as "status!: TaskStatus", created_at, updated_at"#, task_id, data.project_id, data.title, data.description, - TaskStatus::Todo as TaskStatus, - now, - now + TaskStatus::Todo as TaskStatus ) .fetch_one(pool) .await } pub async fn update(pool: &PgPool, id: Uuid, project_id: Uuid, title: String, description: Option, status: TaskStatus) -> Result { - let now = Utc::now(); - sqlx::query_as!( Task, r#"UPDATE tasks - SET title = $3, description = $4, status = $5, updated_at = $6 + SET title = $3, description = $4, status = $5 WHERE id = $1 AND project_id = $2 RETURNING id, project_id, title, description, status as "status!: TaskStatus", created_at, updated_at"#, id, project_id, title, description, - status as TaskStatus, - now + status as TaskStatus ) .fetch_one(pool) .await diff --git a/backend/src/models/task_attempt.rs b/backend/src/models/task_attempt.rs index 0da2a59c..c9cce356 100644 --- a/backend/src/models/task_attempt.rs +++ b/backend/src/models/task_attempt.rs @@ -96,8 +96,6 @@ impl TaskAttempt { } pub async fn create(pool: &PgPool, data: &CreateTaskAttempt, attempt_id: Uuid) -> Result { - let now = Utc::now(); - // First, get the task to get the project_id let task = Task::find_by_id(pool, data.task_id) .await? @@ -124,16 +122,14 @@ impl TaskAttempt { // Insert the record into the database let task_attempt = sqlx::query_as!( TaskAttempt, - r#"INSERT INTO task_attempts (id, task_id, worktree_path, base_commit, merge_commit, created_at, updated_at) - VALUES ($1, $2, $3, $4, $5, $6, $7) + r#"INSERT INTO task_attempts (id, task_id, worktree_path, base_commit, merge_commit) + VALUES ($1, $2, $3, $4, $5) RETURNING id, task_id, worktree_path, base_commit, merge_commit, created_at, updated_at"#, attempt_id, data.task_id, data.worktree_path, data.base_commit, - data.merge_commit, - now, - now + data.merge_commit ) .fetch_one(pool) .await?; diff --git a/backend/src/models/task_attempt_activity.rs b/backend/src/models/task_attempt_activity.rs index b3adb1c2..9b953ede 100644 --- a/backend/src/models/task_attempt_activity.rs +++ b/backend/src/models/task_attempt_activity.rs @@ -39,34 +39,28 @@ impl TaskAttemptActivity { } pub async fn create(pool: &PgPool, data: &CreateTaskAttemptActivity, activity_id: Uuid, status: TaskAttemptStatus) -> Result { - let now = Utc::now(); - sqlx::query_as!( TaskAttemptActivity, - r#"INSERT INTO task_attempt_activities (id, task_attempt_id, status, note, created_at) - VALUES ($1, $2, $3, $4, $5) + r#"INSERT INTO task_attempt_activities (id, task_attempt_id, status, note) + VALUES ($1, $2, $3, $4) RETURNING id, task_attempt_id, status as "status!: TaskAttemptStatus", note, created_at"#, activity_id, data.task_attempt_id, status as TaskAttemptStatus, - data.note, - now + data.note ) .fetch_one(pool) .await } pub async fn create_initial(pool: &PgPool, attempt_id: Uuid, activity_id: Uuid) -> Result<(), sqlx::Error> { - let now = Utc::now(); - sqlx::query!( - r#"INSERT INTO task_attempt_activities (id, task_attempt_id, status, note, created_at) - VALUES ($1, $2, $3, $4, $5)"#, + r#"INSERT INTO task_attempt_activities (id, task_attempt_id, status, note) + VALUES ($1, $2, $3, $4)"#, activity_id, attempt_id, TaskAttemptStatus::Init as TaskAttemptStatus, - Option::::None, - now + Option::::None ) .execute(pool) .await?; diff --git a/backend/src/models/user.rs b/backend/src/models/user.rs index 51d5c01e..d6c58a32 100644 --- a/backend/src/models/user.rs +++ b/backend/src/models/user.rs @@ -101,34 +101,28 @@ impl User { } pub async fn create(pool: &PgPool, data: &CreateUser, password_hash: String, user_id: Uuid) -> Result { - let now = Utc::now(); let is_admin = data.is_admin.unwrap_or(false); sqlx::query_as!( User, - "INSERT INTO users (id, email, password_hash, is_admin, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id, email, password_hash, is_admin, created_at, updated_at", + "INSERT INTO users (id, email, password_hash, is_admin) VALUES ($1, $2, $3, $4) RETURNING id, email, password_hash, is_admin, created_at, updated_at", user_id, data.email, password_hash, - is_admin, - now, - now + is_admin ) .fetch_one(pool) .await } pub async fn update(pool: &PgPool, id: Uuid, email: String, password_hash: String, is_admin: bool) -> Result { - let now = Utc::now(); - sqlx::query_as!( User, - "UPDATE users SET email = $2, password_hash = $3, is_admin = $4, updated_at = $5 WHERE id = $1 RETURNING id, email, password_hash, is_admin, created_at, updated_at", + "UPDATE users SET email = $2, password_hash = $3, is_admin = $4 WHERE id = $1 RETURNING id, email, password_hash, is_admin, created_at, updated_at", id, email, password_hash, - is_admin, - now + is_admin ) .fetch_one(pool) .await