Timestamps in PG

This commit is contained in:
Louis Knight-Webb
2025-06-16 17:24:48 -04:00
parent 7f7592e5bf
commit 5f2af2123a
5 changed files with 22 additions and 52 deletions

View File

@@ -74,32 +74,25 @@ impl Project {
}
pub async fn create(pool: &PgPool, data: &CreateProject, owner_id: Uuid, project_id: Uuid) -> Result<Self, sqlx::Error> {
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<Self, sqlx::Error> {
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

View File

@@ -84,40 +84,33 @@ impl Task {
}
pub async fn create(pool: &PgPool, data: &CreateTask, task_id: Uuid) -> Result<Self, sqlx::Error> {
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<String>, status: TaskStatus) -> Result<Self, sqlx::Error> {
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

View File

@@ -96,8 +96,6 @@ impl TaskAttempt {
}
pub async fn create(pool: &PgPool, data: &CreateTaskAttempt, attempt_id: Uuid) -> Result<Self, TaskAttemptError> {
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?;

View File

@@ -39,34 +39,28 @@ impl TaskAttemptActivity {
}
pub async fn create(pool: &PgPool, data: &CreateTaskAttemptActivity, activity_id: Uuid, status: TaskAttemptStatus) -> Result<Self, sqlx::Error> {
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::<String>::None,
now
Option::<String>::None
)
.execute(pool)
.await?;

View File

@@ -101,34 +101,28 @@ impl User {
}
pub async fn create(pool: &PgPool, data: &CreateUser, password_hash: String, user_id: Uuid) -> Result<Self, sqlx::Error> {
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<Self, sqlx::Error> {
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