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

refactor: Use string ids on Credentials, Workflows, Tags, and Executions DB entities #5041

Merged
merged 22 commits into from
Jan 2, 2023
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
8 changes: 3 additions & 5 deletions packages/cli/src/ActiveExecutions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ export class ActiveExecutions {
fullExecutionData.retryOf = executionData.retryOf.toString();
}

if (
executionData.workflowData.id !== undefined &&
WorkflowHelpers.isWorkflowIdValid(executionData.workflowData.id.toString())
) {
fullExecutionData.workflowId = executionData.workflowData.id.toString();
const workflowId = executionData.workflowData.id;
if (workflowId !== undefined && WorkflowHelpers.isWorkflowIdValid(workflowId)) {
fullExecutionData.workflowId = workflowId;
}

const execution = ResponseHelper.flattenExecutionData(fullExecutionData);
Expand Down
35 changes: 15 additions & 20 deletions packages/cli/src/ActiveWorkflowRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import {
IActivationError,
IQueuedWorkflowActivations,
IResponseCallbackData,
IWebhookDb,
IWorkflowDb,
IWorkflowExecutionDataProcess,
} from '@/Interfaces';
Expand All @@ -53,7 +52,8 @@ import * as WorkflowExecuteAdditionalData from '@/WorkflowExecuteAdditionalData'

import config from '@/config';
import { User } from '@db/entities/User';
import { WorkflowEntity } from '@db/entities/WorkflowEntity';
import type { WorkflowEntity } from '@db/entities/WorkflowEntity';
import type { WebhookEntity } from '@db/entities/WebhookEntity';
import * as ActiveExecutions from '@/ActiveExecutions';
import { createErrorExecution } from '@/GenericHelpers';
import { WORKFLOW_REACTIVATE_INITIAL_TIMEOUT, WORKFLOW_REACTIVATE_MAX_TIMEOUT } from '@/constants';
Expand Down Expand Up @@ -114,7 +114,7 @@ export class ActiveWorkflowRunner {
workflowId: workflowData.id,
});
try {
await this.add(workflowData.id.toString(), 'init', workflowData);
await this.add(workflowData.id, 'init', workflowData);
Logger.verbose(`Successfully started workflow "${workflowData.name}"`, {
workflowName: workflowData.name,
workflowId: workflowData.id,
Expand Down Expand Up @@ -165,10 +165,7 @@ export class ActiveWorkflowRunner {
}

const activeWorkflows = await this.getActiveWorkflows();
activeWorkflowIds = [
...activeWorkflowIds,
...activeWorkflows.map((workflow) => workflow.id.toString()),
];
activeWorkflowIds = [...activeWorkflowIds, ...activeWorkflows.map((workflow) => workflow.id)];

// Make sure IDs are unique
activeWorkflowIds = Array.from(new Set(activeWorkflowIds));
Expand Down Expand Up @@ -206,10 +203,10 @@ export class ActiveWorkflowRunner {
path = path.slice(0, -1);
}

let webhook = (await Db.collections.Webhook.findOne({
let webhook = await Db.collections.Webhook.findOne({
webhookPath: path,
method: httpMethod,
})) as IWebhookDb;
});
let webhookId: string | undefined;

// check if path is dynamic
Expand Down Expand Up @@ -280,7 +277,7 @@ export class ActiveWorkflowRunner {

const nodeTypes = NodeTypes();
const workflow = new Workflow({
id: webhook.workflowId.toString(),
id: webhook.workflowId,
name: workflowData.name,
nodes: workflowData.nodes,
connections: workflowData.connections,
Expand Down Expand Up @@ -419,12 +416,12 @@ export class ActiveWorkflowRunner {

path = webhookData.path;

const webhook = {
const webhook: WebhookEntity = {
workflowId: webhookData.workflowId,
webhookPath: path,
node: node.name,
method: webhookData.httpMethod,
} as IWebhookDb;
};

if (webhook.webhookPath.startsWith('/')) {
webhook.webhookPath = webhook.webhookPath.slice(1);
Expand Down Expand Up @@ -549,11 +546,9 @@ export class ActiveWorkflowRunner {

await WorkflowHelpers.saveStaticData(workflow);

const webhook = {
await Db.collections.Webhook.delete({
workflowId: workflowData.id,
} as IWebhookDb;

await Db.collections.Webhook.delete(webhook);
});
}

/**
Expand Down Expand Up @@ -713,15 +708,15 @@ export class ActiveWorkflowRunner {
`The trigger node "${node.name}" of workflow "${workflowData.name}" failed with the error: "${error.message}". Will try to reactivate.`,
{
nodeName: node.name,
workflowId: workflowData.id.toString(),
workflowId: workflowData.id,
workflowName: workflowData.name,
},
);

// Remove the workflow as "active"

await this.activeWorkflows?.remove(workflowData.id.toString());
this.activationErrors[workflowData.id.toString()] = {
await this.activeWorkflows?.remove(workflowData.id);
this.activationErrors[workflowData.id] = {
time: new Date().getTime(),
error: {
message: error.message,
Expand Down Expand Up @@ -897,7 +892,7 @@ export class ActiveWorkflowRunner {
activationMode: WorkflowActivateMode,
workflowData: IWorkflowDb,
): void {
const workflowId = workflowData.id.toString();
const workflowId = workflowData.id;
const workflowName = workflowData.name;

const retryFunction = async () => {
Expand Down
11 changes: 5 additions & 6 deletions packages/cli/src/CredentialsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ export class CredentialsHelper extends ICredentialsHelper {
const credential = userId
? await Db.collections.SharedCredentials.findOneOrFail({
relations: ['credentials'],
where: { credentials: { id: nodeCredential.id, type }, user: { id: userId } },
where: { credentials: { id: nodeCredential.id, type }, userId },
}).then((shared) => shared.credentials)
: await Db.collections.Credentials.findOneOrFail({ id: nodeCredential.id, type });

Expand All @@ -290,7 +290,7 @@ export class CredentialsHelper extends ICredentialsHelper {
}

return new Credentials(
{ id: credential.id.toString(), name: credential.name },
{ id: credential.id, name: credential.name },
credential.type,
credential.nodesAccess,
credential.data,
Expand Down Expand Up @@ -581,7 +581,7 @@ export class CredentialsHelper extends ICredentialsHelper {
position: [0, 0],
credentials: {
[credentialType]: {
id: credentialsDecrypted.id.toString(),
id: credentialsDecrypted.id,
name: credentialsDecrypted.name,
},
},
Expand Down Expand Up @@ -762,8 +762,7 @@ export async function getCredentialForUser(
export async function getCredentialWithoutUser(
credentialId: string,
): Promise<ICredentialsDb | undefined> {
const credential = await Db.collections.Credentials.findOne(credentialId);
return credential;
return Db.collections.Credentials.findOne(credentialId);
}

export function createCredentialsFromCredentialsEntity(
Expand All @@ -774,5 +773,5 @@ export function createCredentialsFromCredentialsEntity(
if (encrypt) {
return new Credentials({ id: null, name }, type, nodesAccess);
}
return new Credentials({ id: id.toString(), name }, type, nodesAccess, data);
return new Credentials({ id, name }, type, nodesAccess, data);
}
63 changes: 18 additions & 45 deletions packages/cli/src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
ITaskData,
ITelemetrySettings,
ITelemetryTrackProperties,
IWorkflowBase as IWorkflowBaseWorkflow,
IWorkflowBase,
CredentialLoadingDetails,
Workflow,
WorkflowActivateMode,
Expand All @@ -38,6 +38,7 @@ import type { SharedCredentials } from '@db/entities/SharedCredentials';
import type { SharedWorkflow } from '@db/entities/SharedWorkflow';
import type { TagEntity } from '@db/entities/TagEntity';
import type { User } from '@db/entities/User';
import type { WebhookEntity } from '@db/entities/WebhookEntity';
import type { WorkflowEntity } from '@db/entities/WorkflowEntity';
import type { WorkflowStatistics } from '@db/entities/WorkflowStatistics';

Expand Down Expand Up @@ -71,7 +72,7 @@ export interface IDatabaseCollections {
Credentials: Repository<ICredentialsDb>;
Execution: Repository<IExecutionFlattedDb>;
Workflow: Repository<WorkflowEntity>;
Webhook: Repository<IWebhookDb>;
Webhook: Repository<WebhookEntity>;
Tag: Repository<TagEntity>;
Role: Repository<Role>;
User: Repository<User>;
Expand All @@ -83,28 +84,12 @@ export interface IDatabaseCollections {
WorkflowStatistics: Repository<WorkflowStatistics>;
}

export interface IWebhookDb {
workflowId: number | string;
webhookPath: string;
method: string;
node: string;
webhookId?: string;
pathLength?: number;
}

// ----------------------------------
// tags
// ----------------------------------

export interface ITagDb {
id: number;
name: string;
createdAt: Date;
updatedAt: Date;
}

export interface ITagToImport {
id: string | number;
id: string;
name: string;
createdAt?: string;
updatedAt?: string;
Expand All @@ -114,20 +99,16 @@ export type UsageCount = {
usageCount: number;
};

export type ITagWithCountDb = ITagDb & UsageCount;
export type ITagWithCountDb = TagEntity & UsageCount;

// ----------------------------------
// workflows
// ----------------------------------

export interface IWorkflowBase extends IWorkflowBaseWorkflow {
id?: number | string;
}

// Almost identical to editor-ui.Interfaces.ts
export interface IWorkflowDb extends IWorkflowBase {
id: number | string;
tags?: ITagDb[];
id: string;
tags?: TagEntity[];
}

export interface IWorkflowToImport extends IWorkflowBase {
Expand All @@ -148,35 +129,27 @@ export interface ICredentialsBase {
}

export interface ICredentialsDb extends ICredentialsBase, ICredentialsEncrypted {
id: number | string;
id: string;
name: string;
shared?: SharedCredentials[];
}

export interface ICredentialsResponse extends ICredentialsDb {
id: string;
}

export interface ICredentialsDecryptedDb extends ICredentialsBase, ICredentialsDecrypted {
id: number | string;
}
export type ICredentialsDecryptedDb = ICredentialsBase & ICredentialsDecrypted;

export interface ICredentialsDecryptedResponse extends ICredentialsDecryptedDb {
id: string;
}
export type ICredentialsDecryptedResponse = ICredentialsDecryptedDb;

export type DatabaseType = 'mariadb' | 'postgresdb' | 'mysqldb' | 'sqlite';
export type SaveExecutionDataType = 'all' | 'none';

export interface IExecutionBase {
id?: number | string;
id?: string;
mode: WorkflowExecuteMode;
startedAt: Date;
stoppedAt?: Date; // empty value means execution is still running
workflowId?: string; // To be able to filter executions easily //
finished: boolean;
retryOf?: number | string; // If it is a retry, the id of the execution it is a retry of.
retrySuccessId?: number | string; // If it failed and a retry did succeed. The id of the successful retry.
retryOf?: string; // If it is a retry, the id of the execution it is a retry of.
retrySuccessId?: string; // If it failed and a retry did succeed. The id of the successful retry.
}

// Data in regular format with references
Expand Down Expand Up @@ -208,7 +181,7 @@ export interface IExecutionFlatted extends IExecutionBase {
}

export interface IExecutionFlattedDb extends IExecutionBase {
id: number | string;
id: string;
data: string;
waitTill?: Date | null;
workflowData: Omit<IWorkflowBase, 'pinData'>;
Expand All @@ -220,14 +193,14 @@ export interface IExecutionFlattedResponse extends IExecutionFlatted {
}

export interface IExecutionResponseApi {
id: number | string;
id: string;
mode: WorkflowExecuteMode;
startedAt: Date;
stoppedAt?: Date;
workflowId?: string;
finished: boolean;
retryOf?: number | string;
retrySuccessId?: number | string;
retryOf?: string;
retrySuccessId?: string;
data?: object;
waitTill?: Date | null;
workflowData: IWorkflowBase;
Expand Down Expand Up @@ -685,7 +658,7 @@ export interface IWorkflowExecutionDataProcess {
executionData?: IRunExecutionData;
runData?: IRunData;
pinData?: IPinData;
retryOf?: number | string;
retryOf?: string;
sessionId?: string;
startNodes?: string[];
workflowData: IWorkflowBase;
Expand Down
Loading