@sixb/sqlite 0.1.19 → 0.1.20

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.
Files changed (57) hide show
  1. package/dist/action-run-storage.d.ts.map +1 -1
  2. package/dist/agents/agent-storage.d.ts.map +1 -1
  3. package/dist/ai-limit-storage.d.ts.map +1 -1
  4. package/dist/ai-usage-storage.d.ts.map +1 -1
  5. package/dist/auth-storage/auth-storage.d.ts.map +1 -1
  6. package/dist/auth-storage/magic-links.d.ts.map +1 -1
  7. package/dist/auth-storage/oidc-attempts.d.ts.map +1 -1
  8. package/dist/auth-storage/rows.d.ts +0 -3
  9. package/dist/auth-storage/rows.d.ts.map +1 -1
  10. package/dist/auth-storage/sessions.d.ts +0 -10
  11. package/dist/auth-storage/sessions.d.ts.map +1 -1
  12. package/dist/auth-storage/shared.d.ts +0 -2
  13. package/dist/auth-storage/shared.d.ts.map +1 -1
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +55 -116
  16. package/dist/index.js.map +27 -27
  17. package/dist/migrations.d.ts +2 -0
  18. package/dist/migrations.d.ts.map +1 -1
  19. package/dist/objects/storage.d.ts.map +1 -1
  20. package/dist/pipeline-run-storage.d.ts.map +1 -1
  21. package/dist/projection-run-storage.d.ts.map +1 -1
  22. package/dist/rules-storage.d.ts.map +1 -1
  23. package/dist/share-grant-storage.d.ts.map +1 -1
  24. package/dist/share-session-storage.d.ts.map +1 -1
  25. package/dist/sync-run-storage.d.ts.map +1 -1
  26. package/dist/timeseries-storage.d.ts.map +1 -1
  27. package/dist/transactions.d.ts +0 -1
  28. package/dist/transactions.d.ts.map +1 -1
  29. package/dist/webhook-run-storage.d.ts.map +1 -1
  30. package/dist/workflow-intervention-storage.d.ts.map +1 -1
  31. package/dist/workflow-run-storage.d.ts.map +1 -1
  32. package/package.json +3 -3
  33. package/src/action-run-storage.ts +0 -5
  34. package/src/agents/agent-storage.ts +0 -5
  35. package/src/ai-limit-storage.ts +0 -2
  36. package/src/ai-usage-storage.ts +0 -4
  37. package/src/auth-storage/auth-storage.ts +0 -23
  38. package/src/auth-storage/magic-links.ts +1 -6
  39. package/src/auth-storage/oidc-attempts.ts +1 -6
  40. package/src/auth-storage/rows.ts +0 -6
  41. package/src/auth-storage/sessions.ts +1 -49
  42. package/src/auth-storage/shared.ts +3 -20
  43. package/src/index.ts +1 -10
  44. package/src/migrations/061-drop-auth-audience.sql +16 -0
  45. package/src/migrations.ts +22 -0
  46. package/src/objects/storage.ts +0 -5
  47. package/src/pipeline-run-storage.ts +0 -5
  48. package/src/projection-run-storage.ts +0 -5
  49. package/src/rules-storage.ts +0 -5
  50. package/src/share-grant-storage.ts +0 -2
  51. package/src/share-session-storage.ts +0 -2
  52. package/src/sync-run-storage.ts +0 -5
  53. package/src/timeseries-storage.ts +0 -5
  54. package/src/transactions.ts +8 -8
  55. package/src/webhook-run-storage.ts +0 -3
  56. package/src/workflow-intervention-storage.ts +0 -5
  57. package/src/workflow-run-storage.ts +0 -5
package/src/index.ts CHANGED
@@ -41,11 +41,7 @@ import { SqliteConnectorConnectionStorage } from "./connector-connection-storage
41
41
  import { SqliteExecutionStorage } from "./execution-storage"
42
42
  import { SqliteFileDownloadGrantStorage } from "./file-download-grant-storage"
43
43
  import { SqliteFileUploadSessionStorage } from "./file-upload-session-storage"
44
- import {
45
- createSqliteStorageMigrators,
46
- installFreshSqliteSchema,
47
- sqliteStoragePath,
48
- } from "./migrations"
44
+ import { createSqliteStorageMigrators, sqliteStoragePath } from "./migrations"
49
45
  import { SqliteObjectStorage } from "./objects"
50
46
  import { SqliteOntologyStorage, type SqliteOntologyTransactionContext } from "./ontology-storage"
51
47
  import { SqlitePipelineRunStorage } from "./pipeline-run-storage"
@@ -127,10 +123,6 @@ export class SqliteStorage implements MigrationCapableStorage {
127
123
  this.readConnection = this.connection
128
124
  }
129
125
 
130
- if (this.connection.installFreshSchema) {
131
- installFreshSqliteSchema(this.connection.db)
132
- }
133
-
134
126
  const stores = createSqliteStores(this.connection, {
135
127
  runOntologyOperation: async (run) => run(),
136
128
  transactionContext: null,
@@ -256,7 +248,6 @@ export class SqliteStorage implements MigrationCapableStorage {
256
248
  {
257
249
  db: this.connection.db,
258
250
  ownsConnection: false,
259
- installFreshSchema: false,
260
251
  },
261
252
  {
262
253
  runOntologyOperation: async (run) => run(),
@@ -0,0 +1,16 @@
1
+ -- A project has one browser app, so sessions, magic links and sign-in attempts no longer name the
2
+ -- one they belong to. Atlas's browser sessions end here: their cookie is no longer read. Native
3
+ -- sessions carried Atlas's name only because the column needed a value, so they stay.
4
+ UPDATE auth_sessions
5
+ SET revoked_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
6
+ WHERE audience = 'atlas'
7
+ AND refresh_token_hash IS NULL
8
+ AND revoked_at IS NULL;
9
+
10
+ DROP INDEX idx_auth_sessions_user_active;
11
+ ALTER TABLE auth_sessions DROP COLUMN audience;
12
+ CREATE INDEX idx_auth_sessions_user_active
13
+ ON auth_sessions(project_id, user_id, revoked_at, expires_at);
14
+
15
+ ALTER TABLE auth_magic_links DROP COLUMN audience;
16
+ ALTER TABLE auth_oidc_authorization_attempts DROP COLUMN audience;
package/src/migrations.ts CHANGED
@@ -272,6 +272,7 @@ export const sqliteStorageMigrations = defineMigrations({
272
272
  sqliteSql("058-file-download-grants", fileDownloadGrantsSql),
273
273
  sqliteSql("059-drained-source-versions", drainedSourceVersionsSql),
274
274
  sqliteSql("060-terminal-action-runs", terminalActionRunsSql),
275
+ sqliteSql("061-drop-auth-audience", dropAuthAudienceSql),
275
276
  ],
276
277
  })
277
278
 
@@ -297,6 +298,26 @@ export function sqliteStep(
297
298
  return step<Database>(id, up, options)
298
299
  }
299
300
 
301
+ // Replaying every migration costs a few hundred milliseconds, and in-memory databases are made
302
+ // often (each test's storage, for one). The schema is built once per process and each new database
303
+ // starts as a copy of it.
304
+ let freshSchema: Uint8Array | undefined
305
+
306
+ /** A new in-memory database holding the current schema, as replaying every migration leaves it. */
307
+ export function freshSqliteDatabase(): Database {
308
+ if (!freshSchema) {
309
+ const template = new Database(":memory:")
310
+ try {
311
+ template.run("PRAGMA foreign_keys = ON")
312
+ installFreshSqliteSchema(template)
313
+ freshSchema = template.serialize()
314
+ } finally {
315
+ template.close()
316
+ }
317
+ }
318
+ return Database.deserialize(freshSchema)
319
+ }
320
+
300
321
  export function installFreshSqliteSchema(db: Database): void {
301
322
  for (const migration of sqliteStorageMigrations.steps) {
302
323
  const installed = migration.up(db)
@@ -599,3 +620,4 @@ import drainedSourceVersionsSql from "./migrations/059-drained-source-versions.s
599
620
  type: "text",
600
621
  }
601
622
  import terminalActionRunsSql from "./migrations/060-terminal-action-runs.sql" with { type: "text" }
623
+ import dropAuthAudienceSql from "./migrations/061-drop-auth-audience.sql" with { type: "text" }
@@ -22,7 +22,6 @@ import type {
22
22
  QueryObjectsInput,
23
23
  QueryObjectsResult,
24
24
  } from "@sixb/core/storage"
25
- import { installFreshSqliteSchema } from "../migrations"
26
25
  import {
27
26
  closeSqliteStoreConnection,
28
27
  openSqliteStoreConnection,
@@ -124,10 +123,6 @@ export class SqliteObjectStorage implements ObjectStorage {
124
123
  this.connection = openSqliteStoreConnection(options)
125
124
  this.db = this.connection.db
126
125
  this.reader = new SqliteObjectReader(this.db, DEFAULT_OBJECT_QUERY_SOURCE)
127
-
128
- if (this.connection.installFreshSchema) {
129
- installFreshSqliteSchema(this.db)
130
- }
131
126
  }
132
127
 
133
128
  queryCapabilities(): ObjectQueryCapabilities {
@@ -22,7 +22,6 @@ import type {
22
22
  } from "@sixb/core/storage"
23
23
  import { PIPELINE_RUN_FAILURE_CODES, PipelineRunError } from "@sixb/core/storage"
24
24
  import { queryLatestRunsByOwnerId } from "./latest-run-query"
25
- import { installFreshSqliteSchema } from "./migrations"
26
25
  import {
27
26
  appendRunListFilters,
28
27
  hasEmptyStatuses,
@@ -54,10 +53,6 @@ export class SqlitePipelineRunStorage implements PipelineRunStorage {
54
53
  this.connection = openSqliteStoreConnection(options)
55
54
  this.db = this.connection.db
56
55
  this.executions = options.executions
57
-
58
- if (this.connection.installFreshSchema) {
59
- installFreshSqliteSchema(this.db)
60
- }
61
56
  }
62
57
 
63
58
  async queue(input: QueuePipelineRunInput): Promise<PipelineRunRecord> {
@@ -56,7 +56,6 @@ import type {
56
56
  } from "@sixb/core/storage"
57
57
  import { PROJECTION_RUN_FAILURE_CODES, ProjectionRunError } from "@sixb/core/storage"
58
58
  import { queryLatestRunsByOwnerId } from "./latest-run-query"
59
- import { installFreshSqliteSchema } from "./migrations"
60
59
  import {
61
60
  closeSqliteStoreConnection,
62
61
  openSqliteStoreConnection,
@@ -82,10 +81,6 @@ export class SqliteProjectionRunStorage implements ProjectionRunStorage {
82
81
  this.connection = openSqliteStoreConnection(options)
83
82
  this.db = this.connection.db
84
83
  this.executions = options.executions
85
-
86
- if (this.connection.installFreshSchema) {
87
- installFreshSqliteSchema(this.db)
88
- }
89
84
  }
90
85
 
91
86
  async queue(input: QueueProjectionRunInput): Promise<ProjectionRunRecord> {
@@ -11,7 +11,6 @@ import type {
11
11
  StoredRuleResolvedEvent,
12
12
  StoredRuleTriggeredEvent,
13
13
  } from "@sixb/core/storage"
14
- import { installFreshSqliteSchema } from "./migrations"
15
14
  import {
16
15
  closeSqliteStoreConnection,
17
16
  openSqliteStoreConnection,
@@ -33,10 +32,6 @@ export class SqliteRulesStorage implements RulesStorage {
33
32
  constructor(options: SqliteRulesStorageOptions = {}) {
34
33
  this.connection = openSqliteStoreConnection(options)
35
34
  this.db = this.connection.db
36
-
37
- if (this.connection.installFreshSchema) {
38
- installFreshSqliteSchema(this.db)
39
- }
40
35
  }
41
36
 
42
37
  async getActive(params: {
@@ -16,7 +16,6 @@ import type {
16
16
  ShareGrantStorage,
17
17
  } from "@sixb/core/storage"
18
18
  import { ShareGrantStorageError } from "@sixb/core/storage"
19
- import { installFreshSqliteSchema } from "./migrations"
20
19
  import { isUniqueConstraintError } from "./storage-errors"
21
20
  import {
22
21
  closeSqliteStoreConnection,
@@ -37,7 +36,6 @@ export class SqliteShareGrantStorage implements ShareGrantStorage {
37
36
  constructor(options: SqliteShareGrantStorageOptions = {}) {
38
37
  this.connection = openSqliteStoreConnection(options)
39
38
  this.db = this.connection.db
40
- if (this.connection.installFreshSchema) installFreshSqliteSchema(this.db)
41
39
  }
42
40
 
43
41
  async create(input: CreateShareGrantInput): Promise<ShareGrantRecord> {
@@ -15,7 +15,6 @@ import type {
15
15
  ShareSessionStorage,
16
16
  } from "@sixb/core/storage"
17
17
  import { ShareSessionStorageError } from "@sixb/core/storage"
18
- import { installFreshSqliteSchema } from "./migrations"
19
18
  import { isForeignKeyConstraintError, isUniqueConstraintError } from "./storage-errors"
20
19
  import {
21
20
  closeSqliteStoreConnection,
@@ -36,7 +35,6 @@ export class SqliteShareSessionStorage implements ShareSessionStorage {
36
35
  constructor(options: SqliteShareSessionStorageOptions = {}) {
37
36
  this.connection = openSqliteStoreConnection(options)
38
37
  this.db = this.connection.db
39
- if (this.connection.installFreshSchema) installFreshSqliteSchema(this.db)
40
38
  }
41
39
 
42
40
  async create(input: CreateShareSessionInput): Promise<ShareSessionRecord> {
@@ -17,7 +17,6 @@ import type {
17
17
  } from "@sixb/core/storage"
18
18
  import { SYNC_RUN_FAILURE_CODES, SyncRunError } from "@sixb/core/storage"
19
19
  import { queryLatestRunsByOwnerId } from "./latest-run-query"
20
- import { installFreshSqliteSchema } from "./migrations"
21
20
  import {
22
21
  appendRunListFilters,
23
22
  hasEmptyStatuses,
@@ -48,10 +47,6 @@ export class SqliteSyncRunStorage implements SyncRunStorage {
48
47
  this.connection = openSqliteStoreConnection(options)
49
48
  this.db = this.connection.db
50
49
  this.executions = options.executions
51
-
52
- if (this.connection.installFreshSchema) {
53
- installFreshSqliteSchema(this.db)
54
- }
55
50
  }
56
51
 
57
52
  async queue(input: QueueSyncRunInput): Promise<SyncRunRecord> {
@@ -5,7 +5,6 @@ import type {
5
5
  TimeseriesPoint,
6
6
  TimeseriesStorage,
7
7
  } from "@sixb/core/storage"
8
- import { installFreshSqliteSchema } from "./migrations"
9
8
  import {
10
9
  closeSqliteStoreConnection,
11
10
  openSqliteStoreConnection,
@@ -31,10 +30,6 @@ export class SqliteTimeseriesStorage implements TimeseriesStorage {
31
30
  constructor(options: SqliteTimeseriesStorageOptions = {}) {
32
31
  this.connection = openSqliteStoreConnection(options)
33
32
  this.db = this.connection.db
34
-
35
- if (this.connection.installFreshSchema) {
36
- installFreshSqliteSchema(this.db)
37
- }
38
33
  }
39
34
 
40
35
  async getHistory(params: {
@@ -2,11 +2,11 @@ import type { Database } from "bun:sqlite"
2
2
  import { Database as SqliteDatabase } from "bun:sqlite"
3
3
  import { mkdirSync } from "node:fs"
4
4
  import { dirname } from "node:path"
5
+ import { freshSqliteDatabase } from "./migrations"
5
6
 
6
7
  export interface SqliteStoreConnection {
7
8
  readonly db: Database
8
9
  readonly ownsConnection: boolean
9
- readonly installFreshSchema: boolean
10
10
  }
11
11
 
12
12
  export interface SqliteStoreConnectionOptions {
@@ -22,20 +22,20 @@ export function openSqliteStoreConnection(
22
22
  return {
23
23
  db: options.connection.db,
24
24
  ownsConnection: false,
25
- installFreshSchema: false,
26
25
  }
27
26
  }
28
27
 
29
28
  const path = options.path ?? ":memory:"
30
29
  if (path !== ":memory:" && !options.readonly) mkdirSync(dirname(path), { recursive: true })
31
- const db = new SqliteDatabase(path, options.readonly ? { readonly: true } : undefined)
30
+ // A file is brought up to date by its migrators; a writable in-memory database starts with the
31
+ // whole schema.
32
+ const db =
33
+ path === ":memory:" && !options.readonly
34
+ ? freshSqliteDatabase()
35
+ : new SqliteDatabase(path, options.readonly ? { readonly: true } : undefined)
32
36
  db.run("PRAGMA foreign_keys = ON")
33
37
  db.run("PRAGMA busy_timeout = 5000")
34
- return {
35
- db,
36
- ownsConnection: true,
37
- installFreshSchema: path === ":memory:" && !options.readonly,
38
- }
38
+ return { db, ownsConnection: true }
39
39
  }
40
40
 
41
41
  export function closeSqliteStoreConnection(connection: SqliteStoreConnection): void {
@@ -13,7 +13,6 @@ import type {
13
13
  WebhookRunStorage,
14
14
  } from "@sixb/core/storage"
15
15
  import { canRetryWebhookRun, WEBHOOK_RUN_FAILURE_CODES, WebhookRunError } from "@sixb/core/storage"
16
- import { installFreshSqliteSchema } from "./migrations"
17
16
  import {
18
17
  appendRunListFilters,
19
18
  hasEmptyStatuses,
@@ -45,8 +44,6 @@ export class SqliteWebhookRunStorage implements WebhookRunStorage {
45
44
  this.connection = openSqliteStoreConnection(options)
46
45
  this.db = this.connection.db
47
46
  this.executions = options.executions
48
-
49
- if (this.connection.installFreshSchema) installFreshSqliteSchema(this.db)
50
47
  }
51
48
 
52
49
  async start(input: StartWebhookRunInput): Promise<WebhookRunRecord> {
@@ -12,7 +12,6 @@ import type {
12
12
  WorkflowInterventionStorage,
13
13
  } from "@sixb/core/storage"
14
14
  import { WorkflowInterventionError } from "@sixb/core/storage"
15
- import { installFreshSqliteSchema } from "./migrations"
16
15
  import { isUniqueConstraintError } from "./storage-errors"
17
16
  import {
18
17
  closeSqliteStoreConnection,
@@ -34,10 +33,6 @@ export class SqliteWorkflowInterventionStorage implements WorkflowInterventionSt
34
33
  constructor(options: SqliteWorkflowInterventionStorageOptions = {}) {
35
34
  this.connection = openSqliteStoreConnection(options)
36
35
  this.db = this.connection.db
37
-
38
- if (this.connection.installFreshSchema) {
39
- installFreshSqliteSchema(this.db)
40
- }
41
36
  }
42
37
 
43
38
  async create(input: CreateWorkflowInterventionInput): Promise<WorkflowInterventionRecord> {
@@ -44,7 +44,6 @@ import {
44
44
  WorkflowRunError,
45
45
  } from "@sixb/core/storage"
46
46
  import { queryLatestRunsByOwnerId } from "./latest-run-query"
47
- import { installFreshSqliteSchema } from "./migrations"
48
47
  import {
49
48
  appendRunListFilters,
50
49
  hasEmptyStatuses,
@@ -80,10 +79,6 @@ export class SqliteWorkflowRunStorage implements WorkflowRunStorage {
80
79
  this.db = this.connection.db
81
80
  this.executions = options.executions
82
81
 
83
- if (this.connection.installFreshSchema) {
84
- installFreshSqliteSchema(this.db)
85
- }
86
-
87
82
  this.nodes = new SqliteWorkflowNodeRunStorage(this.db)
88
83
  this.agentNodes = new SqliteWorkflowAgentNodeRunStorage(this.db, this.executions)
89
84
  }