Repository navigation
Add agent_id to schedule table with index #583
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| "@truefoundry/trueforge": patch | ||
| --- | ||
|
|
||
| Add NOT NULL `agent_id` on `schedule` (backfilled from `agent`), FK to `agent(id)` ON DELETE CASCADE (replacing the `(tenant_id, agent_name)` FK), and `(tenant_id, agent_id)` index for per-agent listing. |
This file contains hidden or 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 hidden or 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
46 changes: 46 additions & 0 deletions
46
packages/trueforge/src/db/postgres/migrations/20260904_000001_schedule_agent_id.ts
This file contains hidden or 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,46 @@ | ||
| import { sql, type Kysely } from 'kysely'; | ||
| import { SCHEDULE_AGENT_ID_IDX } from '../../indexes'; | ||
|
|
||
| /** | ||
| * Add `agent_id` on schedule (stable ULID binding). Backfills from `agent` via | ||
| * `(tenant_id, agent_name)`, then drops the name FK in favor of `agent(id)`. | ||
| * Index mirrors `session_agent_id_idx` for per-agent schedule lists. | ||
| * Runs inside the Migrator's transaction — do not nest `db.transaction()`. | ||
| */ | ||
| export async function up(db: Kysely<unknown>): Promise<void> { | ||
| await sql` | ||
| SET LOCAL lock_timeout = '5s'; | ||
|
|
||
| ALTER TABLE schedule | ||
| ADD COLUMN agent_id text; | ||
| UPDATE schedule AS s | ||
| SET agent_id = a.id | ||
| FROM agent AS a | ||
| WHERE s.tenant_id = a.tenant_id | ||
| AND s.agent_name = a.name; | ||
| ALTER TABLE schedule | ||
| ALTER COLUMN agent_id SET NOT NULL; | ||
| ALTER TABLE schedule | ||
| DROP CONSTRAINT schedule_agent_name_fk; | ||
| ALTER TABLE schedule | ||
| ADD CONSTRAINT schedule_agent_id_fk | ||
| FOREIGN KEY (agent_id) REFERENCES agent (id) ON DELETE CASCADE; | ||
| CREATE INDEX ${sql.raw(SCHEDULE_AGENT_ID_IDX)} | ||
| ON schedule (tenant_id, agent_id); | ||
| `.execute(db); | ||
| } | ||
|
|
||
| export async function down(db: Kysely<unknown>): Promise<void> { | ||
| await sql` | ||
| SET LOCAL lock_timeout = '5s'; | ||
|
|
||
| DROP INDEX IF EXISTS ${sql.raw(SCHEDULE_AGENT_ID_IDX)}; | ||
| ALTER TABLE schedule | ||
| DROP CONSTRAINT IF EXISTS schedule_agent_id_fk; | ||
| ALTER TABLE schedule | ||
| ADD CONSTRAINT schedule_agent_name_fk | ||
| FOREIGN KEY (tenant_id, agent_name) REFERENCES agent (tenant_id, name) ON DELETE CASCADE; | ||
| ALTER TABLE schedule | ||
| DROP COLUMN agent_id; | ||
| `.execute(db); | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
137 changes: 137 additions & 0 deletions
137
packages/trueforge/src/db/sqlite/migrations/20260904_000001_schedule_agent_id.ts
This file contains hidden or 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,137 @@ | ||
| import { sql, type Kysely } from 'kysely'; | ||
| import { SCHEDULE_AGENT_ID_IDX, SCHEDULE_CREATED_BY_SUBJECT_ID_IDX } from '../../indexes'; | ||
|
|
||
| /** | ||
| * Add NOT NULL `agent_id` on schedule — mirrors | ||
| * db/postgres/migrations/20260904_000001_schedule_agent_id.ts. | ||
| * Drops the `(tenant_id, agent_name)` FK; binding is `agent(id)` only. | ||
| * | ||
| * SQLite STRICT cannot ADD a NOT NULL column without a DEFAULT, so rebuild | ||
| * `schedule` (FKs off so `schedule_run` rows survive the drop/rename). | ||
| * better-sqlite3 prepares one statement per call — keep statements separate. | ||
| */ | ||
| export async function up(db: Kysely<unknown>): Promise<void> { | ||
| await sql`PRAGMA foreign_keys = OFF`.execute(db); | ||
| try { | ||
| await db.transaction().execute(async trx => { | ||
| await sql` | ||
|
thesujai marked this conversation as resolved.
Resolved
|
||
| CREATE TABLE schedule_new ( | ||
| id TEXT NOT NULL, | ||
| tenant_id TEXT NOT NULL, | ||
| agent_id TEXT NOT NULL, | ||
| agent_name TEXT NOT NULL, | ||
| name TEXT NOT NULL, | ||
| manifest BLOB NOT NULL, | ||
| status TEXT NOT NULL CHECK (length(status) <= 16), | ||
| created_by_subject BLOB NOT NULL, | ||
| created_at TEXT NOT NULL, | ||
| updated_at TEXT NOT NULL, | ||
| PRIMARY KEY (id), | ||
| FOREIGN KEY (agent_id) REFERENCES agent (id) ON DELETE CASCADE | ||
| ) STRICT | ||
| `.execute(trx); | ||
|
|
||
| await sql` | ||
| INSERT INTO schedule_new ( | ||
| id, | ||
| tenant_id, | ||
| agent_id, | ||
| agent_name, | ||
| name, | ||
| manifest, | ||
| status, | ||
| created_by_subject, | ||
| created_at, | ||
| updated_at | ||
| ) | ||
| SELECT | ||
| s.id, | ||
| s.tenant_id, | ||
| a.id, | ||
| s.agent_name, | ||
| s.name, | ||
| s.manifest, | ||
| s.status, | ||
| s.created_by_subject, | ||
| s.created_at, | ||
| s.updated_at | ||
| FROM schedule AS s | ||
| INNER JOIN agent AS a | ||
| ON a.tenant_id = s.tenant_id | ||
| AND a.name = s.agent_name | ||
| `.execute(trx); | ||
|
|
||
| await sql`DROP TABLE schedule`.execute(trx); | ||
| await sql`ALTER TABLE schedule_new RENAME TO schedule`.execute(trx); | ||
|
|
||
| await sql` | ||
| CREATE INDEX schedule_agent_idx | ||
| ON schedule (tenant_id, agent_name) | ||
| `.execute(trx); | ||
| await sql` | ||
| CREATE UNIQUE INDEX schedule_name_uq | ||
| ON schedule (tenant_id, agent_name, name) | ||
| `.execute(trx); | ||
| await sql` | ||
| CREATE INDEX ${sql.raw(SCHEDULE_CREATED_BY_SUBJECT_ID_IDX)} | ||
| ON schedule (tenant_id, json_extract(created_by_subject, '$.subject_id')) | ||
| `.execute(trx); | ||
| await sql` | ||
| CREATE INDEX ${sql.raw(SCHEDULE_AGENT_ID_IDX)} | ||
| ON schedule (tenant_id, agent_id) | ||
| `.execute(trx); | ||
| }); | ||
| } finally { | ||
| await sql`PRAGMA foreign_keys = ON`.execute(db); | ||
| } | ||
| } | ||
|
|
||
| export async function down(db: Kysely<unknown>): Promise<void> { | ||
| await sql`PRAGMA foreign_keys = OFF`.execute(db); | ||
| try { | ||
| await db.transaction().execute(async trx => { | ||
| await sql` | ||
| CREATE TABLE schedule_old ( | ||
| id TEXT NOT NULL, | ||
| tenant_id TEXT NOT NULL, | ||
| agent_name TEXT NOT NULL, | ||
| name TEXT NOT NULL, | ||
| manifest BLOB NOT NULL, | ||
| status TEXT NOT NULL CHECK (length(status) <= 16), | ||
| created_by_subject BLOB NOT NULL, | ||
| created_at TEXT NOT NULL, | ||
| updated_at TEXT NOT NULL, | ||
| PRIMARY KEY (id), | ||
| FOREIGN KEY (tenant_id, agent_name) REFERENCES agent (tenant_id, name) ON DELETE CASCADE | ||
| ) STRICT | ||
| `.execute(trx); | ||
|
|
||
| await sql` | ||
| INSERT INTO schedule_old ( | ||
| id, tenant_id, agent_name, name, manifest, status, created_by_subject, created_at, updated_at | ||
| ) | ||
| SELECT | ||
| id, tenant_id, agent_name, name, manifest, status, created_by_subject, created_at, updated_at | ||
| FROM schedule | ||
| `.execute(trx); | ||
|
|
||
| await sql`DROP TABLE schedule`.execute(trx); | ||
| await sql`ALTER TABLE schedule_old RENAME TO schedule`.execute(trx); | ||
|
|
||
| await sql` | ||
| CREATE INDEX schedule_agent_idx | ||
| ON schedule (tenant_id, agent_name) | ||
| `.execute(trx); | ||
| await sql` | ||
| CREATE UNIQUE INDEX schedule_name_uq | ||
| ON schedule (tenant_id, agent_name, name) | ||
| `.execute(trx); | ||
| await sql` | ||
| CREATE INDEX ${sql.raw(SCHEDULE_CREATED_BY_SUBJECT_ID_IDX)} | ||
| ON schedule (tenant_id, json_extract(created_by_subject, '$.subject_id')) | ||
| `.execute(trx); | ||
| }); | ||
| } finally { | ||
| await sql`PRAGMA foreign_keys = ON`.execute(db); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.