mirror of
https://github.com/n8n-io/n8n.git
synced 2026-09-24 23:22:38 +08:00
chore(core): Set foreign key constraint on ChatHub agentId (#23275)
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import type { MigrationContext, ReversibleMigration } from '../migration-types';
|
||||
|
||||
const table = {
|
||||
agents: 'chat_hub_agents',
|
||||
sessions: 'chat_hub_sessions',
|
||||
messages: 'chat_hub_messages',
|
||||
} as const;
|
||||
|
||||
export class AddAgentIdForeignKeys1765886667897 implements ReversibleMigration {
|
||||
async up({ schemaBuilder: { addForeignKey }, runQuery, escape }: MigrationContext) {
|
||||
const escapedAgentIdColumn = escape.columnName('agentId');
|
||||
|
||||
// Clean up orphaned agentId references before adding foreign key constraint
|
||||
await runQuery(
|
||||
`UPDATE ${escape.tableName(table.sessions)} SET ${escapedAgentIdColumn} = NULL WHERE ${escapedAgentIdColumn} IS NOT NULL AND ${escapedAgentIdColumn} NOT IN (SELECT id FROM ${escape.tableName(table.agents)})`,
|
||||
);
|
||||
await runQuery(
|
||||
`UPDATE ${escape.tableName(table.messages)} SET ${escapedAgentIdColumn} = NULL WHERE ${escapedAgentIdColumn} IS NOT NULL AND ${escapedAgentIdColumn} NOT IN (SELECT id FROM ${escape.tableName(table.agents)})`,
|
||||
);
|
||||
|
||||
// Add foreign key constraint for agentId in sessions table
|
||||
await addForeignKey(
|
||||
table.sessions,
|
||||
'agentId',
|
||||
[table.agents, 'id'],
|
||||
'FK_chat_hub_sessions_agentId',
|
||||
'SET NULL',
|
||||
);
|
||||
await addForeignKey(
|
||||
table.messages,
|
||||
'agentId',
|
||||
[table.agents, 'id'],
|
||||
'FK_chat_hub_messages_agentId',
|
||||
'SET NULL',
|
||||
);
|
||||
}
|
||||
|
||||
async down({ schemaBuilder: { dropForeignKey } }: MigrationContext) {
|
||||
await dropForeignKey(
|
||||
table.messages,
|
||||
'agentId',
|
||||
[table.agents, 'id'],
|
||||
'FK_chat_hub_messages_agentId',
|
||||
);
|
||||
await dropForeignKey(
|
||||
table.sessions,
|
||||
'agentId',
|
||||
[table.agents, 'id'],
|
||||
'FK_chat_hub_sessions_agentId',
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -126,6 +126,7 @@ import { AddDynamicCredentialEntryTable1764689388394 } from '../common/176468938
|
||||
import { BackfillMissingWorkflowHistoryRecords1765448186933 } from '../common/1765448186933-BackfillMissingWorkflowHistoryRecords';
|
||||
import { AddResolvableFieldsToCredentials1765459448000 } from '../common/1765459448000-AddResolvableFieldsToCredentials';
|
||||
import { AddIconToAgentTable1765788427674 } from '../common/1765788427674-AddIconToAgentTable';
|
||||
import { AddAgentIdForeignKeys1765886667897 } from '../common/1765886667897-AddAgentIdForeignKeys';
|
||||
import type { Migration } from '../migration-types';
|
||||
|
||||
export const mysqlMigrations: Migration[] = [
|
||||
@@ -257,4 +258,5 @@ export const mysqlMigrations: Migration[] = [
|
||||
BackfillMissingWorkflowHistoryRecords1765448186933,
|
||||
AddResolvableFieldsToCredentials1765459448000,
|
||||
AddIconToAgentTable1765788427674,
|
||||
AddAgentIdForeignKeys1765886667897,
|
||||
];
|
||||
|
||||
@@ -127,6 +127,7 @@ import { AddDynamicCredentialEntryTable1764689388394 } from '../common/176468938
|
||||
import { BackfillMissingWorkflowHistoryRecords1765448186933 } from '../common/1765448186933-BackfillMissingWorkflowHistoryRecords';
|
||||
import { AddResolvableFieldsToCredentials1765459448000 } from '../common/1765459448000-AddResolvableFieldsToCredentials';
|
||||
import { AddIconToAgentTable1765788427674 } from '../common/1765788427674-AddIconToAgentTable';
|
||||
import { AddAgentIdForeignKeys1765886667897 } from '../common/1765886667897-AddAgentIdForeignKeys';
|
||||
import type { Migration } from '../migration-types';
|
||||
|
||||
export const postgresMigrations: Migration[] = [
|
||||
@@ -259,4 +260,5 @@ export const postgresMigrations: Migration[] = [
|
||||
AddResolvableFieldsToCredentials1765459448000,
|
||||
AddIconToAgentTable1765788427674,
|
||||
ConvertAgentIdToUuid1765804780000,
|
||||
AddAgentIdForeignKeys1765886667897,
|
||||
];
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
import { AddAgentIdForeignKeys1765886667897 as BaseMigration } from '../common/1765886667897-AddAgentIdForeignKeys';
|
||||
|
||||
export class AddAgentIdForeignKeys1765886667897 extends BaseMigration {
|
||||
transaction = false as const;
|
||||
}
|
||||
@@ -48,6 +48,7 @@ import { AddWorkflowVersionColumn1761047826451 } from './1761047826451-AddWorkfl
|
||||
import { ChangeDependencyInfoToJson1761655473000 } from './1761655473000-ChangeDependencyInfoToJson';
|
||||
import { AddCreatorIdToProjectTable1764276827837 } from './1764276827837-AddCreatorIdToProjectTable';
|
||||
import { AddResolvableFieldsToCredentials1764689448000 } from './1764689448000-AddResolvableFieldsToCredentials';
|
||||
import { AddAgentIdForeignKeys1765886667897 } from './1765886667897-AddAgentIdForeignKeys';
|
||||
import { UniqueWorkflowNames1620821879465 } from '../common/1620821879465-UniqueWorkflowNames';
|
||||
import { UpdateWorkflowCredentials1630330987096 } from '../common/1630330987096-UpdateWorkflowCredentials';
|
||||
import { AddNodeIds1658930531669 } from '../common/1658930531669-AddNodeIds';
|
||||
@@ -249,6 +250,7 @@ const sqliteMigrations: Migration[] = [
|
||||
BackfillMissingWorkflowHistoryRecords1765448186933,
|
||||
AddResolvableFieldsToCredentials1764689448000,
|
||||
AddIconToAgentTable1765788427674,
|
||||
AddAgentIdForeignKeys1765886667897,
|
||||
];
|
||||
|
||||
export { sqliteMigrations };
|
||||
|
||||
Reference in New Issue
Block a user