The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import * as Migrator from "effect/sql/Migrator"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as SqlClient from "effect/sql/SqlClient"; |
| 4 | import { |
| 5 | DATABASE_MIGRATION_PROGRESS_MESSAGE, |
| 6 | DATABASE_MIGRATIONS_FINISHED_MESSAGE, |
| 7 | DATABASE_MIGRATIONS_STARTED_MESSAGE, |
| 8 | } from "@t3tools/shared/databaseMigrationLog"; |
| 9 | import { reconcileV2PreviewMigration } from "./reconcileV2PreviewMigration.ts"; |
| 10 | |
| 11 | import Migration0001 from "./Migrations/001_OrchestrationEvents.ts"; |
| 12 | import Migration0002 from "./Migrations/002_OrchestrationCommandReceipts.ts"; |
| 13 | import Migration0003 from "./Migrations/003_CheckpointDiffBlobs.ts"; |
| 14 | import Migration0004 from "./Migrations/004_ProviderSessionRuntime.ts"; |
| 15 | import Migration0005 from "./Migrations/005_Projections.ts"; |
| 16 | import Migration0006 from "./Migrations/006_ProjectionThreadSessionRuntimeModeColumns.ts"; |
| 17 | import Migration0007 from "./Migrations/007_ProjectionThreadMessageAttachments.ts"; |
| 18 | import Migration0008 from "./Migrations/008_ProjectionThreadActivitySequence.ts"; |
| 19 | import Migration0009 from "./Migrations/009_ProviderSessionRuntimeMode.ts"; |
| 20 | import Migration0010 from "./Migrations/010_ProjectionThreadsRuntimeMode.ts"; |
| 21 | import Migration0011 from "./Migrations/011_OrchestrationThreadCreatedRuntimeMode.ts"; |
| 22 | import Migration0012 from "./Migrations/012_ProjectionThreadsInteractionMode.ts"; |
| 23 | import Migration0013 from "./Migrations/013_ProjectionThreadProposedPlans.ts"; |
| 24 | import Migration0014 from "./Migrations/014_ProjectionThreadProposedPlanImplementation.ts"; |
| 25 | import Migration0015 from "./Migrations/015_ProjectionTurnsSourceProposedPlan.ts"; |
| 26 | import Migration0016 from "./Migrations/016_CanonicalizeModelSelections.ts"; |
| 27 | import Migration0017 from "./Migrations/017_ProjectionThreadsArchivedAt.ts"; |
| 28 | import Migration0018 from "./Migrations/018_ProjectionThreadsArchivedAtIndex.ts"; |
| 29 | import Migration0019 from "./Migrations/019_ProjectionSnapshotLookupIndexes.ts"; |
| 30 | import Migration0020 from "./Migrations/020_AuthAccessManagement.ts"; |
| 31 | import Migration0021 from "./Migrations/021_AuthSessionClientMetadata.ts"; |
| 32 | import Migration0022 from "./Migrations/022_AuthSessionLastConnectedAt.ts"; |
| 33 | import Migration0023 from "./Migrations/023_ProjectionThreadShellSummary.ts"; |
| 34 | import Migration0024 from "./Migrations/024_BackfillProjectionThreadShellSummary.ts"; |
| 35 | import Migration0025 from "./Migrations/025_CleanupInvalidProjectionPendingApprovals.ts"; |
| 36 | import Migration0026 from "./Migrations/026_CanonicalizeModelSelectionOptions.ts"; |
| 37 | import Migration0027 from "./Migrations/027_ProviderSessionRuntimeInstanceId.ts"; |
| 38 | import Migration0028 from "./Migrations/028_ProjectionThreadSessionInstanceId.ts"; |
| 39 | import Migration0029 from "./Migrations/029_ProjectionThreadDetailOrderingIndexes.ts"; |
| 40 | import Migration0030 from "./Migrations/030_ProjectionThreadShellArchiveIndexes.ts"; |
| 41 | import Migration0031 from "./Migrations/031_AuthAuthorizationScopes.ts"; |
| 42 | import Migration0032 from "./Migrations/032_AuthPairingProofKeyThumbprint.ts"; |
| 43 | import Migration0033 from "./Migrations/033_ProjectionThreadsSettled.ts"; |
| 44 | import Migration0034 from "./Migrations/034_ProjectionThreadsSnoozed.ts"; |
| 45 | import Migration0035 from "./Migrations/035_ProjectionThreadTitleRegeneration.ts"; |
| 46 | import Migration0036 from "./Migrations/036_ProjectionThreadsPinned.ts"; |
| 47 | import Migration0037 from "./Migrations/037_ProjectionTurnsKeysetIndex.ts"; |
| 48 | import Migration0038 from "./Migrations/038_ProjectionThreadsPinOrderKey.ts"; |
| 49 | import Migration0039 from "./Migrations/039_ProjectionProjectsDefaultThreadEnvMode.ts"; |
| 50 | import Migration0040 from "./Migrations/040_ProjectionProjectFaviconPath.ts"; |
| 51 | import Migration0041 from "./Migrations/041_AuthSessionClientConnection.ts"; |
| 52 | import Migration0042 from "./Migrations/042_ProjectionThreadLinkedPullRequest.ts"; |
| 53 | import Migration0043 from "./Migrations/043_ProjectionThreadsUnsettledAt.ts"; |
| 54 | import Migration0044 from "./Migrations/044_ClearAutomaticProjectModelDefaults.ts"; |
| 55 | import Migration0045 from "./Migrations/045_ProjectionProjectsAutoPull.ts"; |
| 56 | import Migration0046 from "./Migrations/046_RepairAutomaticSettlementTimestamps.ts"; |
| 57 | import Migration0047 from "./Migrations/047_ProjectionProjectIcon.ts"; |
| 58 | import Migration0048 from "./Migrations/048_ProjectionThreadBranchPullRequest.ts"; |
| 59 | import Migration0049 from "./Migrations/049_ProjectionThreadsActiveOrderKey.ts"; |
| 60 | import Migration0050 from "./Migrations/050_ProjectionThreadPullRequests.ts"; |
| 61 | import Migration0051 from "./Migrations/051_ProjectionThreadMessageContext.ts"; |
| 62 | import Migration0052 from "./Migrations/052_ProjectionThreadTitleState.ts"; |
| 63 | import Migration0053 from "./Migrations/053_PullRequestFilesViewed.ts"; |
| 64 | import Migration0054 from "./Migrations/054_ProjectionThreadsAutoSettleDisabledAt.ts"; |
| 65 | import Migration0055 from "./Migrations/055_OrchestrationV2.ts"; |
| 66 | import Migration0056 from "./Migrations/056_RemoveRedundantProjectionIndexes.ts"; |
| 67 | import Migration0057 from "./Migrations/057_ScheduledTaskWebhooks.ts"; |
| 68 | import Migration0058 from "./Migrations/058_WebhookRelayDeliveries.ts"; |
| 69 | import Migration0059 from "./Migrations/059_McpAppModelContext.ts"; |
| 70 | import Migration0060 from "./Migrations/060_ThreadSnapshotWindowIndexes.ts"; |
| 71 | import Migration0061 from "./Migrations/061_PullRequestReviewGuides.ts"; |
| 72 | import Migration0062 from "./Migrations/062_ThreadFailureLookupIndex.ts"; |
| 73 | import Migration0063 from "./Migrations/063_DropUnusedV1Projections.ts"; |
| 74 | import Migration0064 from "./Migrations/064_CanonicalizeThreadPullRequestLinks.ts"; |
| 75 | import Migration0065 from "./Migrations/065_CodexUsageNativeTurnIndex.ts"; |
| 76 | import Migration0066 from "./Migrations/066_RepairClientSettlementTimestamps.ts"; |
| 77 | import Migration0067 from "./Migrations/067_WebhookDedupe.ts"; |
| 78 | import Migration0068 from "./Migrations/068_ShellStopRequestIndex.ts"; |
| 79 | import Migration0069 from "./Migrations/069_ThreadMessageSearch.ts"; |
| 80 | import Migration0070 from "./Migrations/070_PeerLinks.ts"; |
| 81 | import Migration0071 from "./Migrations/071_PeerLinkFence.ts"; |
| 82 | import Migration0072 from "./Migrations/072_WaitingLinkRequestIndex.ts"; |
| 83 | import Migration0073 from "./Migrations/073_ActiveTurnItemIndexes.ts"; |
| 84 | import Migration0074 from "./Migrations/074_ExcludeAttachmentPlaceholdersFromSearch.ts"; |
| 85 | import Migration0075 from "./Migrations/075_SubagentDeliveryState.ts"; |
| 86 | import Migration0076 from "./Migrations/076_ProjectionProjectsPinArchive.ts"; |
| 87 | import Migration0077 from "./Migrations/077_RepairBooleanSettledOverride.ts"; |
| 88 | import Migration0078 from "./Migrations/078_DropOrchestrationEventCorrelationIndex.ts"; |
| 89 | import Migration0079 from "./Migrations/079_DelegatedRecoveryIndexes.ts"; |
| 90 | import Migration0080 from "./Migrations/080_ScheduledTaskThreadMode.ts"; |
| 91 | import Migration0081 from "./Migrations/081_ProjectionMessageSearchText.ts"; |
| 92 | import Migration0082 from "./Migrations/082_TurnItemConversationIndex.ts"; |
| 93 | import Migration0083 from "./Migrations/083_CommandReadIndexes.ts"; |
| 94 | import Migration0084 from "./Migrations/084_LastExecutedRunIndex.ts"; |
| 95 | import Migration0085 from "./Migrations/085_TurnItemCounts.ts"; |
| 96 | import Migration0086 from "./Migrations/086_QueuedTurnInputIndex.ts"; |
| 97 | import Migration0087 from "./Migrations/087_ProjectionProjectsCustomTitle.ts"; |
| 98 | import Migration0088 from "./Migrations/088_TurnItemTypeStatusIndex.ts"; |
| 99 | import Migration0089 from "./Migrations/089_ShellThreadReadIndexes.ts"; |
| 100 | import Migration0090 from "./Migrations/090_AuthConnectDevices.ts"; |
| 101 | import Migration0091 from "./Migrations/091_AuthConnectDeniedRequests.ts"; |
| 102 | import Migration0092 from "./Migrations/092_QueuedTurnInputIndexJsonValid.ts"; |
| 103 | import Migration0093 from "./Migrations/093_ProviderTurnAttemptIndex.ts"; |
| 104 | import Migration0094 from "./Migrations/094_TurnItemMessageOrdinalIndex.ts"; |
| 105 | import Migration0095 from "./Migrations/095_TurnItemInlineWorkOutput.ts"; |
| 106 | import Migration0096 from "./Migrations/096_RepairStaleTitleRegeneration.ts"; |
| 107 | import Migration0097 from "./Migrations/097_IssueTrackerBindings.ts"; |
| 108 | import Migration0098 from "./Migrations/098_ActiveShellIndexIndexes.ts"; |
| 109 | import Migration0099 from "./Migrations/099_BackgroundRecoveryIndexes.ts"; |
| 110 | import Migration0100 from "./Migrations/100_TurnStartReadIndexes.ts"; |
| 111 | import Migration0101 from "./Migrations/101_DelegatedTaskReadIndexes.ts"; |
| 112 | import Migration0102 from "./Migrations/102_ThreadItemCounts.ts"; |
| 113 | |
| 114 | const migrationEntries = [ |
| 115 | [1, "OrchestrationEvents", Migration0001], |
| 116 | [2, "OrchestrationCommandReceipts", Migration0002], |
| 117 | [3, "CheckpointDiffBlobs", Migration0003], |
| 118 | [4, "ProviderSessionRuntime", Migration0004], |
| 119 | [5, "Projections", Migration0005], |
| 120 | [6, "ProjectionThreadSessionRuntimeModeColumns", Migration0006], |
| 121 | [7, "ProjectionThreadMessageAttachments", Migration0007], |
| 122 | [8, "ProjectionThreadActivitySequence", Migration0008], |
| 123 | [9, "ProviderSessionRuntimeMode", Migration0009], |
| 124 | [10, "ProjectionThreadsRuntimeMode", Migration0010], |
| 125 | [11, "OrchestrationThreadCreatedRuntimeMode", Migration0011], |
| 126 | [12, "ProjectionThreadsInteractionMode", Migration0012], |
| 127 | [13, "ProjectionThreadProposedPlans", Migration0013], |
| 128 | [14, "ProjectionThreadProposedPlanImplementation", Migration0014], |
| 129 | [15, "ProjectionTurnsSourceProposedPlan", Migration0015], |
| 130 | [16, "CanonicalizeModelSelections", Migration0016], |
| 131 | [17, "ProjectionThreadsArchivedAt", Migration0017], |
| 132 | [18, "ProjectionThreadsArchivedAtIndex", Migration0018], |
| 133 | [19, "ProjectionSnapshotLookupIndexes", Migration0019], |
| 134 | [20, "AuthAccessManagement", Migration0020], |
| 135 | [21, "AuthSessionClientMetadata", Migration0021], |
| 136 | [22, "AuthSessionLastConnectedAt", Migration0022], |
| 137 | [23, "ProjectionThreadShellSummary", Migration0023], |
| 138 | [24, "BackfillProjectionThreadShellSummary", Migration0024], |
| 139 | [25, "CleanupInvalidProjectionPendingApprovals", Migration0025], |
| 140 | [26, "CanonicalizeModelSelectionOptions", Migration0026], |
| 141 | [27, "ProviderSessionRuntimeInstanceId", Migration0027], |
| 142 | [28, "ProjectionThreadSessionInstanceId", Migration0028], |
| 143 | [29, "ProjectionThreadDetailOrderingIndexes", Migration0029], |
| 144 | [30, "ProjectionThreadShellArchiveIndexes", Migration0030], |
| 145 | [31, "AuthAuthorizationScopes", Migration0031], |
| 146 | [32, "AuthPairingProofKeyThumbprint", Migration0032], |
| 147 | [33, "ProjectionThreadsSettled", Migration0033], |
| 148 | [34, "ProjectionThreadsSnoozed", Migration0034], |
| 149 | [35, "ProjectionThreadTitleRegeneration", Migration0035], |
| 150 | [36, "ProjectionThreadsPinned", Migration0036], |
| 151 | [37, "ProjectionTurnsKeysetIndex", Migration0037], |
| 152 | [38, "ProjectionThreadsPinOrderKey", Migration0038], |
| 153 | [39, "ProjectionProjectsDefaultThreadEnvMode", Migration0039], |
| 154 | [40, "ProjectionProjectFaviconPath", Migration0040], |
| 155 | [41, "AuthSessionClientConnection", Migration0041], |
| 156 | [42, "ProjectionThreadLinkedPullRequest", Migration0042], |
| 157 | [43, "ProjectionThreadsUnsettledAt", Migration0043], |
| 158 | [44, "ClearAutomaticProjectModelDefaults", Migration0044], |
| 159 | [45, "ProjectionProjectsAutoPull", Migration0045], |
| 160 | [46, "RepairAutomaticSettlementTimestamps", Migration0046], |
| 161 | [47, "ProjectionProjectIcon", Migration0047], |
| 162 | [48, "ProjectionThreadBranchPullRequest", Migration0048], |
| 163 | [49, "ProjectionThreadsActiveOrderKey", Migration0049], |
| 164 | [50, "ProjectionThreadPullRequests", Migration0050], |
| 165 | [51, "ProjectionThreadMessageContext", Migration0051], |
| 166 | [52, "ProjectionThreadTitleState", Migration0052], |
| 167 | [53, "PullRequestFilesViewed", Migration0053], |
| 168 | [54, "ProjectionThreadsAutoSettleDisabledAt", Migration0054], |
| 169 | // Preserve this migration's schema. Future V2 schema changes need new migrations. |
| 170 | [55, "OrchestrationV2", Migration0055], |
| 171 | [56, "RemoveRedundantProjectionIndexes", Migration0056], |
| 172 | [57, "ScheduledTaskWebhooks", Migration0057], |
| 173 | [58, "WebhookRelayDeliveries", Migration0058], |
| 174 | [59, "McpAppModelContext", Migration0059], |
| 175 | [60, "ThreadSnapshotWindowIndexes", Migration0060], |
| 176 | [61, "PullRequestReviewGuides", Migration0061], |
| 177 | [62, "ThreadFailureLookupIndex", Migration0062], |
| 178 | [63, "DropUnusedV1Projections", Migration0063], |
| 179 | [64, "CanonicalizeThreadPullRequestLinks", Migration0064], |
| 180 | [65, "CodexUsageNativeTurnIndex", Migration0065], |
| 181 | [66, "RepairClientSettlementTimestamps", Migration0066], |
| 182 | [67, "WebhookDedupe", Migration0067], |
| 183 | [68, "ShellStopRequestIndex", Migration0068], |
| 184 | [69, "ThreadMessageSearch", Migration0069], |
| 185 | [70, "PeerLinks", Migration0070], |
| 186 | [71, "PeerLinkFence", Migration0071], |
| 187 | [72, "WaitingLinkRequestIndex", Migration0072], |
| 188 | [73, "ActiveTurnItemIndexes", Migration0073], |
| 189 | [74, "ExcludeAttachmentPlaceholdersFromSearch", Migration0074], |
| 190 | [75, "SubagentDeliveryState", Migration0075], |
| 191 | [76, "ProjectionProjectsPinArchive", Migration0076], |
| 192 | [77, "RepairBooleanSettledOverride", Migration0077], |
| 193 | [78, "DropOrchestrationEventCorrelationIndex", Migration0078], |
| 194 | [79, "DelegatedRecoveryIndexes", Migration0079], |
| 195 | [80, "ScheduledTaskThreadMode", Migration0080], |
| 196 | [81, "ProjectionMessageSearchText", Migration0081], |
| 197 | [82, "TurnItemConversationIndex", Migration0082], |
| 198 | [83, "CommandReadIndexes", Migration0083], |
| 199 | [84, "LastExecutedRunIndex", Migration0084], |
| 200 | [85, "TurnItemCounts", Migration0085], |
| 201 | [86, "QueuedTurnInputIndex", Migration0086], |
| 202 | [87, "ProjectionProjectsCustomTitle", Migration0087], |
| 203 | [88, "TurnItemTypeStatusIndex", Migration0088], |
| 204 | [89, "ShellThreadReadIndexes", Migration0089], |
| 205 | [90, "AuthConnectDevices", Migration0090], |
| 206 | [91, "AuthConnectDeniedRequests", Migration0091], |
| 207 | [92, "QueuedTurnInputIndexJsonValid", Migration0092], |
| 208 | [93, "ProviderTurnAttemptIndex", Migration0093], |
| 209 | [94, "TurnItemMessageOrdinalIndex", Migration0094], |
| 210 | [95, "TurnItemInlineWorkOutput", Migration0095], |
| 211 | [96, "RepairStaleTitleRegeneration", Migration0096], |
| 212 | [97, "IssueTrackerBindings", Migration0097], |
| 213 | [98, "ActiveShellIndexIndexes", Migration0098], |
| 214 | [99, "BackgroundRecoveryIndexes", Migration0099], |
| 215 | [100, "TurnStartReadIndexes", Migration0100], |
| 216 | [101, "DelegatedTaskReadIndexes", Migration0101], |
| 217 | [102, "ThreadItemCounts", Migration0102], |
| 218 | ] as const; |
| 219 | |
| 220 | export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const); |
| 221 | |
| 222 | const makeMigrationLoader = (throughId?: number) => |
| 223 | Migrator.fromRecord( |
| 224 | Object.fromEntries( |
| 225 | migrationEntries |
| 226 | .filter(([id]) => throughId === undefined || id <= throughId) |
| 227 | .map(([id, name, migration]) => [`${id}_${name}`, migration]), |
| 228 | ), |
| 229 | ); |
| 230 | |
| 231 | const FIRST_V2_MIGRATION_ID = 55; |
| 232 | |
| 233 | export function skippedV2Migrations( |
| 234 | recorded: ReadonlyArray<{ readonly id: number; readonly name: string }>, |
| 235 | throughId?: number, |
| 236 | ): string[] { |
| 237 | const latestRecordedId = Math.max(0, ...recorded.map((row) => row.id)); |
| 238 | const recordedNames = new Map(recorded.map((row) => [row.id, row.name])); |
| 239 | return migrationManifest.flatMap(([id, name]) => { |
| 240 | if ( |
| 241 | id < FIRST_V2_MIGRATION_ID || |
| 242 | id > latestRecordedId || |
| 243 | (throughId !== undefined && id > throughId) |
| 244 | ) |
| 245 | return []; |
| 246 | const recordedName = recordedNames.get(id); |
| 247 | return recordedName === name ? [] : [`${id}_${name} (recorded: ${recordedName ?? "nothing"})`]; |
| 248 | }); |
| 249 | } |
| 250 | |
| 251 | // Migrators skip ids up to the highest recorded; reject collisions that would skip V2 schema. |
| 252 | const assertV2MigrationsCanRun = Effect.fn("assertV2MigrationsCanRun")(function* ( |
| 253 | throughId?: number, |
| 254 | ) { |
| 255 | const sql = yield* SqlClient.SqlClient; |
| 256 | const tables = yield* sql` |
| 257 | SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'effect_sql_migrations' |
| 258 | `; |
| 259 | if (tables.length === 0) return; |
| 260 | const recorded = yield* sql<{ |
| 261 | readonly migration_id: number; |
| 262 | readonly name: string; |
| 263 | }>`SELECT migration_id, name FROM effect_sql_migrations`; |
| 264 | const skipped = skippedV2Migrations( |
| 265 | recorded.map((row) => ({ id: row.migration_id, name: row.name })), |
| 266 | throughId, |
| 267 | ); |
| 268 | const latestRecordedId = Math.max(0, ...recorded.map((row) => row.migration_id)); |
| 269 | if (skipped.length === 0) return; |
| 270 | return yield* new Migrator.MigrationError({ |
| 271 | kind: "BadState", |
| 272 | message: `This database's migration history would skip required schema migrations: ${skipped.join(", ")}. The migrator skips every id up to the highest recorded id (${latestRecordedId}), regardless of name. It was probably written by a different build. Start the build that wrote it, or report this migration history so an upgrade path can be added.`, |
| 273 | }); |
| 274 | }); |
| 275 | |
| 276 | const latestMigrationId = Math.max(...migrationEntries.map(([id]) => id)); |
| 277 | |
| 278 | // Mirrors the Migrator, which runs only ids above the highest recorded, plus the V2-preview reconciliation. |
| 279 | export const hasPendingMigrations = Effect.fn("hasPendingMigrations")(function* () { |
| 280 | const sql = yield* SqlClient.SqlClient; |
| 281 | const tables = yield* sql` |
| 282 | SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'effect_sql_migrations' |
| 283 | `; |
| 284 | if (tables.length === 0) return true; |
| 285 | const rows = yield* sql<{ readonly latestId: number | null; readonly previewRows: number }>` |
| 286 | SELECT |
| 287 | MAX(migration_id) AS latestId, |
| 288 | SUM(name = 'OrchestrationV2' AND migration_id IN (53, 54)) AS previewRows |
| 289 | FROM effect_sql_migrations |
| 290 | `; |
| 291 | const row = rows[0]; |
| 292 | return (row?.latestId ?? 0) < latestMigrationId || (row?.previewRows ?? 0) > 0; |
| 293 | }); |
| 294 | |
| 295 | const run = Migrator.make({}); |
| 296 | |
| 297 | export interface RunMigrationsOptions { |
| 298 | readonly toMigrationInclusive?: number | undefined; |
| 299 | } |
| 300 | |
| 301 | export const runMigrations = Effect.fn("runMigrations")(function* ({ |
| 302 | toMigrationInclusive, |
| 303 | }: RunMigrationsOptions = {}) { |
| 304 | // The desktop waits past its readiness budget until the finished line follows this one. |
| 305 | const hasPending = yield* hasPendingMigrations(); |
| 306 | if (hasPending) yield* Effect.log(DATABASE_MIGRATIONS_STARTED_MESSAGE); |
| 307 | const includesV2 = |
| 308 | toMigrationInclusive === undefined || toMigrationInclusive >= FIRST_V2_MIGRATION_ID; |
| 309 | // The reconcile takes the write lock; CLI startup must not contend with the server for a no-op. |
| 310 | const previewMigrations = includesV2 && hasPending ? yield* reconcileV2PreviewMigration() : []; |
| 311 | if (includesV2) { |
| 312 | yield* assertV2MigrationsCanRun(toMigrationInclusive); |
| 313 | } |
| 314 | const sql = yield* SqlClient.SqlClient; |
| 315 | const ledger = yield* sql` |
| 316 | SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'effect_sql_migrations' |
| 317 | `; |
| 318 | const [latest] = |
| 319 | ledger.length === 0 |
| 320 | ? [] |
| 321 | : yield* sql<{ |
| 322 | readonly id: number | null; |
| 323 | }>`SELECT MAX(migration_id) AS id FROM effect_sql_migrations`; |
| 324 | const latestRecordedId = latest?.id ?? 0; |
| 325 | const pending = migrationEntries.filter( |
| 326 | ([id]) => |
| 327 | id > latestRecordedId && (toMigrationInclusive === undefined || id <= toMigrationInclusive), |
| 328 | ); |
| 329 | // One transaction per migration: an index build on a large database can take minutes, and a |
| 330 | // restart then resumes after the last finished migration instead of rolling all of them back. |
| 331 | const executedMigrations: Array<readonly [id: number, name: string]> = [...previewMigrations]; |
| 332 | for (const [index, [id, name]] of pending.entries()) { |
| 333 | if (latestRecordedId > 0) { |
| 334 | yield* Effect.log(DATABASE_MIGRATION_PROGRESS_MESSAGE).pipe( |
| 335 | Effect.annotateLogs({ |
| 336 | migration: `${id}_${name}`, |
| 337 | progress: `${index + 1} of ${pending.length}`, |
| 338 | }), |
| 339 | ); |
| 340 | } |
| 341 | executedMigrations.push(...(yield* run({ loader: makeMigrationLoader(id) }))); |
| 342 | } |
| 343 | const migrations = executedMigrations.map(([id, name]) => `${id}_${name}`); |
| 344 | yield* migrations.length === 0 |
| 345 | ? Effect.logDebug("Database schema is current") |
| 346 | : Effect.log(DATABASE_MIGRATIONS_FINISHED_MESSAGE).pipe(Effect.annotateLogs({ migrations })); |
| 347 | |
| 348 | const recorded = yield* sql<{ |
| 349 | readonly migration_id: number; |
| 350 | readonly name: string; |
| 351 | }>`SELECT migration_id, name FROM effect_sql_migrations`; |
| 352 | const manifestNames = new Map<number, string>(migrationEntries.map(([id, name]) => [id, name])); |
| 353 | const divergent = recorded.flatMap((row) => { |
| 354 | const expected = manifestNames.get(row.migration_id); |
| 355 | if (expected === undefined) { |
| 356 | return [`${row.migration_id}:${row.name} (unknown to this build)`]; |
| 357 | } |
| 358 | return expected === row.name |
| 359 | ? [] |
| 360 | : [`${row.migration_id}:${row.name} (this build: ${expected})`]; |
| 361 | }); |
| 362 | if (divergent.length > 0) { |
| 363 | yield* Effect.logWarning( |
| 364 | "Database migration history diverges from this build; recorded migration ids are skipped, not reconciled by name.", |
| 365 | ).pipe(Effect.annotateLogs({ divergent })); |
| 366 | } |
| 367 | return executedMigrations; |
| 368 | }); |