Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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) {
// Clean up orphaned agentId references before adding foreign key constraint
await runQuery(
`UPDATE ${escape.tableName(table.sessions)} SET "agentId" = NULL WHERE "agentId" IS NOT NULL AND "agentId" NOT IN (SELECT id FROM ${escape.tableName(table.agents)})`,
);
await runQuery(
`UPDATE ${escape.tableName(table.messages)} SET "agentId" = NULL WHERE "agentId" IS NOT NULL AND "agentId" 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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not cascade delete ? Is there a possibility that the agent is deleted and we should still keep the messages ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to keep sessions and messages, as these are historical data to browse even when the user doesn't need the agent anymore for new conversations.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, seems better & more intuitive than deletion

);
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',
);
}
}
2 changes: 2 additions & 0 deletions packages/@n8n/db/src/migrations/mysqldb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [
Expand Down Expand Up @@ -257,4 +258,5 @@ export const mysqlMigrations: Migration[] = [
BackfillMissingWorkflowHistoryRecords1765448186933,
AddResolvableFieldsToCredentials1765459448000,
AddIconToAgentTable1765788427674,
AddAgentIdForeignKeys1765886667897,
];
2 changes: 2 additions & 0 deletions packages/@n8n/db/src/migrations/postgresdb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [
Expand Down Expand Up @@ -259,4 +260,5 @@ export const postgresMigrations: Migration[] = [
AddResolvableFieldsToCredentials1765459448000,
AddIconToAgentTable1765788427674,
ConvertAgentIdToUuid1765804780000,
AddAgentIdForeignKeys1765886667897,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { AddAgentIdForeignKeys1765886667897 as BaseMigration } from '../common/1765886667897-AddAgentIdForeignKeys';

export class AddAgentIdForeignKeys1765886667897 extends BaseMigration {
transaction = false as const;
}
2 changes: 2 additions & 0 deletions packages/@n8n/db/src/migrations/sqlite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -249,6 +250,7 @@ const sqliteMigrations: Migration[] = [
BackfillMissingWorkflowHistoryRecords1765448186933,
AddResolvableFieldsToCredentials1764689448000,
AddIconToAgentTable1765788427674,
AddAgentIdForeignKeys1765886667897,
];

export { sqliteMigrations };