2020-05-30 16:03:58 -07:00
import {
MigrationInterface ,
QueryRunner ,
} from 'typeorm' ;
import {
IWorkflowDb ,
NodeTypes ,
WebhookHelpers ,
2020-06-10 06:58:57 -07:00
} from '../../..' ;
2020-05-30 16:03:58 -07:00
import {
Workflow ,
} from 'n8n-workflow/dist/src/Workflow' ;
2020-05-27 16:32:49 -07:00
import {
IWebhookDb ,
2020-06-10 06:58:57 -07:00
} from '../../../Interfaces' ;
2020-05-27 16:32:49 -07:00
2020-06-10 06:58:57 -07:00
import * as config from '../../../../config' ;
2020-05-27 16:32:49 -07:00
export class WebhookModel1589476000887 implements MigrationInterface {
name = 'WebhookModel1589476000887' ;
2020-06-10 06:58:57 -07:00
async up ( queryRunner : QueryRunner ) : Promise < void > {
2020-05-27 16:32:49 -07:00
let tablePrefix = config . get ( 'database.tablePrefix' ) ;
const schema = config . get ( 'database.postgresdb.schema' ) ;
if ( schema ) {
tablePrefix = schema + '.' + tablePrefix ;
}
2020-06-10 06:58:57 -07:00
await queryRunner . query ( ` CREATE TABLE ${ tablePrefix } webhook_entity ("workflowId" integer NOT NULL, "webhookPath" character varying NOT NULL, "method" character varying NOT NULL, "node" character varying NOT NULL, CONSTRAINT "PK_b21ace2e13596ccd87dc9bf4ea6" PRIMARY KEY ("webhookPath", "method")) ` , undefined ) ;
2020-05-27 16:32:49 -07:00
const workflows = await queryRunner . query ( ` SELECT * FROM ${ tablePrefix } workflow_entity WHERE active=true ` ) as IWorkflowDb [ ] ;
const data : IWebhookDb [ ] = [ ] ;
const nodeTypes = NodeTypes ( ) ;
for ( const workflow of workflows ) {
const workflowInstance = new Workflow ( { id : workflow.id as string , name : workflow.name , nodes : workflow.nodes , connections : workflow.connections , active : workflow.active , nodeTypes , staticData : workflow.staticData , settings : workflow.settings } ) ;
const webhooks = WebhookHelpers . getWorkflowWebhooksBasic ( workflowInstance ) ;
for ( const webhook of webhooks ) {
data . push ( {
workflowId : workflowInstance.id as string ,
webhookPath : webhook.path ,
method : webhook.httpMethod ,
node : webhook.node ,
} ) ;
}
}
if ( data . length !== 0 ) {
await queryRunner . manager . createQueryBuilder ( )
. insert ( )
. into ( ` ${ tablePrefix } webhook_entity ` )
. values ( data )
. execute ( ) ;
}
}
2020-06-10 06:58:57 -07:00
async down ( queryRunner : QueryRunner ) : Promise < void > {
2020-05-27 16:32:49 -07:00
let tablePrefix = config . get ( 'database.tablePrefix' ) ;
const schema = config . get ( 'database.postgresdb.schema' ) ;
if ( schema ) {
tablePrefix = schema + '.' + tablePrefix ;
}
2020-06-10 06:58:57 -07:00
await queryRunner . query ( ` DROP TABLE ${ tablePrefix } webhook_entity ` , undefined ) ;
}
2020-05-27 16:32:49 -07:00
}