Skip to content

Commit

Permalink
wip: Make adding PK to the existing table conditional
Browse files Browse the repository at this point in the history
  • Loading branch information
burivuhster committed Oct 9, 2024
1 parent 027599f commit 30c8dcf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
import { ApplicationError } from 'n8n-workflow';

import type { IrreversibleMigration, MigrationContext } from '@/databases/types';

export class AddMissingPrimaryKeyOnAnnotationTagMapping1728396464278
implements IrreversibleMigration
{
async up({ queryRunner, tablePrefix }: MigrationContext) {
await queryRunner.createPrimaryKey(`${tablePrefix}execution_annotation_tags`, [
'annotationId',
'tagId',
]);
// Check if the primary key already exists
const table = await queryRunner.getTable(`${tablePrefix}execution_annotation_tags`);

if (!table) {
throw new ApplicationError('execution_annotation_tags table not found');
}

const hasPrimaryKey = table.primaryColumns.length > 0;

if (!hasPrimaryKey) {
await queryRunner.createPrimaryKey(`${tablePrefix}execution_annotation_tags`, [
'annotationId',
'tagId',
]);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ApplicationError } from 'n8n-workflow/src';

import type { IrreversibleMigration, MigrationContext } from '@/databases/types';

const annotationsTableName = 'execution_annotations';
Expand All @@ -12,10 +14,19 @@ export class AddMissingPrimaryKeyOnAnnotationTagMapping1728396464278
tablePrefix,
schemaBuilder: { createTable, column, dropIndex },
}: MigrationContext) {
await queryRunner.createPrimaryKey(`${tablePrefix}${annotationTagMappingsTableName}`, [
'annotationId',
'tagId',
]);
// Check if the primary key already exists
const table = await queryRunner.getTable(`${tablePrefix}execution_annotation_tags`);

if (!table) {
throw new ApplicationError('execution_annotation_tags table not found');
}

const hasPrimaryKey = table.primaryColumns.length > 0;

// Do nothing if the primary key already exists
if (hasPrimaryKey) {
return;
}

// SQLite doesn't support adding a primary key to an existing table
// So we have to do the following steps:
Expand Down

0 comments on commit 30c8dcf

Please sign in to comment.