@pikku/kysely 0.13.33 → 0.13.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { CamelCasePlugin } from 'kysely';
1
+ import { CamelCasePlugin, sql } from 'kysely';
2
2
  import { schemaContext, } from './pikku-schema.types.js';
3
3
  import { agentSchema } from './agent.schema.js';
4
4
  import { analyticsSchema } from './analytics.schema.js';
@@ -102,10 +102,30 @@ const bind = (db) => db.withPlugin(new CamelCasePlugin());
102
102
  * connection they are handed. A caller that had to remember to say the schema
103
103
  * twice would eventually say it once.
104
104
  */
105
- const boundSchema = (db) => CREATE_TABLE.exec(db.schema.createTable('pikkuSchemaProbe').addColumn('id', 'text').compile()
106
- .sql)?.[1];
107
- /** The context for statements compiled against `db`, matching its binding. */
108
- const contextFor = (db) => schemaContext(boundSchema(db));
105
+ const probe = (db) => db.schema.createTable('pikkuSchemaProbe').addColumn('id', 'text').compile()
106
+ .sql;
107
+ /**
108
+ * The context for statements compiled against `db`, matching its binding and
109
+ * its dialect — MySQL is recognised by the backtick it quotes with.
110
+ */
111
+ const contextFor = (db) => {
112
+ const sql = probe(db);
113
+ return schemaContext(CREATE_TABLE.exec(sql)?.[1], {
114
+ mysql: sql.includes('`'),
115
+ });
116
+ };
117
+ /**
118
+ * A MySQL column's full declared type — `varchar(36)`, not the `varchar` the
119
+ * introspector reports. The length is part of the type a foreign key has to
120
+ * match, and a bare `varchar` is not a type MySQL accepts.
121
+ */
122
+ const mysqlColumnType = async (db, table, column) => {
123
+ const { rows } = await sql `
124
+ select column_type as type from information_schema.columns
125
+ where table_schema = database() and table_name = ${table} and column_name = ${column}
126
+ `.execute(db);
127
+ return rows[0].type;
128
+ };
109
129
  /**
110
130
  * Look up every prerequisite in `db`, reporting the gaps and the types found.
111
131
  *
@@ -122,6 +142,7 @@ export const resolveRequirements = async (db, schemas = pikkuSchemas) => {
122
142
  table.name,
123
143
  table,
124
144
  ]));
145
+ const mysql = contextFor(bind(db)).mysql;
125
146
  const types = {};
126
147
  const unmet = [];
127
148
  for (const schema of schemas) {
@@ -133,7 +154,9 @@ export const resolveRequirements = async (db, schemas = pikkuSchemas) => {
133
154
  unmet.push({ schema, requirement });
134
155
  }
135
156
  else {
136
- types[`${requirement.table}.${requirement.column}`] = column.dataType;
157
+ types[`${requirement.table}.${requirement.column}`] = mysql
158
+ ? await mysqlColumnType(db, requirement.table, requirement.column)
159
+ : column.dataType;
137
160
  }
138
161
  }
139
162
  }
@@ -234,7 +257,7 @@ const displayName = (table) => table.schema ? `${table.schema}.${table.name}` :
234
257
  * identifier — matching the first one instead reads a `withSchema('app')`
235
258
  * connection's DDL as a table called `app`, which is no table at all.
236
259
  */
237
- const CREATE_TABLE = /^\s*create table (?:if not exists\s+)?(?:"([^"]+)"\.)?"([^"]+)"/i;
260
+ const CREATE_TABLE = /^\s*create table (?:if not exists\s+)?(?:["`]([^"`]+)["`]\.)?["`]([^"`]+)["`]/i;
238
261
  /**
239
262
  * The tables a schema creates, read back out of its own compiled SQL.
240
263
  *
@@ -2,10 +2,10 @@ export const leaseSchema = {
2
2
  name: 'lease',
3
3
  ownedBy: ['leaseService', 'workflowService'],
4
4
  statements: [
5
- (db) => db.schema
5
+ (db, _types, ctx) => db.schema
6
6
  .createTable('pikkuLease')
7
- .addColumn('key', 'text', (col) => col.primaryKey())
8
- .addColumn('holder', 'text', (col) => col.notNull())
7
+ .addColumn('key', ctx.key, (col) => col.primaryKey())
8
+ .addColumn('holder', ctx.text, (col) => col.notNull())
9
9
  .addColumn('token', 'integer', (col) => col.notNull())
10
10
  .addColumn('expiresAt', 'bigint', (col) => col.notNull()),
11
11
  ],
@@ -1,4 +1,5 @@
1
- import { type CompiledQuery, type Kysely, type RawBuilder } from 'kysely';
1
+ import { type ColumnDefinitionBuilder, type CompiledQuery, type CreateTableBuilder, type Kysely, type RawBuilder } from 'kysely';
2
+ type DataTypeExpression = Parameters<CreateTableBuilder<any, any>['addColumn']>[1];
2
3
  /**
3
4
  * One DDL statement, bound to a database.
4
5
  *
@@ -21,6 +22,12 @@ export type SchemaStatementFactory = (db: Kysely<any>, types: RequiredTypes, ctx
21
22
  * statement that spells its own SQL has to spell the qualifier too.
22
23
  */
23
24
  export interface SchemaContext {
25
+ /**
26
+ * Whether the connection is MySQL, which is the one engine the declarations
27
+ * cannot spell the same way: it refuses a `TEXT` column in a key or with a
28
+ * literal default, and quotes with backticks.
29
+ */
30
+ mysql: boolean;
24
31
  /**
25
32
  * A table as the surrounding DDL addresses it: `"app"."credentials"` on a
26
33
  * schema-bound connection, `"credentials"` otherwise.
@@ -30,6 +37,36 @@ export interface SchemaContext {
30
37
  * physical names throughout and this is no exception.
31
38
  */
32
39
  table: (name: string) => RawBuilder<unknown>;
40
+ /**
41
+ * The type of a text column that is part of a key — primary, foreign, unique
42
+ * or indexed. `text` everywhere but MySQL, where a key needs a bounded type.
43
+ */
44
+ key: DataTypeExpression;
45
+ /**
46
+ * The type of free text of any length. `text` everywhere but MySQL, where
47
+ * `TEXT` stops at 64KB and these columns hold serialized state and payloads.
48
+ */
49
+ text: DataTypeExpression;
50
+ /**
51
+ * A literal default for a text column. MySQL refuses one on `TEXT`, so it
52
+ * takes the parenthesised expression form (8.0.13+); everywhere else the
53
+ * literal itself.
54
+ */
55
+ defaultText: (value: string) => string | RawBuilder<unknown>;
56
+ /**
57
+ * An inline `references ... on delete cascade` on a column.
58
+ *
59
+ * A no-op on MySQL, which parses an inline `REFERENCES` and discards it — the
60
+ * constraint never exists. There the same relationship is declared once, at
61
+ * table level, through `foreignKeys`.
62
+ */
63
+ references: (column: ColumnDefinitionBuilder, target: string) => ColumnDefinitionBuilder;
64
+ /**
65
+ * The table-level foreign keys MySQL needs in place of inline `references`,
66
+ * keyed by local column to `table.column` target. Identity everywhere else,
67
+ * where `references` has already done the work.
68
+ */
69
+ foreignKeys: (table: string, keys: Record<string, string>) => <T extends CreateTableBuilder<any, any>>(builder: T) => T;
33
70
  }
34
71
  /**
35
72
  * The `SchemaContext` for DDL that names no schema.
@@ -44,7 +81,9 @@ export declare const unqualifiedContext: SchemaContext;
44
81
  * Identifiers are quoted rather than interpolated bare so a schema named after
45
82
  * a reserved word still compiles.
46
83
  */
47
- export declare const schemaContext: (schema: string | undefined) => SchemaContext;
84
+ export declare const schemaContext: (schema: string | undefined, options?: {
85
+ mysql?: boolean;
86
+ }) => SchemaContext;
48
87
  /**
49
88
  * The physical types of the columns named in `requires`, keyed `table.column`.
50
89
  *
@@ -129,3 +168,4 @@ export interface SchemaRequirement {
129
168
  */
130
169
  export declare function rawStatement(fragment: RawBuilder<unknown>): SchemaStatementFactory;
131
170
  export declare function rawStatement(build: (ctx: SchemaContext) => RawBuilder<unknown>): SchemaStatementFactory;
171
+ export {};
@@ -1,22 +1,38 @@
1
- import { sql } from 'kysely';
1
+ import { sql, } from 'kysely';
2
2
  /**
3
3
  * The `SchemaContext` for DDL that names no schema.
4
4
  *
5
5
  * The default everywhere a schema has not been asked for, which is every engine
6
6
  * but postgres and most postgres projects too.
7
7
  */
8
- export const unqualifiedContext = {
9
- table: (name) => sql.raw(`"${name}"`),
10
- };
8
+ export const unqualifiedContext = buildContext(undefined, false);
9
+ function buildContext(schema, mysql) {
10
+ const quote = mysql ? '`' : '"';
11
+ const name = (table) => schema
12
+ ? `${quote}${schema}${quote}.${quote}${table}${quote}`
13
+ : `${quote}${table}${quote}`;
14
+ return {
15
+ mysql,
16
+ table: (table) => sql.raw(name(table)),
17
+ key: mysql ? sql.raw('varchar(255)') : 'text',
18
+ text: mysql ? sql.raw('longtext') : 'text',
19
+ defaultText: (value) => (mysql ? sql `(${sql.lit(value)})` : value),
20
+ references: (column, target) => mysql ? column : column.references(target).onDelete('cascade'),
21
+ foreignKeys: (table, keys) => (builder) => mysql
22
+ ? Object.entries(keys).reduce((acc, [column, target]) => {
23
+ const [targetTable, targetColumn] = target.split('.');
24
+ return acc.addForeignKeyConstraint(`${table}_${column}_fk`, [column], targetTable, [targetColumn], (fk) => fk.onDelete('cascade'));
25
+ }, builder)
26
+ : builder,
27
+ };
28
+ }
11
29
  /**
12
30
  * A `SchemaContext` qualifying every table into `schema`.
13
31
  *
14
32
  * Identifiers are quoted rather than interpolated bare so a schema named after
15
33
  * a reserved word still compiles.
16
34
  */
17
- export const schemaContext = (schema) => schema
18
- ? { table: (name) => sql.raw(`"${schema}"."${name}"`) }
19
- : unqualifiedContext;
35
+ export const schemaContext = (schema, options = {}) => buildContext(schema, options.mysql ?? false);
20
36
  /**
21
37
  * The declared type for a foreign key onto a column another source owns.
22
38
  *
@@ -21,15 +21,15 @@ export const scopeSchema = {
21
21
  ownedBy: ['scopeService'],
22
22
  requires: [{ table: 'user', column: 'id', owner: 'Better Auth' }],
23
23
  statements: [
24
- (db) => db.schema
24
+ (db, _types, ctx) => db.schema
25
25
  .createTable('pikkuScopes')
26
- .addColumn('name', 'text', (col) => col.primaryKey())
27
- .addColumn('description', 'text')
26
+ .addColumn('name', ctx.key, (col) => col.primaryKey())
27
+ .addColumn('description', ctx.text)
28
28
  .addColumn('declared', 'boolean', (col) => col.defaultTo(true).notNull()),
29
- (db) => db.schema
29
+ (db, _types, ctx) => db.schema
30
30
  .createTable('pikkuRoles')
31
- .addColumn('name', 'text', (col) => col.primaryKey())
32
- .addColumn('description', 'text')
31
+ .addColumn('name', ctx.key, (col) => col.primaryKey())
32
+ .addColumn('description', ctx.text)
33
33
  // Declared in code with `defineSystemRole` rather than composed by an
34
34
  // admin. What it buys is the refusals: a system role cannot be renamed,
35
35
  // re-scoped or deleted from the console, and a console role cannot be
@@ -43,30 +43,42 @@ export const scopeSchema = {
43
43
  // be able to cause.
44
44
  .addColumn('declared', 'boolean', (col) => col.defaultTo(true).notNull())
45
45
  .addColumn('createdAt', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull()),
46
- (db) => db.schema
46
+ (db, _types, ctx) => db.schema
47
47
  .createTable('pikkuRoleScopes')
48
- .addColumn('role', 'text', (col) => col.notNull().references('pikkuRoles.name').onDelete('cascade'))
49
- .addColumn('scope', 'text', (col) => col.notNull().references('pikkuScopes.name').onDelete('cascade'))
50
- .addPrimaryKeyConstraint('pikku_role_scopes_pk', ['role', 'scope']),
51
- (db, types) => db.schema
48
+ .addColumn('role', ctx.key, (col) => ctx.references(col.notNull(), 'pikkuRoles.name'))
49
+ .addColumn('scope', ctx.key, (col) => ctx.references(col.notNull(), 'pikkuScopes.name'))
50
+ .addPrimaryKeyConstraint('pikku_role_scopes_pk', ['role', 'scope'])
51
+ .$call(ctx.foreignKeys('pikkuRoleScopes', {
52
+ role: 'pikkuRoles.name',
53
+ scope: 'pikkuScopes.name',
54
+ })),
55
+ (db, types, ctx) => db.schema
52
56
  .createTable('pikkuUserRole')
53
- .addColumn('userId', requiredType(types, 'user', 'id'), (col) => col.notNull().references('user.id').onDelete('cascade'))
54
- .addColumn('role', 'text', (col) => col.notNull().references('pikkuRoles.name').onDelete('cascade'))
55
- .addColumn('grantedBy', 'text')
57
+ .addColumn('userId', requiredType(types, 'user', 'id'), (col) => ctx.references(col.notNull(), 'user.id'))
58
+ .addColumn('role', ctx.key, (col) => ctx.references(col.notNull(), 'pikkuRoles.name'))
59
+ .addColumn('grantedBy', ctx.text)
56
60
  .addColumn('grantedAt', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull())
57
- .addPrimaryKeyConstraint('pikku_user_role_pk', ['userId', 'role']),
58
- (db, types) => db.schema
61
+ .addPrimaryKeyConstraint('pikku_user_role_pk', ['userId', 'role'])
62
+ .$call(ctx.foreignKeys('pikkuUserRole', {
63
+ userId: 'user.id',
64
+ role: 'pikkuRoles.name',
65
+ })),
66
+ (db, types, ctx) => db.schema
59
67
  .createTable('pikkuUserScope')
60
- .addColumn('userId', requiredType(types, 'user', 'id'), (col) => col.notNull().references('user.id').onDelete('cascade'))
61
- .addColumn('scope', 'text', (col) => col.notNull().references('pikkuScopes.name').onDelete('cascade'))
62
- .addColumn('grantedBy', 'text')
68
+ .addColumn('userId', requiredType(types, 'user', 'id'), (col) => ctx.references(col.notNull(), 'user.id'))
69
+ .addColumn('scope', ctx.key, (col) => ctx.references(col.notNull(), 'pikkuScopes.name'))
70
+ .addColumn('grantedBy', ctx.text)
63
71
  .addColumn('grantedAt', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull())
64
- .addPrimaryKeyConstraint('pikku_user_scope_pk', ['userId', 'scope']),
65
- (db) => db.schema
72
+ .addPrimaryKeyConstraint('pikku_user_scope_pk', ['userId', 'scope'])
73
+ .$call(ctx.foreignKeys('pikkuUserScope', {
74
+ userId: 'user.id',
75
+ scope: 'pikkuScopes.name',
76
+ })),
77
+ (db, _types, ctx) => db.schema
66
78
  .createIndex('pikku_role_scopes_scope_idx')
67
79
  .on('pikkuRoleScopes')
68
80
  .column('scope'),
69
- (db) => db.schema
81
+ (db, _types, ctx) => db.schema
70
82
  .createIndex('pikku_user_role_role_idx')
71
83
  .on('pikkuUserRole')
72
84
  .column('role'),
@@ -11,20 +11,20 @@ import { sql } from 'kysely';
11
11
  export const secretSchema = {
12
12
  name: 'secret',
13
13
  statements: [
14
- (db) => db.schema
14
+ (db, _types, ctx) => db.schema
15
15
  .createTable('secrets')
16
16
  .addColumn('key', 'varchar(255)', (col) => col.primaryKey())
17
- .addColumn('ciphertext', 'text', (col) => col.notNull())
18
- .addColumn('wrappedDek', 'text', (col) => col.notNull())
17
+ .addColumn('ciphertext', ctx.text, (col) => col.notNull())
18
+ .addColumn('wrappedDek', ctx.text, (col) => col.notNull())
19
19
  .addColumn('keyVersion', 'integer', (col) => col.notNull())
20
20
  .addColumn('createdAt', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull())
21
21
  .addColumn('updatedAt', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull()),
22
- (db) => db.schema
22
+ (db, _types, ctx) => db.schema
23
23
  .createTable('secretKekSalts')
24
24
  .addColumn('keyVersion', 'integer', (col) => col.primaryKey())
25
25
  .addColumn('salt', 'varchar(64)', (col) => col.notNull())
26
26
  .addColumn('createdAt', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull()),
27
- (db) => db.schema
27
+ (db, _types, ctx) => db.schema
28
28
  .createTable('secretsAudit')
29
29
  .addColumn('id', 'varchar(36)', (col) => col.primaryKey())
30
30
  .addColumn('secretKey', 'varchar(255)', (col) => col.notNull())
@@ -3,10 +3,10 @@ import { sql } from 'kysely';
3
3
  export const sessionSchema = {
4
4
  name: 'session',
5
5
  statements: [
6
- (db) => db.schema
6
+ (db, _types, ctx) => db.schema
7
7
  .createTable('pikkuUserSessions')
8
- .addColumn('pikkuUserId', 'text', (col) => col.primaryKey())
9
- .addColumn('session', 'text', (col) => col.notNull())
8
+ .addColumn('pikkuUserId', ctx.key, (col) => col.primaryKey())
9
+ .addColumn('session', ctx.text, (col) => col.notNull())
10
10
  .addColumn('createdAt', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull())
11
11
  .addColumn('updatedAt', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull()),
12
12
  ],
@@ -7,17 +7,17 @@ export const triggerSourceSchema = {
7
7
  name: 'trigger-source',
8
8
  ownedBy: ['triggerSourceStore'],
9
9
  statements: [
10
- (db) => db.schema
10
+ (db, _types, ctx) => db.schema
11
11
  .createTable('pikkuTriggerSource')
12
- .addColumn('name', 'text', (col) => col.primaryKey())
13
- .addColumn('kind', 'text', (col) => col.notNull())
14
- .addColumn('baseUrl', 'text')
15
- .addColumn('labelPrefix', 'text')
12
+ .addColumn('name', ctx.key, (col) => col.primaryKey())
13
+ .addColumn('kind', ctx.text, (col) => col.notNull())
14
+ .addColumn('baseUrl', ctx.text)
15
+ .addColumn('labelPrefix', ctx.text)
16
16
  .addColumn('declared', 'boolean', (col) => col.defaultTo(true).notNull())
17
17
  .addColumn('enabled', 'boolean', (col) => col.defaultTo(false).notNull())
18
- .addColumn('status', 'text')
19
- .addColumn('state', 'text')
20
- .addColumn('detail', 'text')
18
+ .addColumn('status', ctx.text)
19
+ .addColumn('state', ctx.text)
20
+ .addColumn('detail', ctx.text)
21
21
  .addColumn('updatedAt', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull()),
22
22
  ],
23
23
  };
@@ -15,7 +15,7 @@ export const virtualUserScheduleSchema = {
15
15
  name: 'virtual-user-schedule',
16
16
  ownedBy: ['virtualUserScheduleStore'],
17
17
  statements: [
18
- (db) => db.schema
18
+ (db, _types, ctx) => db.schema
19
19
  .createTable('virtualUserSchedule')
20
20
  .addColumn('persona', 'varchar(255)', (col) => col.primaryKey())
21
21
  // 0 or 1 rather than a boolean, matching the run tables: a bare sqlite
@@ -23,8 +23,8 @@ export const virtualUserScheduleSchema = {
23
23
  // everywhere. Off by default, so creating the table spends nothing.
24
24
  .addColumn('enabled', 'integer', (col) => col.defaultTo(0).notNull())
25
25
  .addColumn('disposition', 'varchar(50)', (col) => col.notNull())
26
- .addColumn('goals', 'text', (col) => col.defaultTo('[]').notNull())
27
- .addColumn('budget', 'text')
26
+ .addColumn('goals', ctx.text, (col) => col.defaultTo(ctx.defaultText('[]')).notNull())
27
+ .addColumn('budget', ctx.text)
28
28
  // The gap to the next run is drawn between these rather than fixed, so
29
29
  // the persona does not keep an appointment.
30
30
  .addColumn('minIntervalMs', 'bigint', (col) => col.notNull())
@@ -35,7 +35,7 @@ export const virtualUserScheduleSchema = {
35
35
  .addColumn('lastRunId', 'varchar(36)')
36
36
  .addColumn('lastRunAt', 'timestamp'),
37
37
  // The only read that matters at speed: which rows are due, on every tick.
38
- (db) => db.schema
38
+ (db, _types, ctx) => db.schema
39
39
  .createIndex('idx_virtual_user_schedule_due')
40
40
  .on('virtualUserSchedule')
41
41
  .columns(['enabled', 'nextRunAt']),
@@ -21,7 +21,7 @@ export const virtualUserSchema = {
21
21
  name: 'virtual-user',
22
22
  ownedBy: ['virtualUserRunStore'],
23
23
  statements: [
24
- (db) => db.schema
24
+ (db, _types, ctx) => db.schema
25
25
  .createTable('virtualUserRun')
26
26
  .addColumn('runId', 'varchar(36)', (col) => col.primaryKey())
27
27
  .addColumn('persona', 'varchar(255)', (col) => col.notNull())
@@ -30,29 +30,29 @@ export const virtualUserSchema = {
30
30
  // running | completed | failed. Not derived from `finishedAt` being
31
31
  // null: a crashed run has no finish time either, and "crashed" and
32
32
  // "still going" are not the same answer.
33
- .addColumn('status', 'varchar(50)', (col) => col.defaultTo('running').notNull())
34
- .addColumn('goals', 'text', (col) => col.defaultTo('[]').notNull())
35
- .addColumn('memory', 'text', (col) => col.defaultTo('{}').notNull())
36
- .addColumn('findings', 'text', (col) => col.defaultTo('[]').notNull())
33
+ .addColumn('status', 'varchar(50)', (col) => col.defaultTo(ctx.defaultText('running')).notNull())
34
+ .addColumn('goals', ctx.text, (col) => col.defaultTo(ctx.defaultText('[]')).notNull())
35
+ .addColumn('memory', ctx.text, (col) => col.defaultTo(ctx.defaultText('{}')).notNull())
36
+ .addColumn('findings', ctx.text, (col) => col.defaultTo(ctx.defaultText('[]')).notNull())
37
37
  // What the user set out to do and how far each one got. On the run row
38
38
  // rather than beside the steps: there are a handful of them, and every
39
39
  // read of the run wants them.
40
- .addColumn('intents', 'text', (col) => col.defaultTo('[]').notNull())
40
+ .addColumn('intents', ctx.text, (col) => col.defaultTo(ctx.defaultText('[]')).notNull())
41
41
  // Counts the engine kept: steps, calls, mutations, tokens, elapsed.
42
- .addColumn('tally', 'text')
42
+ .addColumn('tally', ctx.text)
43
43
  .addColumn('stoppedBy', 'varchar(255)')
44
44
  // Why the run itself failed, as opposed to what it found.
45
- .addColumn('error', 'text')
45
+ .addColumn('error', ctx.text)
46
46
  .addColumn('startedBy', 'varchar(255)')
47
47
  .addColumn('createdAt', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull())
48
48
  .addColumn('finishedAt', 'timestamp'),
49
49
  // The two reads: the run list newest-first, and one persona's history when
50
50
  // a finding needs comparing against what that persona found before.
51
- (db) => db.schema
51
+ (db, _types, ctx) => db.schema
52
52
  .createIndex('idx_virtual_user_run_created')
53
53
  .on('virtualUserRun')
54
54
  .column('createdAt'),
55
- (db) => db.schema
55
+ (db, _types, ctx) => db.schema
56
56
  .createIndex('idx_virtual_user_run_persona')
57
57
  .on('virtualUserRun')
58
58
  .columns(['persona', 'createdAt']),
@@ -62,7 +62,7 @@ export const virtualUserSchema = {
62
62
  // created by the store at boot on whatever engine the project uses, and a
63
63
  // cascade that half the engines enforce differently is worse than the read
64
64
  // rule that a step with no run is orphaned.
65
- (db) => db.schema
65
+ (db, _types, ctx) => db.schema
66
66
  .createTable('virtualUserRunStep')
67
67
  .addColumn('runId', 'varchar(36)', (col) => col.notNull())
68
68
  // The engine's own step number, which is what the intent records point
@@ -72,7 +72,7 @@ export const virtualUserSchema = {
72
72
  .addColumn('intentId', 'varchar(255)')
73
73
  // The action as the engine scheduled it, including the `invalid` shape
74
74
  // for a turn the model got wrong — that turn is the interesting one.
75
- .addColumn('action', 'text', (col) => col.notNull())
75
+ .addColumn('action', ctx.text, (col) => col.notNull())
76
76
  .addColumn('status', 'integer')
77
77
  // 0 or 1 rather than a boolean, because a bare sqlite driver cannot
78
78
  // bind one at all and `SerializePlugin` is not installed everywhere.
@@ -81,8 +81,8 @@ export const virtualUserSchema = {
81
81
  // raw: a truncated API response usually starts with a brace, which
82
82
  // `SerializePlugin` would read back as an object rather than the string
83
83
  // the engine actually saw.
84
- .addColumn('response', 'text')
85
- .addColumn('findingKinds', 'text')
84
+ .addColumn('response', ctx.text)
85
+ .addColumn('findingKinds', ctx.text)
86
86
  .addColumn('tokensIn', 'integer', (col) => col.defaultTo(0).notNull())
87
87
  .addColumn('tokensOut', 'integer', (col) => col.defaultTo(0).notNull())
88
88
  .addPrimaryKeyConstraint('pk_virtual_user_run_step', [
@@ -4,34 +4,34 @@ export const webhookSchema = {
4
4
  name: 'webhook',
5
5
  ownedBy: ['webhookService'],
6
6
  statements: [
7
- (db) => db.schema
7
+ (db, _types, ctx) => db.schema
8
8
  .createTable('webhookDelivery')
9
- .addColumn('deliveryId', 'text', (col) => col.primaryKey())
10
- .addColumn('organizationId', 'text')
11
- .addColumn('url', 'text', (col) => col.notNull())
12
- .addColumn('event', 'text')
13
- .addColumn('status', 'text', (col) => col.defaultTo('pending').notNull())
9
+ .addColumn('deliveryId', ctx.key, (col) => col.primaryKey())
10
+ .addColumn('organizationId', ctx.key)
11
+ .addColumn('url', ctx.text, (col) => col.notNull())
12
+ .addColumn('event', ctx.text)
13
+ .addColumn('status', ctx.text, (col) => col.defaultTo(ctx.defaultText('pending')).notNull())
14
14
  .addColumn('attempts', 'integer', (col) => col.defaultTo(0).notNull())
15
15
  .addColumn('createdAt', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull())
16
16
  .addColumn('updatedAt', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull())
17
17
  .addColumn('deliveredAt', 'timestamp'),
18
- (db) => db.schema
18
+ (db, _types, ctx) => db.schema
19
19
  .createTable('webhookDeliveryAttempt')
20
- .addColumn('attemptId', 'text', (col) => col.primaryKey())
21
- .addColumn('deliveryId', 'text', (col) => col
22
- .notNull()
23
- .references('webhookDelivery.deliveryId')
24
- .onDelete('cascade'))
20
+ .addColumn('attemptId', ctx.key, (col) => col.primaryKey())
21
+ .addColumn('deliveryId', ctx.key, (col) => ctx.references(col.notNull(), 'webhookDelivery.deliveryId'))
25
22
  .addColumn('attemptNumber', 'integer', (col) => col.notNull())
26
23
  .addColumn('statusCode', 'integer')
27
- .addColumn('responseBody', 'text')
28
- .addColumn('error', 'text')
29
- .addColumn('createdAt', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull()),
30
- (db) => db.schema
24
+ .addColumn('responseBody', ctx.text)
25
+ .addColumn('error', ctx.text)
26
+ .addColumn('createdAt', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull())
27
+ .$call(ctx.foreignKeys('webhookDeliveryAttempt', {
28
+ deliveryId: 'webhookDelivery.deliveryId',
29
+ })),
30
+ (db, _types, ctx) => db.schema
31
31
  .createIndex('idx_webhook_delivery_org')
32
32
  .on('webhookDelivery')
33
33
  .column('organizationId'),
34
- (db) => db.schema
34
+ (db, _types, ctx) => db.schema
35
35
  .createIndex('idx_webhook_delivery_attempt_delivery')
36
36
  .on('webhookDeliveryAttempt')
37
37
  .column('deliveryId'),