Skip to content

Commit

Permalink
fix(core): add commit to transaction call
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammisuli committed Oct 28, 2024
1 parent af108c3 commit e5533b1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 24 deletions.
8 changes: 6 additions & 2 deletions packages/nx/src/native/db/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ impl NxDbConnection {
let transaction = retry_db_operation_when_busy!(self.conn.transaction())
.map_err(|e| anyhow::anyhow!("DB transaction error: {:?}", e))?;

transaction_operation(&transaction)
.map_err(|e| anyhow::anyhow!("DB transaction operation error: {:?}", e))
let result = transaction_operation(&transaction)
.map_err(|e| anyhow::anyhow!("DB transaction operation error: {:?}", e))?;

transaction.commit().map_err(|e| anyhow::anyhow!("DB transaction commit error: {:?}", e))?;

Ok(result)
}

pub fn query_row<T, P, F>(&self, sql: &str, params: P, f: F) -> Result<Option<T>>
Expand Down
2 changes: 2 additions & 0 deletions packages/nx/src/native/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ use std::fs::create_dir_all;
use std::path::PathBuf;
use std::process;
use tracing::{trace, trace_span};
use crate::native::logger::enable_logger;

#[napi]
pub fn connect_to_nx_db(
cache_dir: String,
nx_version: String,
db_name: Option<String>,
) -> anyhow::Result<External<NxDbConnection>> {
enable_logger();
let cache_dir_buf = PathBuf::from(cache_dir);
let db_path = cache_dir_buf.join(format!("{}.db", db_name.unwrap_or_else(get_machine_id)));
create_dir_all(cache_dir_buf)?;
Expand Down
9 changes: 5 additions & 4 deletions packages/nx/src/native/tasks/details.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::native::db::connection::NxDbConnection;
use napi::bindgen_prelude::*;
use rusqlite::params;
use tracing::trace;

#[napi(object)]
#[derive(Default, Clone)]
#[derive(Default, Clone, Debug)]
pub struct HashedTask {
pub hash: String,
pub project: String,
Expand Down Expand Up @@ -43,11 +44,11 @@ impl TaskDetails {

#[napi]
pub fn record_task_details(&mut self, tasks: Vec<HashedTask>) -> anyhow::Result<()> {
trace!("Recording task details");
self.db.transaction(|conn| {
let mut stmt = conn.prepare("INSERT OR REPLACE INTO task_details (hash, project, target, configuration) VALUES (?1, ?2, ?3, ?4)")?;
for task in tasks.iter() {
conn.execute(
"INSERT OR REPLACE INTO task_details (hash, project, target, configuration)
VALUES (?1, ?2, ?3, ?4)",
stmt.execute(
params![task.hash, task.project, task.target, task.configuration],
)?;
}
Expand Down
39 changes: 21 additions & 18 deletions packages/nx/src/native/tasks/task_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rusqlite::vtab::array;
use rusqlite::{params, types::Value};
use std::collections::HashMap;
use std::rc::Rc;
use tracing::trace;

#[napi(object)]
pub struct TaskRun {
Expand Down Expand Up @@ -54,24 +55,26 @@ impl NxTaskHistory {
}

#[napi]
pub fn record_task_runs(&self, task_runs: Vec<TaskRun>) -> anyhow::Result<()> {
for task_run in task_runs.iter() {
self.db
.execute(
"
INSERT INTO task_history
(hash, status, code, start, end)
VALUES (?1, ?2, ?3, ?4, ?5)",
params![
task_run.hash,
task_run.status,
task_run.code,
task_run.start,
task_run.end
],
)
.map_err(anyhow::Error::from)?;
}
pub fn record_task_runs(&mut self, task_runs: Vec<TaskRun>) -> anyhow::Result<()> {
trace!("Recording task runs");
self.db.transaction(|conn| {
let mut stmt = conn.prepare(
"INSERT OR REPLACE INTO task_history
(hash, status, code, start, end)
VALUES (?1, ?2, ?3, ?4, ?5)",
)?;
for task_run in task_runs.iter() {
stmt.execute(params![
task_run.hash,
task_run.status,
task_run.code,
task_run.start,
task_run.end
])
.inspect_err(|e| trace!("Error trying to insert {:?}: {:?}", &task_run.hash, e))?;
}
Ok(())
})?;
Ok(())
}

Expand Down

0 comments on commit e5533b1

Please sign in to comment.