forked from n8n-io/n8n
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): Add missing primary key to execution annotation tags table (…
- Loading branch information
1 parent
09954f6
commit b4b543d
Showing
7 changed files
with
111 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...c/databases/migrations/common/1728659839644-AddMissingPrimaryKeyOnAnnotationTagMapping.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import assert from 'node:assert'; | ||
|
||
import type { IrreversibleMigration, MigrationContext } from '@/databases/types'; | ||
|
||
export class AddMissingPrimaryKeyOnAnnotationTagMapping1728659839644 | ||
implements IrreversibleMigration | ||
{ | ||
async up({ queryRunner, tablePrefix }: MigrationContext) { | ||
// Check if the primary key already exists | ||
const table = await queryRunner.getTable(`${tablePrefix}execution_annotation_tags`); | ||
|
||
assert(table, 'execution_annotation_tags table not found'); | ||
|
||
const hasPrimaryKey = table.primaryColumns.length > 0; | ||
|
||
if (!hasPrimaryKey) { | ||
await queryRunner.createPrimaryKey(`${tablePrefix}execution_annotation_tags`, [ | ||
'annotationId', | ||
'tagId', | ||
]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...c/databases/migrations/sqlite/1728659839644-AddMissingPrimaryKeyOnAnnotationTagMapping.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import assert from 'node:assert'; | ||
|
||
import type { IrreversibleMigration, MigrationContext } from '@/databases/types'; | ||
|
||
const annotationsTableName = 'execution_annotations'; | ||
const annotationTagsTableName = 'annotation_tag_entity'; | ||
const annotationTagMappingsTableName = 'execution_annotation_tags'; | ||
|
||
export class AddMissingPrimaryKeyOnAnnotationTagMapping1728659839644 | ||
implements IrreversibleMigration | ||
{ | ||
async up({ | ||
queryRunner, | ||
tablePrefix, | ||
schemaBuilder: { createTable, column, dropIndex }, | ||
}: MigrationContext) { | ||
// Check if the primary key already exists | ||
const table = await queryRunner.getTable(`${tablePrefix}execution_annotation_tags`); | ||
|
||
assert(table, '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: | ||
|
||
// 1. Rename the existing table | ||
await queryRunner.query( | ||
`ALTER TABLE ${tablePrefix}${annotationTagMappingsTableName} RENAME TO ${tablePrefix}${annotationTagMappingsTableName}_tmp;`, | ||
); | ||
|
||
// 1.1 Drop the existing indices | ||
await dropIndex(`${annotationTagMappingsTableName}_tmp`, ['tagId'], { | ||
customIndexName: 'IDX_a3697779b366e131b2bbdae297', | ||
}); | ||
await dropIndex(`${annotationTagMappingsTableName}_tmp`, ['annotationId'], { | ||
customIndexName: 'IDX_c1519757391996eb06064f0e7c', | ||
}); | ||
|
||
// 2. Create a new table with the desired structure | ||
await createTable(annotationTagMappingsTableName) | ||
.withColumns( | ||
column('annotationId').int.notNull.primary, | ||
column('tagId').varchar(24).notNull.primary, | ||
) | ||
.withForeignKey('annotationId', { | ||
tableName: annotationsTableName, | ||
columnName: 'id', | ||
onDelete: 'CASCADE', | ||
}) | ||
.withIndexOn('tagId') | ||
.withIndexOn('annotationId') | ||
.withForeignKey('tagId', { | ||
tableName: annotationTagsTableName, | ||
columnName: 'id', | ||
onDelete: 'CASCADE', | ||
}); | ||
|
||
// 3. Copy data from the old table to the new one | ||
await queryRunner.query( | ||
`INSERT INTO ${tablePrefix}${annotationTagMappingsTableName} SELECT * FROM ${tablePrefix}${annotationTagMappingsTableName}_tmp;`, | ||
); | ||
|
||
// 4. Drop the old table | ||
await queryRunner.dropTable(`${tablePrefix}${annotationTagMappingsTableName}_tmp`, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters