Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): allow creating a db cache without linking task details #28023

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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