diff --git a/packages/opencode/migration/20260318163842_events/migration.sql b/packages/opencode/migration/20260323234822_events/migration.sql similarity index 94% rename from packages/opencode/migration/20260318163842_events/migration.sql rename to packages/opencode/migration/20260323234822_events/migration.sql index ac2fe89f97..b0fe7e4e6b 100644 --- a/packages/opencode/migration/20260318163842_events/migration.sql +++ b/packages/opencode/migration/20260323234822_events/migration.sql @@ -7,7 +7,7 @@ CREATE TABLE `event` ( `id` text PRIMARY KEY, `aggregate_id` text NOT NULL, `seq` integer NOT NULL, - `name` text NOT NULL, + `type` text NOT NULL, `data` text NOT NULL, CONSTRAINT `fk_event_aggregate_id_event_sequence_aggregate_id_fk` FOREIGN KEY (`aggregate_id`) REFERENCES `event_sequence`(`aggregate_id`) ON DELETE CASCADE ); diff --git a/packages/opencode/migration/20260318163842_events/snapshot.json b/packages/opencode/migration/20260323234822_events/snapshot.json similarity index 93% rename from packages/opencode/migration/20260318163842_events/snapshot.json rename to packages/opencode/migration/20260323234822_events/snapshot.json index 136223bc45..b8e3932fb2 100644 --- a/packages/opencode/migration/20260318163842_events/snapshot.json +++ b/packages/opencode/migration/20260323234822_events/snapshot.json @@ -1,8 +1,10 @@ { "version": "7", "dialect": "sqlite", - "id": "64c94495-3f7d-4169-a078-2df22ca80057", - "prevIds": ["fb311f30-9948-4131-b15c-7d308478a878"], + "id": "f13dfa58-7fb4-47a2-8f6b-dc70258e14ed", + "prevIds": [ + "37e1554d-af4c-43f2-aa7c-307fb49a315e" + ], "ddl": [ { "name": "account_state", @@ -952,7 +954,7 @@ "autoincrement": false, "default": null, "generated": null, - "name": "name", + "name": "type", "entityType": "columns", "table": "event" }, @@ -967,9 +969,13 @@ "table": "event" }, { - "columns": ["active_account_id"], + "columns": [ + "active_account_id" + ], "tableTo": "account", - "columnsTo": ["id"], + "columnsTo": [ + "id" + ], "onUpdate": "NO ACTION", "onDelete": "SET NULL", "nameExplicit": false, @@ -978,9 +984,13 @@ "table": "account_state" }, { - "columns": ["project_id"], + "columns": [ + "project_id" + ], "tableTo": "project", - "columnsTo": ["id"], + "columnsTo": [ + "id" + ], "onUpdate": "NO ACTION", "onDelete": "CASCADE", "nameExplicit": false, @@ -989,9 +999,13 @@ "table": "workspace" }, { - "columns": ["session_id"], + "columns": [ + "session_id" + ], "tableTo": "session", - "columnsTo": ["id"], + "columnsTo": [ + "id" + ], "onUpdate": "NO ACTION", "onDelete": "CASCADE", "nameExplicit": false, @@ -1000,9 +1014,13 @@ "table": "message" }, { - "columns": ["message_id"], + "columns": [ + "message_id" + ], "tableTo": "message", - "columnsTo": ["id"], + "columnsTo": [ + "id" + ], "onUpdate": "NO ACTION", "onDelete": "CASCADE", "nameExplicit": false, @@ -1011,9 +1029,13 @@ "table": "part" }, { - "columns": ["project_id"], + "columns": [ + "project_id" + ], "tableTo": "project", - "columnsTo": ["id"], + "columnsTo": [ + "id" + ], "onUpdate": "NO ACTION", "onDelete": "CASCADE", "nameExplicit": false, @@ -1022,9 +1044,13 @@ "table": "permission" }, { - "columns": ["project_id"], + "columns": [ + "project_id" + ], "tableTo": "project", - "columnsTo": ["id"], + "columnsTo": [ + "id" + ], "onUpdate": "NO ACTION", "onDelete": "CASCADE", "nameExplicit": false, @@ -1033,9 +1059,13 @@ "table": "session" }, { - "columns": ["session_id"], + "columns": [ + "session_id" + ], "tableTo": "session", - "columnsTo": ["id"], + "columnsTo": [ + "id" + ], "onUpdate": "NO ACTION", "onDelete": "CASCADE", "nameExplicit": false, @@ -1044,9 +1074,13 @@ "table": "todo" }, { - "columns": ["session_id"], + "columns": [ + "session_id" + ], "tableTo": "session", - "columnsTo": ["id"], + "columnsTo": [ + "id" + ], "onUpdate": "NO ACTION", "onDelete": "CASCADE", "nameExplicit": false, @@ -1055,9 +1089,13 @@ "table": "session_share" }, { - "columns": ["aggregate_id"], + "columns": [ + "aggregate_id" + ], "tableTo": "event_sequence", - "columnsTo": ["aggregate_id"], + "columnsTo": [ + "aggregate_id" + ], "onUpdate": "NO ACTION", "onDelete": "CASCADE", "nameExplicit": false, @@ -1066,91 +1104,119 @@ "table": "event" }, { - "columns": ["email", "url"], + "columns": [ + "email", + "url" + ], "nameExplicit": false, "name": "control_account_pk", "entityType": "pks", "table": "control_account" }, { - "columns": ["session_id", "position"], + "columns": [ + "session_id", + "position" + ], "nameExplicit": false, "name": "todo_pk", "entityType": "pks", "table": "todo" }, { - "columns": ["id"], + "columns": [ + "id" + ], "nameExplicit": false, "name": "account_state_pk", "table": "account_state", "entityType": "pks" }, { - "columns": ["id"], + "columns": [ + "id" + ], "nameExplicit": false, "name": "account_pk", "table": "account", "entityType": "pks" }, { - "columns": ["id"], + "columns": [ + "id" + ], "nameExplicit": false, "name": "workspace_pk", "table": "workspace", "entityType": "pks" }, { - "columns": ["id"], + "columns": [ + "id" + ], "nameExplicit": false, "name": "project_pk", "table": "project", "entityType": "pks" }, { - "columns": ["id"], + "columns": [ + "id" + ], "nameExplicit": false, "name": "message_pk", "table": "message", "entityType": "pks" }, { - "columns": ["id"], + "columns": [ + "id" + ], "nameExplicit": false, "name": "part_pk", "table": "part", "entityType": "pks" }, { - "columns": ["project_id"], + "columns": [ + "project_id" + ], "nameExplicit": false, "name": "permission_pk", "table": "permission", "entityType": "pks" }, { - "columns": ["id"], + "columns": [ + "id" + ], "nameExplicit": false, "name": "session_pk", "table": "session", "entityType": "pks" }, { - "columns": ["session_id"], + "columns": [ + "session_id" + ], "nameExplicit": false, "name": "session_share_pk", "table": "session_share", "entityType": "pks" }, { - "columns": ["aggregate_id"], + "columns": [ + "aggregate_id" + ], "nameExplicit": false, "name": "event_sequence_pk", "table": "event_sequence", "entityType": "pks" }, { - "columns": ["id"], + "columns": [ + "id" + ], "nameExplicit": false, "name": "event_pk", "table": "event", @@ -1161,12 +1227,20 @@ { "value": "session_id", "isExpression": false + }, + { + "value": "time_created", + "isExpression": false + }, + { + "value": "id", + "isExpression": false } ], "isUnique": false, "where": null, "origin": "manual", - "name": "message_session_idx", + "name": "message_session_time_created_id_idx", "entityType": "indexes", "table": "message" }, @@ -1175,12 +1249,16 @@ { "value": "message_id", "isExpression": false + }, + { + "value": "id", + "isExpression": false } ], "isUnique": false, "where": null, "origin": "manual", - "name": "part_message_idx", + "name": "part_message_id_id_idx", "entityType": "indexes", "table": "part" }, @@ -1256,4 +1334,4 @@ } ], "renames": [] -} +} \ No newline at end of file diff --git a/packages/opencode/src/sync/event.sql.ts b/packages/opencode/src/sync/event.sql.ts index d17dabd72d..b51b5a5dfe 100644 --- a/packages/opencode/src/sync/event.sql.ts +++ b/packages/opencode/src/sync/event.sql.ts @@ -11,6 +11,6 @@ export const EventTable = sqliteTable("event", { .notNull() .references(() => EventSequenceTable.aggregate_id, { onDelete: "cascade" }), seq: integer().notNull(), - name: text().notNull(), + type: text().notNull(), data: text({ mode: "json" }).$type>().notNull(), }) diff --git a/packages/opencode/src/sync/index.ts b/packages/opencode/src/sync/index.ts index 185e88a893..270950fd4b 100644 --- a/packages/opencode/src/sync/index.ts +++ b/packages/opencode/src/sync/index.ts @@ -133,7 +133,7 @@ export namespace SyncEvent { id: event.id, seq: event.seq, aggregate_id: event.aggregateID, - name: versionedType(def.type, def.version), + type: versionedType(def.type, def.version), data: event.data as Record, }) .run() diff --git a/packages/opencode/test/sync/index.test.ts b/packages/opencode/test/sync/index.test.ts index 7336352299..0e9e596ea8 100644 --- a/packages/opencode/test/sync/index.test.ts +++ b/packages/opencode/test/sync/index.test.ts @@ -10,7 +10,7 @@ import { Identifier } from "../../src/id/id" import { Flag } from "../../src/flag/flag" beforeEach(() => { - Database.Client.reset() + Database.close() }) const original = Flag.OPENCODE_EXPERIMENTAL_WORKSPACES @@ -59,19 +59,19 @@ describe("SyncEvent", () => { test( "inserts event row", withInstance(() => { - SyncEvent.run(Created, { id: "msg_1", name: "first" }) + SyncEvent.run(Created, { id: "evt_1", name: "first" }) const rows = Database.use((db) => db.select().from(EventTable).all()) expect(rows).toHaveLength(1) - expect(rows[0].name).toBe("item.created.1") - expect(rows[0].aggregate_id).toBe("msg_1") + expect(rows[0].type).toBe("item.created.1") + expect(rows[0].aggregate_id).toBe("evt_1") }), ) test( "increments seq per aggregate", withInstance(() => { - SyncEvent.run(Created, { id: "msg_1", name: "first" }) - SyncEvent.run(Created, { id: "msg_1", name: "second" }) + SyncEvent.run(Created, { id: "evt_1", name: "first" }) + SyncEvent.run(Created, { id: "evt_1", name: "second" }) const rows = Database.use((db) => db.select().from(EventTable).all()) expect(rows).toHaveLength(2) expect(rows[1].seq).toBe(rows[0].seq + 1) @@ -81,10 +81,10 @@ describe("SyncEvent", () => { test( "uses custom aggregate field from agg()", withInstance(() => { - SyncEvent.run(Sent, { item_id: "msg_1", to: "james" }) + SyncEvent.run(Sent, { item_id: "evt_1", to: "james" }) const rows = Database.use((db) => db.select().from(EventTable).all()) expect(rows).toHaveLength(1) - expect(rows[0].aggregate_id).toBe("msg_1") + expect(rows[0].aggregate_id).toBe("evt_1") }), ) @@ -93,22 +93,18 @@ describe("SyncEvent", () => { withInstance(async () => { const events: Array<{ type: string - properties: { seq: number; aggregateID: string; data: { id: string; name: string } } + properties: { id: string; name: string } }> = [] const unsub = Bus.subscribeAll((event) => events.push(event)) - SyncEvent.run(Created, { id: "msg_1", name: "test" }) + SyncEvent.run(Created, { id: "evt_1", name: "test" }) expect(events).toHaveLength(1) expect(events[0]).toEqual({ - type: "item.created.1", + type: "item.created", properties: { - seq: 0, - aggregateID: "msg_1", - data: { - id: "msg_1", - name: "test", - }, + id: "evt_1", + name: "test", }, })