-
Notifications
You must be signed in to change notification settings - Fork 52.5k
chore(core): Set foreign key constraint on ChatHub agentId #23275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
0dabdfe
80e8aad
d299d24
f45faaa
43c5d10
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', | ||
| ); | ||
| } | ||
| } | ||
| 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; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.