Skip to content

Commit a37cdea

Browse files
authored
Add agent_id to schedule table with index (#583)
* Add agent_id to schedule table and update related functionality * Add NOT NULL `agent_id` to schedule table, backfilled from `agent`, and update foreign key constraints. Replace `(tenant_id, agent_name)` FK with `agent(id)` ON DELETE CASCADE. Update related documentation and migration scripts for consistency across Postgres and SQLite.
1 parent 284762f commit a37cdea

13 files changed

Lines changed: 242 additions & 13 deletions

File tree

‎.changeset/schedule-agent-id.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@truefoundry/trueforge": patch
3+
---
4+
5+
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.

‎packages/trueforge/src/apis/schedules.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ export function createSchedulesRouter<TTransaction>(deps: SchedulesRouterDeps<TT
250250
const { schedule } = await deps.scheduleStore.createScheduleAndRun(
251251
{
252252
tenant_id: requestContext.tenant_id,
253+
agent_id: agent.id,
253254
agent_name: agent.name,
254255
name: body.name,
255256
manifest: body.manifest,

‎packages/trueforge/src/db/indexes.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@ export const SESSION_CREATED_BY_SUBJECT_ID_IDX = 'session_created_by_subject_id_
1313
/** `(tenant_id, created_by_subject.subject_id)` on schedule. */
1414
export const SCHEDULE_CREATED_BY_SUBJECT_ID_IDX = 'schedule_created_by_subject_id_idx';
1515

16+
/** `(tenant_id, agent_id)` on schedule — list schedules for one agent by id. */
17+
export const SCHEDULE_AGENT_ID_IDX = 'schedule_agent_id_idx';
18+
1619
/** `(tenant_id, created_by_subject.subject_id)` on schedule_run. */
1720
export const SCHEDULE_RUN_CREATED_BY_SUBJECT_ID_IDX = 'schedule_run_created_by_subject_id_idx';
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { sql, type Kysely } from 'kysely';
2+
import { SCHEDULE_AGENT_ID_IDX } from '../../indexes';
3+
4+
/**
5+
* Add `agent_id` on schedule (stable ULID binding). Backfills from `agent` via
6+
* `(tenant_id, agent_name)`, then drops the name FK in favor of `agent(id)`.
7+
* Index mirrors `session_agent_id_idx` for per-agent schedule lists.
8+
* Runs inside the Migrator's transaction — do not nest `db.transaction()`.
9+
*/
10+
export async function up(db: Kysely<unknown>): Promise<void> {
11+
await sql`
12+
SET LOCAL lock_timeout = '5s';
13+
14+
ALTER TABLE schedule
15+
ADD COLUMN agent_id text;
16+
UPDATE schedule AS s
17+
SET agent_id = a.id
18+
FROM agent AS a
19+
WHERE s.tenant_id = a.tenant_id
20+
AND s.agent_name = a.name;
21+
ALTER TABLE schedule
22+
ALTER COLUMN agent_id SET NOT NULL;
23+
ALTER TABLE schedule
24+
DROP CONSTRAINT schedule_agent_name_fk;
25+
ALTER TABLE schedule
26+
ADD CONSTRAINT schedule_agent_id_fk
27+
FOREIGN KEY (agent_id) REFERENCES agent (id) ON DELETE CASCADE;
28+
CREATE INDEX ${sql.raw(SCHEDULE_AGENT_ID_IDX)}
29+
ON schedule (tenant_id, agent_id);
30+
`.execute(db);
31+
}
32+
33+
export async function down(db: Kysely<unknown>): Promise<void> {
34+
await sql`
35+
SET LOCAL lock_timeout = '5s';
36+
37+
DROP INDEX IF EXISTS ${sql.raw(SCHEDULE_AGENT_ID_IDX)};
38+
ALTER TABLE schedule
39+
DROP CONSTRAINT IF EXISTS schedule_agent_id_fk;
40+
ALTER TABLE schedule
41+
ADD CONSTRAINT schedule_agent_name_fk
42+
FOREIGN KEY (tenant_id, agent_name) REFERENCES agent (tenant_id, name) ON DELETE CASCADE;
43+
ALTER TABLE schedule
44+
DROP COLUMN agent_id;
45+
`.execute(db);
46+
}

‎packages/trueforge/src/db/postgres/schedule-store/PostgresScheduleStore.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function toScheduleRecord(row: Selectable<ScheduleTable>): ScheduleRecord {
3737
return {
3838
id: row.id,
3939
tenant_id: row.tenant_id,
40+
agent_id: row.agent_id,
4041
agent_name: row.agent_name,
4142
name: row.name,
4243
manifest: parseStoredScheduleManifest(row.manifest),
@@ -140,6 +141,7 @@ export class PostgresScheduleStore implements IScheduleStore<Transaction<Databas
140141
.values({
141142
id: newId(),
142143
tenant_id: input.tenant_id,
144+
agent_id: input.agent_id,
143145
agent_name: input.agent_name,
144146
name: input.name,
145147
manifest: json(input.manifest),

‎packages/trueforge/src/db/postgres/types.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,16 @@ export interface AgentTable {
386386
* Configured schedules — immutable ULID `id` PK.
387387
* PRIMARY KEY (id)
388388
* CREATE INDEX schedule_agent_idx ON schedule (tenant_id, agent_name)
389-
* FK (tenant_id, agent_name) → agent(tenant_id, name) ON DELETE CASCADE
389+
* CREATE INDEX schedule_agent_id_idx ON schedule (tenant_id, agent_id)
390+
* FK (agent_id) → agent(id) ON DELETE CASCADE
390391
*/
391392
export interface ScheduleTable {
392393
/** application-generated (ulid); FK target for schedule_run */
393394
id: string;
394395
tenant_id: string;
395-
/** FK with tenant_id → agent(tenant_id, name). Immutable; agent version resolves at run time. */
396+
/** FK → agent(id). Immutable. */
397+
agent_id: string;
398+
/** Create-time snapshot of registry agent name. */
396399
agent_name: string;
397400
/** Display label; not unique. */
398401
name: string;

‎packages/trueforge/src/db/scheduleStore.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ export function manualRunName(): string {
3838
export interface ScheduleRecord {
3939
id: string;
4040
tenant_id: string;
41-
/** Immutable FK to `agent.name` (with tenant); agent version resolves at run time. */
41+
/** Immutable FK to `agent.id`. */
42+
agent_id: string;
43+
/** Create-time snapshot of registry agent name. */
4244
agent_name: string;
4345
/** Slug-shaped label, unique per agent (`schedule_name_uq`). */
4446
name: string;
@@ -108,6 +110,7 @@ export interface GetScheduleInput {
108110

109111
export interface CreateScheduleInput {
110112
tenant_id: string;
113+
agent_id: string;
111114
agent_name: string;
112115
name: string;
113116
manifest: ScheduleManifest;
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import { sql, type Kysely } from 'kysely';
2+
import { SCHEDULE_AGENT_ID_IDX, SCHEDULE_CREATED_BY_SUBJECT_ID_IDX } from '../../indexes';
3+
4+
/**
5+
* Add NOT NULL `agent_id` on schedule — mirrors
6+
* db/postgres/migrations/20260904_000001_schedule_agent_id.ts.
7+
* Drops the `(tenant_id, agent_name)` FK; binding is `agent(id)` only.
8+
*
9+
* SQLite STRICT cannot ADD a NOT NULL column without a DEFAULT, so rebuild
10+
* `schedule` (FKs off so `schedule_run` rows survive the drop/rename).
11+
* better-sqlite3 prepares one statement per call — keep statements separate.
12+
*/
13+
export async function up(db: Kysely<unknown>): Promise<void> {
14+
await sql`PRAGMA foreign_keys = OFF`.execute(db);
15+
try {
16+
await db.transaction().execute(async trx => {
17+
await sql`
18+
CREATE TABLE schedule_new (
19+
id TEXT NOT NULL,
20+
tenant_id TEXT NOT NULL,
21+
agent_id TEXT NOT NULL,
22+
agent_name TEXT NOT NULL,
23+
name TEXT NOT NULL,
24+
manifest BLOB NOT NULL,
25+
status TEXT NOT NULL CHECK (length(status) <= 16),
26+
created_by_subject BLOB NOT NULL,
27+
created_at TEXT NOT NULL,
28+
updated_at TEXT NOT NULL,
29+
PRIMARY KEY (id),
30+
FOREIGN KEY (agent_id) REFERENCES agent (id) ON DELETE CASCADE
31+
) STRICT
32+
`.execute(trx);
33+
34+
await sql`
35+
INSERT INTO schedule_new (
36+
id,
37+
tenant_id,
38+
agent_id,
39+
agent_name,
40+
name,
41+
manifest,
42+
status,
43+
created_by_subject,
44+
created_at,
45+
updated_at
46+
)
47+
SELECT
48+
s.id,
49+
s.tenant_id,
50+
a.id,
51+
s.agent_name,
52+
s.name,
53+
s.manifest,
54+
s.status,
55+
s.created_by_subject,
56+
s.created_at,
57+
s.updated_at
58+
FROM schedule AS s
59+
INNER JOIN agent AS a
60+
ON a.tenant_id = s.tenant_id
61+
AND a.name = s.agent_name
62+
`.execute(trx);
63+
64+
await sql`DROP TABLE schedule`.execute(trx);
65+
await sql`ALTER TABLE schedule_new RENAME TO schedule`.execute(trx);
66+
67+
await sql`
68+
CREATE INDEX schedule_agent_idx
69+
ON schedule (tenant_id, agent_name)
70+
`.execute(trx);
71+
await sql`
72+
CREATE UNIQUE INDEX schedule_name_uq
73+
ON schedule (tenant_id, agent_name, name)
74+
`.execute(trx);
75+
await sql`
76+
CREATE INDEX ${sql.raw(SCHEDULE_CREATED_BY_SUBJECT_ID_IDX)}
77+
ON schedule (tenant_id, json_extract(created_by_subject, '$.subject_id'))
78+
`.execute(trx);
79+
await sql`
80+
CREATE INDEX ${sql.raw(SCHEDULE_AGENT_ID_IDX)}
81+
ON schedule (tenant_id, agent_id)
82+
`.execute(trx);
83+
});
84+
} finally {
85+
await sql`PRAGMA foreign_keys = ON`.execute(db);
86+
}
87+
}
88+
89+
export async function down(db: Kysely<unknown>): Promise<void> {
90+
await sql`PRAGMA foreign_keys = OFF`.execute(db);
91+
try {
92+
await db.transaction().execute(async trx => {
93+
await sql`
94+
CREATE TABLE schedule_old (
95+
id TEXT NOT NULL,
96+
tenant_id TEXT NOT NULL,
97+
agent_name TEXT NOT NULL,
98+
name TEXT NOT NULL,
99+
manifest BLOB NOT NULL,
100+
status TEXT NOT NULL CHECK (length(status) <= 16),
101+
created_by_subject BLOB NOT NULL,
102+
created_at TEXT NOT NULL,
103+
updated_at TEXT NOT NULL,
104+
PRIMARY KEY (id),
105+
FOREIGN KEY (tenant_id, agent_name) REFERENCES agent (tenant_id, name) ON DELETE CASCADE
106+
) STRICT
107+
`.execute(trx);
108+
109+
await sql`
110+
INSERT INTO schedule_old (
111+
id, tenant_id, agent_name, name, manifest, status, created_by_subject, created_at, updated_at
112+
)
113+
SELECT
114+
id, tenant_id, agent_name, name, manifest, status, created_by_subject, created_at, updated_at
115+
FROM schedule
116+
`.execute(trx);
117+
118+
await sql`DROP TABLE schedule`.execute(trx);
119+
await sql`ALTER TABLE schedule_old RENAME TO schedule`.execute(trx);
120+
121+
await sql`
122+
CREATE INDEX schedule_agent_idx
123+
ON schedule (tenant_id, agent_name)
124+
`.execute(trx);
125+
await sql`
126+
CREATE UNIQUE INDEX schedule_name_uq
127+
ON schedule (tenant_id, agent_name, name)
128+
`.execute(trx);
129+
await sql`
130+
CREATE INDEX ${sql.raw(SCHEDULE_CREATED_BY_SUBJECT_ID_IDX)}
131+
ON schedule (tenant_id, json_extract(created_by_subject, '$.subject_id'))
132+
`.execute(trx);
133+
});
134+
} finally {
135+
await sql`PRAGMA foreign_keys = ON`.execute(db);
136+
}
137+
}

‎packages/trueforge/src/db/sqlite/schedule-store/SqliteScheduleStore.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function scheduleColumns(eb: ExpressionBuilder<Database, 'schedule'>) {
3939
return [
4040
'id' as const,
4141
'tenant_id' as const,
42+
'agent_id' as const,
4243
'agent_name' as const,
4344
'name' as const,
4445
jsonText<ScheduleManifest>(eb.ref('manifest')).as('manifest'),
@@ -67,6 +68,7 @@ function runColumns(eb: ExpressionBuilder<Database, 'schedule_run'>) {
6768
interface ScheduleRow {
6869
id: string;
6970
tenant_id: string;
71+
agent_id: string;
7072
agent_name: string;
7173
name: string;
7274
manifest: ScheduleManifest;
@@ -146,6 +148,7 @@ export class SqliteScheduleStore implements IScheduleStore<Transaction<Database>
146148
.values({
147149
id: newId(),
148150
tenant_id: input.tenant_id,
151+
agent_id: input.agent_id,
149152
agent_name: input.agent_name,
150153
name: input.name,
151154
manifest: jsonbBind(input.manifest),

‎packages/trueforge/src/db/sqlite/types.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,15 @@ export interface AgentTable {
231231
/**
232232
* Configured schedules.
233233
* PRIMARY KEY (id).
234-
* FK (tenant_id, agent_name) → agent(tenant_id, name) ON DELETE CASCADE.
234+
* FK (agent_id) → agent(id) ON DELETE CASCADE.
235235
*/
236236
export interface ScheduleTable {
237237
/** application-generated (ulid); FK target for schedule_run */
238238
id: string;
239239
tenant_id: string;
240-
/** FK with tenant_id → agent(tenant_id, name). Immutable; agent version resolves at run time. */
240+
/** FK → agent(id). Immutable. */
241+
agent_id: string;
242+
/** Create-time snapshot of registry agent name. */
241243
agent_name: string;
242244
/** Display label; not unique. */
243245
name: string;

0 commit comments

Comments
 (0)