@@ -158,8 +158,16 @@ type DurableWorkflowSignalRow = {
158158} ;
159159
160160type CountRow = { count : number | bigint } ;
161+ type DurableSchemaMigrationRow = {
162+ schema_name : string ;
163+ version : number | bigint ;
164+ applied_at : number | bigint ;
165+ metadata_json : string | null ;
166+ } ;
161167
162168const DURABLE_WORKFLOW_SQLITE_BUSY_TIMEOUT_MS = 30_000 ;
169+ export const DURABLE_WORKFLOW_SQLITE_SCHEMA_VERSION = 1 ;
170+ const DURABLE_WORKFLOW_SQLITE_SCHEMA_NAME = "durable_workflows" ;
163171
164172function optionalText ( value : string | undefined ) : string | null {
165173 return value && value . trim ( ) ? value : null ;
@@ -326,6 +334,13 @@ function ensureColumn(db: DatabaseSync, tableName: string, columnDefinition: str
326334
327335function ensureDurableWorkflowSchema ( db : DatabaseSync ) : void {
328336 db . exec ( `
337+ CREATE TABLE IF NOT EXISTS durable_schema_migrations (
338+ schema_name TEXT NOT NULL PRIMARY KEY,
339+ version INTEGER NOT NULL,
340+ applied_at INTEGER NOT NULL,
341+ metadata_json TEXT
342+ );
343+
329344 CREATE TABLE IF NOT EXISTS durable_workflow_runs (
330345 workflow_run_id TEXT NOT NULL PRIMARY KEY,
331346 workflow_id TEXT NOT NULL,
@@ -507,6 +522,55 @@ function ensureDurableWorkflowSchema(db: DatabaseSync): void {
507522 for ( const column of [ "claimed_by TEXT" , "claim_expires_at INTEGER" , "heartbeat_at INTEGER" ] ) {
508523 ensureColumn ( db , "durable_workflow_steps" , column ) ;
509524 }
525+ ensureDurableWorkflowSchemaVersion ( db ) ;
526+ }
527+
528+ function ensureDurableWorkflowSchemaVersion ( db : DatabaseSync ) : void {
529+ const now = Date . now ( ) ;
530+ const row = db
531+ . prepare ( "SELECT * FROM durable_schema_migrations WHERE schema_name = ?" )
532+ . get ( DURABLE_WORKFLOW_SQLITE_SCHEMA_NAME ) as DurableSchemaMigrationRow | undefined ;
533+ const currentVersion = Number ( row ?. version ?? 0 ) ;
534+ if ( currentVersion > DURABLE_WORKFLOW_SQLITE_SCHEMA_VERSION ) {
535+ throw new Error (
536+ `Durable workflow schema version ${ currentVersion } is newer than supported version ${ DURABLE_WORKFLOW_SQLITE_SCHEMA_VERSION } ` ,
537+ ) ;
538+ }
539+ if ( currentVersion === DURABLE_WORKFLOW_SQLITE_SCHEMA_VERSION ) {
540+ return ;
541+ }
542+
543+ const metadata = JSON . stringify ( {
544+ kind : currentVersion === 0 ? "fresh-install" : "schema-upgrade" ,
545+ previousVersion : currentVersion ,
546+ } ) ;
547+ if ( row ) {
548+ db . prepare (
549+ `UPDATE durable_schema_migrations
550+ SET version = ?,
551+ applied_at = ?,
552+ metadata_json = ?
553+ WHERE schema_name = ?` ,
554+ ) . run (
555+ DURABLE_WORKFLOW_SQLITE_SCHEMA_VERSION ,
556+ now ,
557+ metadata ,
558+ DURABLE_WORKFLOW_SQLITE_SCHEMA_NAME ,
559+ ) ;
560+ return ;
561+ }
562+
563+ db . prepare (
564+ `INSERT INTO durable_schema_migrations (schema_name, version, applied_at, metadata_json)
565+ VALUES (?, ?, ?, ?)` ,
566+ ) . run ( DURABLE_WORKFLOW_SQLITE_SCHEMA_NAME , DURABLE_WORKFLOW_SQLITE_SCHEMA_VERSION , now , metadata ) ;
567+ }
568+
569+ function getDurableWorkflowSchemaVersion ( db : DatabaseSync ) : number {
570+ const row = db
571+ . prepare ( "SELECT * FROM durable_schema_migrations WHERE schema_name = ?" )
572+ . get ( DURABLE_WORKFLOW_SQLITE_SCHEMA_NAME ) as DurableSchemaMigrationRow | undefined ;
573+ return Number ( row ?. version ?? 0 ) ;
510574}
511575
512576function count ( db : DatabaseSync , sql : string , values : SQLInputValue [ ] = [ ] ) : number {
@@ -1386,6 +1450,7 @@ export function openDurableWorkflowSqliteStore(options?: {
13861450 getStats ( ) : DurableWorkflowStoreStats {
13871451 return {
13881452 path : pathname ,
1453+ schemaVersion : getDurableWorkflowSchemaVersion ( db ) ,
13891454 runs : count ( db , "SELECT COUNT(*) AS count FROM durable_workflow_runs" ) ,
13901455 events : count ( db , "SELECT COUNT(*) AS count FROM durable_workflow_events" ) ,
13911456 steps : count ( db , "SELECT COUNT(*) AS count FROM durable_workflow_steps" ) ,
0 commit comments