openworkflow 0.4.1 → 0.6.0
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.
- package/README.md +43 -345
- package/dist/backend-test/backend.testsuite.d.ts +20 -0
- package/dist/backend-test/backend.testsuite.d.ts.map +1 -0
- package/dist/{core → backend-test}/backend.testsuite.js +191 -59
- package/dist/backend-test/index.d.ts +2 -0
- package/dist/backend-test/index.d.ts.map +1 -0
- package/dist/backend-test/index.js +1 -0
- package/dist/{core/backend.d.ts → backend.d.ts} +7 -5
- package/dist/backend.d.ts.map +1 -0
- package/dist/{core/backend.js → backend.js} +0 -1
- package/dist/backend.testsuite.d.ts +20 -0
- package/dist/backend.testsuite.d.ts.map +1 -0
- package/dist/{core/backend-test-suite.js → backend.testsuite.js} +301 -171
- package/dist/bin/openworkflow.d.ts +3 -0
- package/dist/bin/openworkflow.d.ts.map +1 -0
- package/dist/bin/openworkflow.js +43 -0
- package/dist/chaos.test.d.ts +2 -0
- package/dist/chaos.test.d.ts.map +1 -0
- package/dist/chaos.test.js +88 -0
- package/dist/client.d.ts +141 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/{sdk/sdk.js → client.js} +43 -71
- package/dist/client.test.d.ts +2 -0
- package/dist/client.test.d.ts.map +1 -0
- package/dist/{sdk/sdk.test.js → client.test.js} +130 -14
- package/dist/core/duration.d.ts +4 -2
- package/dist/core/duration.d.ts.map +1 -1
- package/dist/core/duration.js +3 -2
- package/dist/core/duration.test.js +0 -1
- package/dist/core/error.d.ts +14 -0
- package/dist/core/error.d.ts.map +1 -0
- package/dist/core/error.js +17 -0
- package/dist/core/error.test.d.ts +2 -0
- package/dist/core/error.test.d.ts.map +1 -0
- package/dist/core/error.test.js +60 -0
- package/dist/core/json.js +0 -1
- package/dist/core/result.d.ts +14 -4
- package/dist/core/result.d.ts.map +1 -1
- package/dist/core/result.js +10 -1
- package/dist/core/result.test.js +2 -2
- package/dist/core/retry.d.ts +0 -9
- package/dist/core/retry.d.ts.map +1 -1
- package/dist/core/retry.js +0 -15
- package/dist/core/schema.js +0 -1
- package/dist/core/step.d.ts +1 -32
- package/dist/core/step.d.ts.map +1 -1
- package/dist/core/step.js +0 -36
- package/dist/core/step.test.js +1 -75
- package/dist/core/workflow.d.ts +2 -47
- package/dist/core/workflow.d.ts.map +1 -1
- package/dist/core/workflow.js +0 -45
- package/dist/core/workflow.test.js +1 -104
- package/dist/driver.d.ts +116 -0
- package/dist/driver.d.ts.map +1 -0
- package/dist/driver.js +1 -0
- package/dist/{execution/execution.d.ts → execution.d.ts} +4 -26
- package/dist/execution.d.ts.map +1 -0
- package/dist/{execution/execution.js → execution.js} +4 -5
- package/dist/execution.test.d.ts.map +1 -0
- package/dist/{execution/execution.test.js → execution.test.js} +4 -5
- package/dist/factory.d.ts +74 -0
- package/dist/factory.d.ts.map +1 -0
- package/dist/factory.js +72 -0
- package/dist/index.d.ts +6 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -5
- package/dist/internal.d.ts +7 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +2 -0
- package/dist/node-sqlite/backend.d.ts +52 -0
- package/dist/node-sqlite/backend.d.ts.map +1 -0
- package/dist/node-sqlite/backend.js +673 -0
- package/dist/node-sqlite/index.d.ts +11 -0
- package/dist/node-sqlite/index.d.ts.map +1 -0
- package/dist/node-sqlite/index.js +7 -0
- package/dist/node-sqlite/sqlite.d.ts +60 -0
- package/dist/node-sqlite/sqlite.d.ts.map +1 -0
- package/dist/{backend-sqlite → node-sqlite}/sqlite.js +20 -3
- package/dist/postgres/backend.d.ts +44 -0
- package/dist/postgres/backend.d.ts.map +1 -0
- package/dist/postgres/backend.js +534 -0
- package/dist/postgres/backend.test.d.ts +2 -0
- package/dist/postgres/backend.test.d.ts.map +1 -0
- package/dist/postgres/backend.test.js +19 -0
- package/dist/postgres/driver.d.ts +81 -0
- package/dist/postgres/driver.d.ts.map +1 -0
- package/dist/postgres/driver.js +63 -0
- package/dist/postgres/index.d.ts +11 -0
- package/dist/postgres/index.d.ts.map +1 -0
- package/dist/postgres/index.js +7 -0
- package/dist/postgres/internal.d.ts +2 -0
- package/dist/postgres/internal.d.ts.map +1 -0
- package/dist/postgres/internal.js +1 -0
- package/dist/postgres/postgres.d.ts +42 -0
- package/dist/postgres/postgres.d.ts.map +1 -0
- package/dist/postgres/postgres.js +233 -0
- package/dist/postgres/postgres.test.d.ts +2 -0
- package/dist/postgres/postgres.test.d.ts.map +1 -0
- package/dist/postgres/postgres.test.js +45 -0
- package/dist/postgres/scripts/db-migrate.d.ts +2 -0
- package/dist/postgres/scripts/db-migrate.d.ts.map +1 -0
- package/dist/postgres/scripts/db-migrate.js +4 -0
- package/dist/postgres/scripts/db-reset.d.ts +2 -0
- package/dist/postgres/scripts/db-reset.d.ts.map +1 -0
- package/dist/postgres/scripts/db-reset.js +5 -0
- package/dist/postgres/scripts/squawk.d.ts +2 -0
- package/dist/postgres/scripts/squawk.d.ts.map +1 -0
- package/dist/postgres/scripts/squawk.js +16 -0
- package/dist/postgres/vitest.global-setup.d.ts +3 -0
- package/dist/postgres/vitest.global-setup.d.ts.map +1 -0
- package/dist/postgres/vitest.global-setup.js +7 -0
- package/dist/postgres.d.ts +2 -0
- package/dist/postgres.d.ts.map +1 -0
- package/dist/postgres.js +1 -0
- package/dist/registry.d.ts +27 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +48 -0
- package/dist/registry.test.d.ts +2 -0
- package/dist/registry.test.d.ts.map +1 -0
- package/dist/registry.test.js +109 -0
- package/dist/{backend-sqlite → sqlite}/backend.d.ts +8 -4
- package/dist/sqlite/backend.d.ts.map +1 -0
- package/dist/{backend-sqlite → sqlite}/backend.js +35 -9
- package/dist/sqlite/backend.test.d.ts +2 -0
- package/dist/sqlite/backend.test.d.ts.map +1 -0
- package/dist/sqlite/backend.test.js +50 -0
- package/dist/sqlite/driver.d.ts +79 -0
- package/dist/sqlite/driver.d.ts.map +1 -0
- package/dist/sqlite/driver.js +62 -0
- package/dist/sqlite/index.d.ts +13 -0
- package/dist/sqlite/index.d.ts.map +1 -0
- package/dist/sqlite/index.js +11 -0
- package/dist/sqlite/internal.d.ts +2 -0
- package/dist/sqlite/internal.d.ts.map +1 -0
- package/dist/sqlite/internal.js +1 -0
- package/dist/{backend-sqlite → sqlite}/sqlite.d.ts +18 -2
- package/dist/sqlite/sqlite.d.ts.map +1 -0
- package/dist/sqlite/sqlite.js +246 -0
- package/dist/sqlite/sqlite.test.d.ts +2 -0
- package/dist/sqlite/sqlite.test.d.ts.map +1 -0
- package/dist/sqlite/sqlite.test.js +171 -0
- package/dist/sqlite.d.ts +2 -0
- package/dist/sqlite.d.ts.map +1 -0
- package/dist/sqlite.js +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/{worker/worker.d.ts → worker.d.ts} +11 -4
- package/dist/worker.d.ts.map +1 -0
- package/dist/{worker/worker.js → worker.js} +20 -11
- package/dist/{worker/worker.test.d.ts.map → worker.test.d.ts.map} +1 -1
- package/dist/{worker/worker.test.js → worker.test.js} +136 -22
- package/dist/workflow.d.ts +60 -0
- package/dist/workflow.d.ts.map +1 -0
- package/dist/workflow.js +48 -0
- package/dist/workflow.test.d.ts +2 -0
- package/dist/workflow.test.d.ts.map +1 -0
- package/dist/workflow.test.js +84 -0
- package/package.json +28 -4
- package/dist/backend-sqlite/backend.d.ts.map +0 -1
- package/dist/backend-sqlite/backend.js.map +0 -1
- package/dist/backend-sqlite/index.d.ts +0 -2
- package/dist/backend-sqlite/index.d.ts.map +0 -1
- package/dist/backend-sqlite/index.js +0 -2
- package/dist/backend-sqlite/index.js.map +0 -1
- package/dist/backend-sqlite/sqlite.d.ts.map +0 -1
- package/dist/backend-sqlite/sqlite.js.map +0 -1
- package/dist/config/config.d.ts +0 -102
- package/dist/config/config.d.ts.map +0 -1
- package/dist/config/config.js +0 -29
- package/dist/config/config.js.map +0 -1
- package/dist/config/index.d.ts +0 -3
- package/dist/config/index.d.ts.map +0 -1
- package/dist/config/index.js +0 -2
- package/dist/config/index.js.map +0 -1
- package/dist/config.d.ts +0 -28
- package/dist/config.d.ts.map +0 -1
- package/dist/config.js +0 -41
- package/dist/config.js.map +0 -1
- package/dist/core/backend-test-suite.d.ts +0 -22
- package/dist/core/backend-test-suite.d.ts.map +0 -1
- package/dist/core/backend-test-suite.js.map +0 -1
- package/dist/core/backend.d.ts.map +0 -1
- package/dist/core/backend.js.map +0 -1
- package/dist/core/backend.testsuite.d.ts +0 -21
- package/dist/core/backend.testsuite.d.ts.map +0 -1
- package/dist/core/backend.testsuite.js.map +0 -1
- package/dist/core/duration.js.map +0 -1
- package/dist/core/duration.test.js.map +0 -1
- package/dist/core/json.js.map +0 -1
- package/dist/core/result.js.map +0 -1
- package/dist/core/result.test.js.map +0 -1
- package/dist/core/retry.js.map +0 -1
- package/dist/core/retry.test.d.ts +0 -2
- package/dist/core/retry.test.d.ts.map +0 -1
- package/dist/core/retry.test.js +0 -36
- package/dist/core/retry.test.js.map +0 -1
- package/dist/core/schema.js.map +0 -1
- package/dist/core/step.js.map +0 -1
- package/dist/core/step.test.js.map +0 -1
- package/dist/core/workflow.js.map +0 -1
- package/dist/core/workflow.test.js.map +0 -1
- package/dist/execution/execution.d.ts.map +0 -1
- package/dist/execution/execution.js.map +0 -1
- package/dist/execution/execution.test.d.ts.map +0 -1
- package/dist/execution/execution.test.js.map +0 -1
- package/dist/global.d.ts +0 -62
- package/dist/global.d.ts.map +0 -1
- package/dist/global.js +0 -78
- package/dist/global.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/sdk/sdk.d.ts +0 -182
- package/dist/sdk/sdk.d.ts.map +0 -1
- package/dist/sdk/sdk.js.map +0 -1
- package/dist/sdk/sdk.test.d.ts +0 -2
- package/dist/sdk/sdk.test.d.ts.map +0 -1
- package/dist/sdk/sdk.test.js.map +0 -1
- package/dist/worker/worker.d.ts.map +0 -1
- package/dist/worker/worker.js.map +0 -1
- package/dist/worker/worker.test.js.map +0 -1
- /package/dist/{execution/execution.test.d.ts → execution.test.d.ts} +0 -0
- /package/dist/{worker/worker.test.d.ts → worker.test.d.ts} +0 -0
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { DatabaseSync } from "node:sqlite";
|
|
3
|
+
/**
|
|
4
|
+
* newDatabase creates a new SQLite database connection.
|
|
5
|
+
* @param path - Database file path (or ":memory:") for testing
|
|
6
|
+
* @returns SQLite database connection
|
|
7
|
+
*/
|
|
8
|
+
export function newDatabase(path) {
|
|
9
|
+
const db = new DatabaseSync(path);
|
|
10
|
+
// Only enable WAL mode for file-based databases
|
|
11
|
+
if (path !== ":memory:") {
|
|
12
|
+
db.exec("PRAGMA journal_mode = WAL;");
|
|
13
|
+
}
|
|
14
|
+
db.exec("PRAGMA foreign_keys = ON;");
|
|
15
|
+
return db;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* migrations returns the list of migration SQL statements.
|
|
19
|
+
* @returns Migration SQL statements
|
|
20
|
+
*/
|
|
21
|
+
export function migrations() {
|
|
22
|
+
return [
|
|
23
|
+
// 0 - init
|
|
24
|
+
`BEGIN;
|
|
25
|
+
|
|
26
|
+
CREATE TABLE IF NOT EXISTS "openworkflow_migrations" (
|
|
27
|
+
"version" INTEGER NOT NULL PRIMARY KEY
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
INSERT OR IGNORE INTO "openworkflow_migrations" ("version")
|
|
31
|
+
VALUES (0);
|
|
32
|
+
|
|
33
|
+
COMMIT;`,
|
|
34
|
+
// 1 - add workflow_runs and step_attempts tables
|
|
35
|
+
`BEGIN;
|
|
36
|
+
|
|
37
|
+
PRAGMA defer_foreign_keys = ON;
|
|
38
|
+
|
|
39
|
+
CREATE TABLE IF NOT EXISTS "workflow_runs" (
|
|
40
|
+
"namespace_id" TEXT NOT NULL,
|
|
41
|
+
"id" TEXT NOT NULL,
|
|
42
|
+
--
|
|
43
|
+
"workflow_name" TEXT NOT NULL,
|
|
44
|
+
"version" TEXT,
|
|
45
|
+
"status" TEXT NOT NULL,
|
|
46
|
+
"idempotency_key" TEXT,
|
|
47
|
+
"config" TEXT NOT NULL,
|
|
48
|
+
"context" TEXT,
|
|
49
|
+
"input" TEXT,
|
|
50
|
+
"output" TEXT,
|
|
51
|
+
"error" TEXT,
|
|
52
|
+
"attempts" INTEGER NOT NULL,
|
|
53
|
+
"parent_step_attempt_namespace_id" TEXT,
|
|
54
|
+
"parent_step_attempt_id" TEXT,
|
|
55
|
+
"worker_id" TEXT,
|
|
56
|
+
"available_at" TEXT,
|
|
57
|
+
"deadline_at" TEXT,
|
|
58
|
+
"started_at" TEXT,
|
|
59
|
+
"finished_at" TEXT,
|
|
60
|
+
"created_at" TEXT NOT NULL,
|
|
61
|
+
"updated_at" TEXT NOT NULL,
|
|
62
|
+
PRIMARY KEY ("namespace_id", "id"),
|
|
63
|
+
FOREIGN KEY ("parent_step_attempt_namespace_id", "parent_step_attempt_id")
|
|
64
|
+
REFERENCES "step_attempts" ("namespace_id", "id")
|
|
65
|
+
ON DELETE SET NULL
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
CREATE TABLE IF NOT EXISTS "step_attempts" (
|
|
69
|
+
"namespace_id" TEXT NOT NULL,
|
|
70
|
+
"id" TEXT NOT NULL,
|
|
71
|
+
--
|
|
72
|
+
"workflow_run_id" TEXT NOT NULL,
|
|
73
|
+
"step_name" TEXT NOT NULL,
|
|
74
|
+
"kind" TEXT NOT NULL,
|
|
75
|
+
"status" TEXT NOT NULL,
|
|
76
|
+
"config" TEXT NOT NULL,
|
|
77
|
+
"context" TEXT,
|
|
78
|
+
"output" TEXT,
|
|
79
|
+
"error" TEXT,
|
|
80
|
+
"child_workflow_run_namespace_id" TEXT,
|
|
81
|
+
"child_workflow_run_id" TEXT,
|
|
82
|
+
"started_at" TEXT,
|
|
83
|
+
"finished_at" TEXT,
|
|
84
|
+
"created_at" TEXT NOT NULL,
|
|
85
|
+
"updated_at" TEXT NOT NULL,
|
|
86
|
+
PRIMARY KEY ("namespace_id", "id"),
|
|
87
|
+
FOREIGN KEY ("namespace_id", "workflow_run_id")
|
|
88
|
+
REFERENCES "workflow_runs" ("namespace_id", "id")
|
|
89
|
+
ON DELETE CASCADE,
|
|
90
|
+
FOREIGN KEY ("child_workflow_run_namespace_id", "child_workflow_run_id")
|
|
91
|
+
REFERENCES "workflow_runs" ("namespace_id", "id")
|
|
92
|
+
ON DELETE SET NULL
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
INSERT OR IGNORE INTO "openworkflow_migrations" ("version")
|
|
96
|
+
VALUES (1);
|
|
97
|
+
|
|
98
|
+
COMMIT;`,
|
|
99
|
+
// 2 - foreign keys
|
|
100
|
+
`BEGIN;
|
|
101
|
+
|
|
102
|
+
-- Foreign keys are defined in migration 1 since SQLite requires them during table creation
|
|
103
|
+
-- This migration exists for version parity with PostgreSQL backend
|
|
104
|
+
|
|
105
|
+
INSERT OR IGNORE INTO "openworkflow_migrations" ("version")
|
|
106
|
+
VALUES (2);
|
|
107
|
+
|
|
108
|
+
COMMIT;`,
|
|
109
|
+
// 3 - validate foreign keys
|
|
110
|
+
`BEGIN;
|
|
111
|
+
|
|
112
|
+
-- Foreign key validation happens automatically in SQLite when PRAGMA foreign_keys = ON
|
|
113
|
+
-- This migration exists for version parity with PostgreSQL backend
|
|
114
|
+
|
|
115
|
+
INSERT OR IGNORE INTO "openworkflow_migrations" ("version")
|
|
116
|
+
VALUES (3);
|
|
117
|
+
|
|
118
|
+
COMMIT;`,
|
|
119
|
+
// 4 - indexes
|
|
120
|
+
`BEGIN;
|
|
121
|
+
|
|
122
|
+
CREATE INDEX IF NOT EXISTS "workflow_runs_status_available_at_created_at_idx"
|
|
123
|
+
ON "workflow_runs" ("namespace_id", "status", "available_at", "created_at");
|
|
124
|
+
|
|
125
|
+
CREATE INDEX IF NOT EXISTS "workflow_runs_workflow_name_idempotency_key_created_at_idx"
|
|
126
|
+
ON "workflow_runs" ("namespace_id", "workflow_name", "idempotency_key", "created_at");
|
|
127
|
+
|
|
128
|
+
CREATE INDEX IF NOT EXISTS "workflow_runs_parent_step_idx"
|
|
129
|
+
ON "workflow_runs" ("parent_step_attempt_namespace_id", "parent_step_attempt_id")
|
|
130
|
+
WHERE parent_step_attempt_namespace_id IS NOT NULL AND parent_step_attempt_id IS NOT NULL;
|
|
131
|
+
|
|
132
|
+
CREATE INDEX IF NOT EXISTS "workflow_runs_created_at_desc_idx"
|
|
133
|
+
ON "workflow_runs" ("namespace_id", "created_at" DESC);
|
|
134
|
+
|
|
135
|
+
CREATE INDEX IF NOT EXISTS "workflow_runs_status_created_at_desc_idx"
|
|
136
|
+
ON "workflow_runs" ("namespace_id", "status", "created_at" DESC);
|
|
137
|
+
|
|
138
|
+
CREATE INDEX IF NOT EXISTS "workflow_runs_workflow_name_status_created_at_desc_idx"
|
|
139
|
+
ON "workflow_runs" ("namespace_id", "workflow_name", "status", "created_at" DESC);
|
|
140
|
+
|
|
141
|
+
CREATE INDEX IF NOT EXISTS "step_attempts_workflow_run_created_at_idx"
|
|
142
|
+
ON "step_attempts" ("namespace_id", "workflow_run_id", "created_at");
|
|
143
|
+
|
|
144
|
+
CREATE INDEX IF NOT EXISTS "step_attempts_workflow_run_step_name_created_at_idx"
|
|
145
|
+
ON "step_attempts" ("namespace_id", "workflow_run_id", "step_name", "created_at");
|
|
146
|
+
|
|
147
|
+
CREATE INDEX IF NOT EXISTS "step_attempts_child_workflow_run_idx"
|
|
148
|
+
ON "step_attempts" ("child_workflow_run_namespace_id", "child_workflow_run_id")
|
|
149
|
+
WHERE child_workflow_run_namespace_id IS NOT NULL AND child_workflow_run_id IS NOT NULL;
|
|
150
|
+
|
|
151
|
+
INSERT OR IGNORE INTO "openworkflow_migrations" ("version")
|
|
152
|
+
VALUES (4);
|
|
153
|
+
|
|
154
|
+
COMMIT;`,
|
|
155
|
+
];
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* migrate applies pending migrations to the database. Does nothing if the
|
|
159
|
+
* database is already up to date.
|
|
160
|
+
* @param db - SQLite database
|
|
161
|
+
*/
|
|
162
|
+
export function migrate(db) {
|
|
163
|
+
const currentMigrationVersion = getCurrentMigrationVersion(db);
|
|
164
|
+
for (const [i, migrationSql] of migrations().entries()) {
|
|
165
|
+
if (i <= currentMigrationVersion)
|
|
166
|
+
continue; // already applied
|
|
167
|
+
db.exec(migrationSql);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* getCurrentMigrationVersion returns the current migration version of the database.
|
|
172
|
+
* @param db - SQLite database
|
|
173
|
+
* @returns Current migration version
|
|
174
|
+
*/
|
|
175
|
+
function getCurrentMigrationVersion(db) {
|
|
176
|
+
// check if migrations table exists
|
|
177
|
+
const existsStmt = db.prepare(`
|
|
178
|
+
SELECT COUNT(*) as count
|
|
179
|
+
FROM sqlite_master
|
|
180
|
+
WHERE type = 'table' AND name = 'openworkflow_migrations'
|
|
181
|
+
`);
|
|
182
|
+
const existsResult = existsStmt.get();
|
|
183
|
+
if (!existsResult || existsResult.count === 0)
|
|
184
|
+
return -1;
|
|
185
|
+
// get current version
|
|
186
|
+
const versionStmt = db.prepare(`SELECT MAX("version") AS "version" FROM "openworkflow_migrations";`);
|
|
187
|
+
const versionResult = versionStmt.get();
|
|
188
|
+
return versionResult?.version ?? -1;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Helper to generate UUIDs (SQLite doesn't have built-in UUID generation)
|
|
192
|
+
* @returns A UUID string
|
|
193
|
+
*/
|
|
194
|
+
export function generateUUID() {
|
|
195
|
+
return randomUUID();
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Helper to get current timestamp in ISO8601 format
|
|
199
|
+
* @returns ISO8601 timestamp string
|
|
200
|
+
*/
|
|
201
|
+
export function now() {
|
|
202
|
+
return new Date().toISOString();
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Helper to add milliseconds to a date and return ISO8601 string
|
|
206
|
+
* @param date - ISO8601 date string
|
|
207
|
+
* @param ms - Milliseconds to add
|
|
208
|
+
* @returns Updated ISO8601 date string
|
|
209
|
+
*/
|
|
210
|
+
export function addMilliseconds(date, ms) {
|
|
211
|
+
const d = new Date(date);
|
|
212
|
+
d.setMilliseconds(d.getMilliseconds() + ms);
|
|
213
|
+
return d.toISOString();
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Helper to serialize JSON for SQLite storage
|
|
217
|
+
* @param value - Value to serialize
|
|
218
|
+
* @returns JSON string or null
|
|
219
|
+
*/
|
|
220
|
+
export function toJSON(value) {
|
|
221
|
+
return value === null || value === undefined ? null : JSON.stringify(value);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Helper to deserialize JSON from SQLite storage
|
|
225
|
+
* @param value - JSON string or null
|
|
226
|
+
* @returns Parsed value
|
|
227
|
+
*/
|
|
228
|
+
export function fromJSON(value) {
|
|
229
|
+
return value === null ? null : JSON.parse(value);
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Helper to convert Date to ISO8601 string for SQLite
|
|
233
|
+
* @param date - Date or null
|
|
234
|
+
* @returns ISO8601 date string or null
|
|
235
|
+
*/
|
|
236
|
+
export function toISO(date) {
|
|
237
|
+
return date ? date.toISOString() : null;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Helper to convert ISO8601 string from SQLite to Date
|
|
241
|
+
* @param dateStr - ISO8601 date string or null
|
|
242
|
+
* @returns Date or null
|
|
243
|
+
*/
|
|
244
|
+
export function fromISO(dateStr) {
|
|
245
|
+
return dateStr ? new Date(dateStr) : null;
|
|
246
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlite.test.d.ts","sourceRoot":"","sources":["../../sqlite/sqlite.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { migrate, migrations, newDatabase } from "./sqlite.js";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { existsSync, unlinkSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { afterEach, beforeEach, describe, expect, test } from "vitest";
|
|
7
|
+
// Helper to get the current migration version (exported for testing)
|
|
8
|
+
// Note: This function exists in sqlite.ts but isn't exported, so we'll
|
|
9
|
+
// test it indirectly through migrate() and by checking the migrations table
|
|
10
|
+
function getMigrationVersion(db) {
|
|
11
|
+
const existsStmt = db.prepare(`
|
|
12
|
+
SELECT COUNT(*) as count
|
|
13
|
+
FROM sqlite_master
|
|
14
|
+
WHERE type = 'table' AND name = 'openworkflow_migrations'
|
|
15
|
+
`);
|
|
16
|
+
const existsResult = existsStmt.get();
|
|
17
|
+
if (!existsResult || existsResult.count === 0)
|
|
18
|
+
return -1;
|
|
19
|
+
const versionStmt = db.prepare(`SELECT MAX("version") AS "version" FROM "openworkflow_migrations";`);
|
|
20
|
+
const versionResult = versionStmt.get();
|
|
21
|
+
return versionResult?.version ?? -1;
|
|
22
|
+
}
|
|
23
|
+
describe("sqlite", () => {
|
|
24
|
+
let db;
|
|
25
|
+
let dbPath;
|
|
26
|
+
beforeEach(() => {
|
|
27
|
+
// Use a unique file path for each test to ensure isolation
|
|
28
|
+
dbPath = path.join(tmpdir(), `test_${randomUUID()}.db`);
|
|
29
|
+
db = newDatabase(dbPath);
|
|
30
|
+
});
|
|
31
|
+
afterEach(() => {
|
|
32
|
+
db.close();
|
|
33
|
+
const walPath = `${dbPath}-wal`;
|
|
34
|
+
const shmPath = `${dbPath}-shm`;
|
|
35
|
+
// clean up the test database, WAL, and SHM files if they exist
|
|
36
|
+
if (existsSync(dbPath)) {
|
|
37
|
+
unlinkSync(dbPath);
|
|
38
|
+
}
|
|
39
|
+
if (existsSync(walPath)) {
|
|
40
|
+
unlinkSync(walPath);
|
|
41
|
+
}
|
|
42
|
+
if (existsSync(shmPath)) {
|
|
43
|
+
unlinkSync(shmPath);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
describe("migrations()", () => {
|
|
47
|
+
test("returns migration SQL statements with correct table names", () => {
|
|
48
|
+
const migs = migrations();
|
|
49
|
+
expect(migs.length).toBeGreaterThan(0);
|
|
50
|
+
// Check that migrations reference the openworkflow_migrations table
|
|
51
|
+
for (const mig of migs) {
|
|
52
|
+
expect(mig).toContain("openworkflow_migrations");
|
|
53
|
+
}
|
|
54
|
+
// Verify first migration creates the migrations table
|
|
55
|
+
expect(migs[0]).toContain('CREATE TABLE IF NOT EXISTS "openworkflow_migrations"');
|
|
56
|
+
expect(migs[0]).toContain('"version"');
|
|
57
|
+
});
|
|
58
|
+
test("migrations create workflow_runs and step_attempts tables", () => {
|
|
59
|
+
const migs = migrations();
|
|
60
|
+
// Migration 1 should create workflow_runs and step_attempts
|
|
61
|
+
const migration1 = migs[1];
|
|
62
|
+
expect(migration1).toContain('CREATE TABLE IF NOT EXISTS "workflow_runs"');
|
|
63
|
+
expect(migration1).toContain('CREATE TABLE IF NOT EXISTS "step_attempts"');
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
describe("migrate()", () => {
|
|
67
|
+
test("runs database migrations idempotently", () => {
|
|
68
|
+
// First migration
|
|
69
|
+
migrate(db);
|
|
70
|
+
const version1 = getMigrationVersion(db);
|
|
71
|
+
expect(version1).toBeGreaterThanOrEqual(0);
|
|
72
|
+
// Second migration - should not cause errors
|
|
73
|
+
migrate(db);
|
|
74
|
+
const version2 = getMigrationVersion(db);
|
|
75
|
+
expect(version2).toBe(version1); // Version should not change
|
|
76
|
+
// Third migration - should still work
|
|
77
|
+
migrate(db);
|
|
78
|
+
const version3 = getMigrationVersion(db);
|
|
79
|
+
expect(version3).toBe(version1);
|
|
80
|
+
});
|
|
81
|
+
test("tracks migration versions correctly", () => {
|
|
82
|
+
// Before migration, version should be -1 (table doesn't exist)
|
|
83
|
+
let version = getMigrationVersion(db);
|
|
84
|
+
expect(version).toBe(-1);
|
|
85
|
+
// After migration, version should be the latest migration version
|
|
86
|
+
migrate(db);
|
|
87
|
+
version = getMigrationVersion(db);
|
|
88
|
+
const allMigrations = migrations();
|
|
89
|
+
const expectedLatestVersion = allMigrations.length - 1;
|
|
90
|
+
expect(version).toBe(expectedLatestVersion);
|
|
91
|
+
});
|
|
92
|
+
test("applies migrations incrementally", () => {
|
|
93
|
+
// Create the migrations table manually with version 0
|
|
94
|
+
db.exec(`
|
|
95
|
+
CREATE TABLE IF NOT EXISTS "openworkflow_migrations" (
|
|
96
|
+
"version" INTEGER NOT NULL PRIMARY KEY
|
|
97
|
+
);
|
|
98
|
+
INSERT OR IGNORE INTO "openworkflow_migrations" ("version")
|
|
99
|
+
VALUES (0);
|
|
100
|
+
`);
|
|
101
|
+
let version = getMigrationVersion(db);
|
|
102
|
+
expect(version).toBe(0);
|
|
103
|
+
// Run migrate - should apply remaining migrations
|
|
104
|
+
migrate(db);
|
|
105
|
+
version = getMigrationVersion(db);
|
|
106
|
+
const allMigrations = migrations();
|
|
107
|
+
const expectedLatestVersion = allMigrations.length - 1;
|
|
108
|
+
expect(version).toBe(expectedLatestVersion);
|
|
109
|
+
});
|
|
110
|
+
test("creates all required tables after migration", () => {
|
|
111
|
+
migrate(db);
|
|
112
|
+
// Check that migrations table exists
|
|
113
|
+
const migrationsCheck = db
|
|
114
|
+
.prepare(`
|
|
115
|
+
SELECT COUNT(*) as count
|
|
116
|
+
FROM sqlite_master
|
|
117
|
+
WHERE type = 'table' AND name = 'openworkflow_migrations'
|
|
118
|
+
`)
|
|
119
|
+
.get();
|
|
120
|
+
expect(migrationsCheck.count).toBe(1);
|
|
121
|
+
// Check that workflow_runs table exists
|
|
122
|
+
const workflowRunsCheck = db
|
|
123
|
+
.prepare(`
|
|
124
|
+
SELECT COUNT(*) as count
|
|
125
|
+
FROM sqlite_master
|
|
126
|
+
WHERE type = 'table' AND name = 'workflow_runs'
|
|
127
|
+
`)
|
|
128
|
+
.get();
|
|
129
|
+
expect(workflowRunsCheck.count).toBe(1);
|
|
130
|
+
// Check that step_attempts table exists
|
|
131
|
+
const stepAttemptsCheck = db
|
|
132
|
+
.prepare(`
|
|
133
|
+
SELECT COUNT(*) as count
|
|
134
|
+
FROM sqlite_master
|
|
135
|
+
WHERE type = 'table' AND name = 'step_attempts'
|
|
136
|
+
`)
|
|
137
|
+
.get();
|
|
138
|
+
expect(stepAttemptsCheck.count).toBe(1);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
describe("migration version tracking", () => {
|
|
142
|
+
test("migrations table stores version numbers correctly", () => {
|
|
143
|
+
migrate(db);
|
|
144
|
+
const versionStmt = db.prepare(`SELECT "version" FROM "openworkflow_migrations" ORDER BY "version";`);
|
|
145
|
+
const versions = versionStmt.all();
|
|
146
|
+
// Should have all migration versions from 0 to latest
|
|
147
|
+
const allMigrations = migrations();
|
|
148
|
+
const expectedLatestVersion = allMigrations.length - 1;
|
|
149
|
+
expect(versions.length).toBe(expectedLatestVersion + 1);
|
|
150
|
+
for (let i = 0; i <= expectedLatestVersion; i++) {
|
|
151
|
+
const version = versions[i];
|
|
152
|
+
expect(version).toBeDefined();
|
|
153
|
+
expect(version?.version).toBe(i);
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
test("migrations can be run multiple times safely with INSERT OR IGNORE", () => {
|
|
157
|
+
migrate(db);
|
|
158
|
+
const versionAfterFirst = getMigrationVersion(db);
|
|
159
|
+
// Run migrations again
|
|
160
|
+
migrate(db);
|
|
161
|
+
const versionAfterSecond = getMigrationVersion(db);
|
|
162
|
+
expect(versionAfterSecond).toBe(versionAfterFirst);
|
|
163
|
+
// Check that version entries aren't duplicated
|
|
164
|
+
const versionStmt = db.prepare(`SELECT COUNT(*) as count FROM "openworkflow_migrations";`);
|
|
165
|
+
const countResult = versionStmt.get();
|
|
166
|
+
const allMigrations = migrations();
|
|
167
|
+
const expectedCount = allMigrations.length;
|
|
168
|
+
expect(countResult.count).toBe(expectedCount);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
});
|
package/dist/sqlite.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlite.d.ts","sourceRoot":"","sources":["../sqlite.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC"}
|
package/dist/sqlite.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { BackendSqlite } from "./sqlite/backend.js";
|