Skip to content

Commit

Permalink
fix(core): disable the db by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammisuli committed Nov 1, 2024
1 parent 9adb9e0 commit 4a153e2
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/nx/src/hasher/hash-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { ProjectGraph } from '../config/project-graph';
import { NxJsonConfiguration } from '../config/nx-json';
import { readNxJson } from '../config/nx-json';
import { HashedTask, IS_WASM, TaskDetails } from '../native';
import { getDbConnection } from '../utils/db-connection';
import { databaseSupportEnabled, getDbConnection } from '../utils/database';

let taskDetails: TaskDetails;

export function getTaskDetails(): TaskDetails | null {
// TODO: Remove when wasm supports sqlite
if (process.env.NX_DISABLE_DB === 'true' || IS_WASM) {
if (!databaseSupportEnabled() || IS_WASM) {
return null;
}
if (!taskDetails) {
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/native/tests/cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TaskDetails, NxCache } from '../index';
import { join } from 'path';
import { TempFs } from '../../internal-testing-utils/temp-fs';
import { rmSync } from 'fs';
import { getDbConnection } from '../../utils/db-connection';
import { getDbConnection } from '../../utils/database';
import { randomBytes } from 'crypto';

describe('Cache', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/native/tests/task_history.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TaskDetails, NxTaskHistory } from '../index';
import { join } from 'path';
import { TempFs } from '../../internal-testing-utils/temp-fs';
import { rmSync } from 'fs';
import { getDbConnection } from '../../utils/db-connection';
import { getDbConnection } from '../../utils/database';
import { randomBytes } from 'crypto';

const dbOutputFolder = 'temp-db-task';
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/tasks-runner/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { cacheDir } from '../utils/cache-directory';
import { Task } from '../config/task-graph';
import { machineId } from 'node-machine-id';
import { NxCache, CachedResult as NativeCacheResult, IS_WASM } from '../native';
import { getDbConnection } from '../utils/db-connection';
import { databaseSupportEnabled, getDbConnection } from '../utils/database';
import { isNxCloudUsed } from '../utils/nx-cloud-utils';
import { NxJsonConfiguration, readNxJson } from '../config/nx-json';
import { verifyOrUpdateNxCloudClient } from '../nx-cloud/update-manager';
Expand All @@ -32,7 +32,7 @@ export type TaskWithCachedResult = { task: Task; cachedResult: CachedResult };
export function dbCacheEnabled(nxJson: NxJsonConfiguration = readNxJson()) {
return (
!IS_WASM &&
process.env.NX_DISABLE_DB !== 'true' &&
databaseSupportEnabled() &&
nxJson.useLegacyCache !== true &&
process.env.NX_DB_CACHE !== 'false'
);
Expand Down
3 changes: 2 additions & 1 deletion packages/nx/src/tasks-runner/run-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import { shouldStreamOutput } from './utils';
import chalk = require('chalk');
import type { Observable } from 'rxjs';
import { printPowerpackLicense } from '../utils/powerpack';
import { databaseSupportEnabled } from '../utils/database';

async function getTerminalOutputLifeCycle(
initiatingProject: string,
Expand Down Expand Up @@ -711,7 +712,7 @@ function constructLifeCycles(lifeCycle: LifeCycle): LifeCycle[] {
}
if (!isNxCloudUsed(readNxJson())) {
lifeCycles.push(
process.env.NX_DISABLE_DB !== 'true' && !IS_WASM
databaseSupportEnabled() && !IS_WASM
? new TaskHistoryLifeCycle()
: new LegacyTaskHistoryLifeCycle()
);
Expand Down
16 changes: 16 additions & 0 deletions packages/nx/src/utils/database.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { databaseSupportEnabled } from './database';

describe('test if database is enabled', () => {
it('should be disabled by default', () => {
process.env.NX_DISABLE_DB = undefined;
expect(databaseSupportEnabled()).toBe(false);
});
it('should be disabled if NX_DISABLE_DB is explicitly set to `true`', () => {
process.env.NX_DISABLE_DB = 'true';
expect(databaseSupportEnabled()).toBe(false);
});
it('should be enabled if NX_DISABLE_DB is explicitly set to `false`', () => {
process.env.NX_DISABLE_DB = 'false';
expect(databaseSupportEnabled()).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ function getEntryOrSet<TKey, TVal>(
map.set(key, val);
return val;
}

// database is disabled by default
export function databaseSupportEnabled() {
return process.env.NX_DISABLE_DB === 'false';
}
4 changes: 2 additions & 2 deletions packages/nx/src/utils/task-history.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { daemonClient } from '../daemon/client/client';
import { isOnDaemon } from '../daemon/is-on-daemon';
import { IS_WASM, NxTaskHistory, TaskRun, TaskTarget } from '../native';
import { getDbConnection } from './db-connection';
import { databaseSupportEnabled, getDbConnection } from './database';

export class TaskHistory {
taskHistory = new NxTaskHistory(getDbConnection());
Expand Down Expand Up @@ -42,7 +42,7 @@ let taskHistory: TaskHistory;
* @returns singleton instance of TaskHistory, null if database is disabled or WASM is enabled
*/
export function getTaskHistory(): TaskHistory | null {
if (process.env.NX_DISABLE_DB === 'true' || IS_WASM) {
if (!databaseSupportEnabled() || IS_WASM) {
return null;
}

Expand Down

0 comments on commit 4a153e2

Please sign in to comment.