Skip to content

Commit

Permalink
fix(core): allow creating a db cache without linking task details (#2…
Browse files Browse the repository at this point in the history
…8023)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

The db cache does not work without task details.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

The db cache works without task details.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
FrozenPandaz authored Sep 20, 2024
1 parent 7af099c commit a510b36
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
28 changes: 23 additions & 5 deletions packages/nx/src/native/cache/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct NxCache {
workspace_root: PathBuf,
cache_path: PathBuf,
db: External<Connection>,
link_task_details: bool,
}

#[napi]
Expand All @@ -35,6 +36,7 @@ impl NxCache {
workspace_root: String,
cache_path: String,
db_connection: External<Connection>,
link_task_details: Option<bool>,
) -> anyhow::Result<Self> {
let cache_path = PathBuf::from(&cache_path);

Expand All @@ -46,6 +48,7 @@ impl NxCache {
workspace_root: PathBuf::from(workspace_root),
cache_directory: cache_path.to_normalized_string(),
cache_path,
link_task_details: link_task_details.unwrap_or(true)
};

r.setup()?;
Expand All @@ -54,18 +57,32 @@ impl NxCache {
}

fn setup(&self) -> anyhow::Result<()> {
self.db
.execute_batch(
"BEGIN;
let query = if self.link_task_details {
"BEGIN;
CREATE TABLE IF NOT EXISTS cache_outputs (
hash TEXT PRIMARY KEY NOT NULL,
code INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
accessed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (hash) REFERENCES task_details (hash)
);
COMMIT;
",
COMMIT;
"
} else {
"BEGIN;
CREATE TABLE IF NOT EXISTS cache_outputs (
hash TEXT PRIMARY KEY NOT NULL,
code INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
accessed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
COMMIT;
"
};

self.db
.execute_batch(
query,
)
.map_err(anyhow::Error::from)
}
Expand Down Expand Up @@ -115,6 +132,7 @@ impl NxCache {
outputs: Vec<String>,
code: i16,
) -> anyhow::Result<()> {
trace!("PUT {}", &hash);
let task_dir = self.cache_path.join(&hash);

// Remove the task directory
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/native/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export declare class ImportResult {

export declare class NxCache {
cacheDirectory: string
constructor(workspaceRoot: string, cachePath: string, dbConnection: ExternalObject<Connection>)
constructor(workspaceRoot: string, cachePath: string, dbConnection: ExternalObject<Connection>, linkTaskDetails?: boolean | undefined | null)
get(hash: string): CachedResult | null
put(hash: string, terminalOutput: string, outputs: Array<string>, code: number): void
applyRemoteCacheResults(hash: string, result: CachedResult): void
Expand Down

0 comments on commit a510b36

Please sign in to comment.