The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { createHash } from "node:crypto"; |
| 2 | import * as AWS from "@alchemy.run/aws"; |
| 3 | import { makeDynamoDBState, makeS3State } from "@alchemy.run/aws"; |
| 4 | import { AWSEnvironment } from "@alchemy.run/aws/Environment"; |
| 5 | import { createOverflowBucketName } from "@alchemy.run/aws/StateStore/DynamoDBState"; |
| 6 | import { openLegacyBucket } from "@alchemy.run/aws/StateStore/LegacyLayout"; |
| 7 | import { migrateToDynamoDB, rollbackToS3 } from "@alchemy.run/aws/StateStore/Migration"; |
| 8 | import { expect } from "@alchemy.run/test"; |
| 9 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 10 | import type { Credentials } from "@distilled.cloud/aws/Credentials"; |
| 11 | import * as dynamodb from "@distilled.cloud/aws/dynamodb"; |
| 12 | import * as s3 from "@distilled.cloud/aws/s3"; |
| 13 | import * as Alchemy from "alchemy"; |
| 14 | import * as Alchemist from "alchemy/Alchemist"; |
| 15 | import { StateStoreError, type ResourceState } from "alchemy/State"; |
| 16 | import * as Cause from "effect/Cause"; |
| 17 | import * as Effect from "effect/Effect"; |
| 18 | import * as Exit from "effect/Exit"; |
| 19 | import type * as HttpClient from "effect/http/HttpClient"; |
| 20 | import * as Redacted from "effect/Redacted"; |
| 21 | import * as Schedule from "effect/Schedule"; |
| 22 | |
| 23 | const { test, deploy, destroy } = Test.make({ providers: AWS.providers() }); |
| 24 | |
| 25 | const STACK = "StateMigrationTestStack"; |
| 26 | |
| 27 | const tags = [ |
| 28 | "provider:aws", |
| 29 | "provider:aws:dynamodb", |
| 30 | "provider:aws:s3", |
| 31 | "provider:aws:statestore", |
| 32 | "live", |
| 33 | ]; |
| 34 | |
| 35 | /** A DynamoDB store's table and overflow bucket, and an S3 store's bucket. */ |
| 36 | const Storage = Effect.gen(function* () { |
| 37 | const table = yield* AWS.DynamoDB.Table("MigrationTable", { |
| 38 | partitionKey: "PK", |
| 39 | sortKey: "SK", |
| 40 | attributes: { PK: "S", SK: "S" }, |
| 41 | }); |
| 42 | const overflow = yield* AWS.S3.Bucket("MigrationOverflow", { forceDestroy: true }); |
| 43 | const legacy = yield* AWS.S3.Bucket("MigrationLegacy", { forceDestroy: true }); |
| 44 | return { |
| 45 | tableName: table.tableName, |
| 46 | overflowBucketName: overflow.bucketName, |
| 47 | legacyBucketName: legacy.bucketName, |
| 48 | }; |
| 49 | }); |
| 50 | |
| 51 | interface Storage { |
| 52 | readonly tableName: string; |
| 53 | readonly overflowBucketName: string; |
| 54 | readonly legacyBucketName: string; |
| 55 | } |
| 56 | |
| 57 | const PREFIX = "legacy"; |
| 58 | |
| 59 | const stores = (storage: Storage) => ({ |
| 60 | /** The S3 store, as an older alchemy runs it. */ |
| 61 | s3: makeS3State({ bucketName: storage.legacyBucketName, prefix: PREFIX }), |
| 62 | dynamodb: makeDynamoDBState({ |
| 63 | tableName: storage.tableName, |
| 64 | overflowBucketName: storage.overflowBucketName, |
| 65 | migrateFrom: { bucketName: storage.legacyBucketName, prefix: PREFIX }, |
| 66 | }), |
| 67 | }); |
| 68 | |
| 69 | const resource = (fqn: string, attr: Record<string, unknown>): ResourceState => |
| 70 | ({ |
| 71 | resourceType: "test:resource", |
| 72 | namespace: undefined, |
| 73 | fqn, |
| 74 | logicalId: fqn, |
| 75 | instanceId: `instance-${fqn}`, |
| 76 | providerVersion: 1, |
| 77 | status: "created", |
| 78 | downstream: [], |
| 79 | bindings: [], |
| 80 | props: {}, |
| 81 | attr, |
| 82 | }) as ResourceState; |
| 83 | |
| 84 | const a = resource("Parent/ResourceA", { value: "a" }); |
| 85 | const b = resource("ResourceB", { value: "b" }); |
| 86 | const output = { url: "https://example.com" }; |
| 87 | |
| 88 | /** Deploy-like writes through the S3 store, released when done. */ |
| 89 | const seedS3 = (storage: Storage, stage: string) => |
| 90 | Effect.scoped( |
| 91 | Effect.gen(function* () { |
| 92 | const state = yield* stores(storage).s3; |
| 93 | yield* state.set({ stack: STACK, stage, fqn: a.fqn, value: a }); |
| 94 | yield* state.set({ stack: STACK, stage, fqn: b.fqn, value: b }); |
| 95 | yield* state.setOutput({ stack: STACK, stage, value: output }); |
| 96 | }), |
| 97 | ); |
| 98 | |
| 99 | /** Lift the bucket policy a migration writes, so the stack can empty and delete the bucket. */ |
| 100 | const release = (storage: Storage) => |
| 101 | openLegacyBucket(storage.legacyBucketName).pipe(Effect.ignore); |
| 102 | |
| 103 | const migrate = (storage: Storage, options?: Parameters<typeof migrateToDynamoDB>[1]) => |
| 104 | Effect.scoped( |
| 105 | Effect.flatMap(stores(storage).dynamodb, (state) => migrateToDynamoDB(state, options)), |
| 106 | ); |
| 107 | |
| 108 | const rollback = (storage: Storage, options?: Parameters<typeof rollbackToS3>[1]) => |
| 109 | Effect.scoped(Effect.flatMap(stores(storage).dynamodb, (state) => rollbackToS3(state, options))); |
| 110 | |
| 111 | const migrateCommand = (stage: string) => `alchemy state migrate --stack ${STACK} --stage ${stage}`; |
| 112 | |
| 113 | /** |
| 114 | * A moved stage, as a version of alchemy from before the S3 store took a |
| 115 | * lease would see it: the bucket policy denies every access to its |
| 116 | * objects. Writes and deletes are denied from the fence on, so they are |
| 117 | * refused at once; the read deny is a later policy update, which S3 can |
| 118 | * take minutes to enforce everywhere, so it is checked in the policy. |
| 119 | */ |
| 120 | const deniedRawAccess = (storage: Storage, stage: string) => |
| 121 | Effect.gen(function* () { |
| 122 | const Bucket = storage.legacyBucketName; |
| 123 | const Key = `${PREFIX}/${STACK}/${stage}/${b.fqn}.json`; |
| 124 | const policy = JSON.parse((yield* s3.getBucketPolicy({ Bucket })).Policy ?? "{}") as { |
| 125 | Statement?: Array<{ |
| 126 | Sid?: string; |
| 127 | Effect?: string; |
| 128 | Action?: string[]; |
| 129 | Resource?: string | string[]; |
| 130 | }>; |
| 131 | }; |
| 132 | const migrated = (policy.Statement ?? []).find( |
| 133 | (statement) => statement.Sid === "AlchemyStateMigrated", |
| 134 | ); |
| 135 | expect(migrated?.Effect).toBe("Deny"); |
| 136 | expect(migrated?.Action).toEqual(["s3:GetObject", "s3:PutObject", "s3:DeleteObject"]); |
| 137 | expect([migrated?.Resource].flat()).toContain( |
| 138 | `arn:aws:s3:::${Bucket}/${PREFIX}/${STACK}/${stage}/*`, |
| 139 | ); |
| 140 | const denied = <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 141 | effect.pipe(Effect.flip, Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 15 })); |
| 142 | yield* denied(s3.putObject({ Bucket, Key, Body: "{}" })); |
| 143 | yield* denied(s3.deleteObject({ Bucket, Key })); |
| 144 | }); |
| 145 | |
| 146 | const legacyKeys = (storage: Storage, stage: string) => |
| 147 | s3 |
| 148 | .listObjectsV2({ Bucket: storage.legacyBucketName, Prefix: `${PREFIX}/${STACK}/${stage}/` }) |
| 149 | .pipe(Effect.map((result) => (result.Contents ?? []).map((object) => object.Key).sort())); |
| 150 | |
| 151 | test.provider( |
| 152 | "a stage still in S3 is refused until it is migrated, then served from DynamoDB", |
| 153 | (stack) => |
| 154 | Effect.gen(function* () { |
| 155 | yield* stack.destroy(); |
| 156 | const storage = yield* stack.deploy(Storage); |
| 157 | const stage = "refuse-then-migrate"; |
| 158 | yield* Effect.gen(function* () { |
| 159 | yield* seedS3(storage, stage); |
| 160 | |
| 161 | yield* Effect.scoped( |
| 162 | Effect.gen(function* () { |
| 163 | const state = yield* stores(storage).dynamodb; |
| 164 | const refused = yield* state.get({ stack: STACK, stage, fqn: a.fqn }).pipe(Effect.flip); |
| 165 | expect(refused).toBeInstanceOf(StateStoreError); |
| 166 | expect(refused.requires?.command).toBe(migrateCommand(stage)); |
| 167 | // Reads that take no lease still see the S3 copy. |
| 168 | expect(yield* state.getOutput({ stack: STACK, stage })).toEqual(output); |
| 169 | expect(yield* state.listStages(STACK)).toContain(stage); |
| 170 | }), |
| 171 | ); |
| 172 | |
| 173 | expect(yield* migrate(storage)).toEqual([ |
| 174 | { stack: STACK, stage, status: "migrated", records: 2 }, |
| 175 | ]); |
| 176 | |
| 177 | yield* Effect.scoped( |
| 178 | Effect.gen(function* () { |
| 179 | const state = yield* stores(storage).dynamodb; |
| 180 | expect(yield* state.get({ stack: STACK, stage, fqn: a.fqn })).toEqual(a); |
| 181 | expect([...(yield* state.list({ stack: STACK, stage }))].sort()).toEqual([ |
| 182 | "Parent/ResourceA", |
| 183 | "ResourceB", |
| 184 | ]); |
| 185 | expect(yield* state.getOutput({ stack: STACK, stage })).toEqual(output); |
| 186 | }), |
| 187 | ); |
| 188 | |
| 189 | // The S3 copy is kept, and closed to every older version: the |
| 190 | // lease-taking ones and the ones from before the lease. |
| 191 | expect(yield* legacyKeys(storage, stage)).toHaveLength(4); |
| 192 | yield* deniedRawAccess(storage, stage); |
| 193 | const older = yield* Effect.scoped( |
| 194 | Effect.flatMap(stores(storage).s3, (state) => |
| 195 | state.set({ stack: STACK, stage, fqn: a.fqn, value: a }), |
| 196 | ), |
| 197 | ).pipe(Effect.flip); |
| 198 | expect(older).toBeInstanceOf(StateStoreError); |
| 199 | |
| 200 | // Running it again finds nothing left to move. |
| 201 | expect(yield* migrate(storage)).toEqual([ |
| 202 | { stack: STACK, stage, status: "already-migrated" }, |
| 203 | ]); |
| 204 | }).pipe(Effect.ensuring(release(storage))); |
| 205 | yield* stack.destroy(); |
| 206 | }), |
| 207 | { tags, timeout: 180_000 }, |
| 208 | ); |
| 209 | |
| 210 | test.provider( |
| 211 | "a migration that stops after fencing or before committing finishes on the next run", |
| 212 | (stack) => |
| 213 | Effect.gen(function* () { |
| 214 | yield* stack.destroy(); |
| 215 | const storage = yield* stack.deploy(Storage); |
| 216 | const stage = "interrupted"; |
| 217 | yield* Effect.gen(function* () { |
| 218 | yield* seedS3(storage, stage); |
| 219 | |
| 220 | for (const failAt of ["fenced", "copied"] as const) { |
| 221 | const interrupted = yield* migrate(storage, { |
| 222 | afterStep: (step) => |
| 223 | step === failAt |
| 224 | ? Effect.fail(new StateStoreError({ message: `stopped after ${step}` })) |
| 225 | : Effect.void, |
| 226 | }); |
| 227 | expect(interrupted).toEqual([ |
| 228 | { stack: STACK, stage, status: "failed", reason: `stopped after ${failAt}` }, |
| 229 | ]); |
| 230 | |
| 231 | // Not committed: the stage is still refused, and still fenced in S3. |
| 232 | const refused = yield* Effect.scoped( |
| 233 | Effect.flatMap(stores(storage).dynamodb, (state) => |
| 234 | state.get({ stack: STACK, stage, fqn: a.fqn }), |
| 235 | ), |
| 236 | ).pipe(Effect.flip); |
| 237 | expect(refused.requires?.command).toBe(migrateCommand(stage)); |
| 238 | // The S3 store fails at the lock, or at the bucket policy's |
| 239 | // write deny once it applies. |
| 240 | const fenced = yield* Effect.scoped( |
| 241 | Effect.flatMap(stores(storage).s3, (state) => state.list({ stack: STACK, stage })), |
| 242 | ).pipe(Effect.flip); |
| 243 | expect(fenced.message).toMatch(/moved to a DynamoDB state store|explicit deny/); |
| 244 | } |
| 245 | |
| 246 | expect(yield* migrate(storage)).toEqual([ |
| 247 | { stack: STACK, stage, status: "migrated", records: 2 }, |
| 248 | ]); |
| 249 | yield* Effect.scoped( |
| 250 | Effect.gen(function* () { |
| 251 | const state = yield* stores(storage).dynamodb; |
| 252 | expect(yield* state.get({ stack: STACK, stage, fqn: b.fqn })).toEqual(b); |
| 253 | }), |
| 254 | ); |
| 255 | }).pipe(Effect.ensuring(release(storage))); |
| 256 | yield* stack.destroy(); |
| 257 | }), |
| 258 | { tags, timeout: 180_000 }, |
| 259 | ); |
| 260 | |
| 261 | test.provider( |
| 262 | "while a stage is copied, writes to its S3 objects are denied and reads are not", |
| 263 | (stack) => |
| 264 | Effect.gen(function* () { |
| 265 | yield* stack.destroy(); |
| 266 | const storage = yield* stack.deploy(Storage); |
| 267 | const stage = "copy-window"; |
| 268 | const Key = `${PREFIX}/${STACK}/${stage}/${b.fqn}.json`; |
| 269 | const Bucket = storage.legacyBucketName; |
| 270 | yield* Effect.gen(function* () { |
| 271 | yield* seedS3(storage, stage); |
| 272 | const checked = { done: false }; |
| 273 | // The hook runs inside the migration, which provides no AWS context of its own. |
| 274 | const aws = yield* Effect.context<Credentials | HttpClient.HttpClient>(); |
| 275 | const result = yield* migrate(storage, { |
| 276 | afterStep: (step) => |
| 277 | step !== "fenced" |
| 278 | ? Effect.void |
| 279 | : Effect.gen(function* () { |
| 280 | // As a version from before the lease would write. The |
| 281 | // policy takes a few seconds to apply. |
| 282 | yield* s3 |
| 283 | .putObject({ Bucket, Key, Body: "{}" }) |
| 284 | .pipe( |
| 285 | Effect.flip, |
| 286 | Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 30 }), |
| 287 | ); |
| 288 | yield* s3.deleteObject({ Bucket, Key }).pipe(Effect.flip); |
| 289 | yield* s3.getObject({ Bucket, Key }); |
| 290 | checked.done = true; |
| 291 | }).pipe( |
| 292 | Effect.provideContext(aws), |
| 293 | Effect.mapError( |
| 294 | (error) => new StateStoreError({ message: `copy window: ${String(error)}` }), |
| 295 | ), |
| 296 | ), |
| 297 | }); |
| 298 | expect(checked.done).toBe(true); |
| 299 | // The writes were refused, so the copy is the seeded stage. |
| 300 | expect(result).toEqual([{ stack: STACK, stage, status: "migrated", records: 2 }]); |
| 301 | yield* Effect.scoped( |
| 302 | Effect.gen(function* () { |
| 303 | const state = yield* stores(storage).dynamodb; |
| 304 | expect(yield* state.get({ stack: STACK, stage, fqn: b.fqn })).toEqual(b); |
| 305 | }), |
| 306 | ); |
| 307 | }).pipe(Effect.ensuring(release(storage))); |
| 308 | yield* stack.destroy(); |
| 309 | }), |
| 310 | { tags, timeout: 180_000 }, |
| 311 | ); |
| 312 | |
| 313 | test.provider( |
| 314 | "the store's own migrate moves stages between the table and bucket it is configured with", |
| 315 | (stack) => |
| 316 | Effect.gen(function* () { |
| 317 | yield* stack.destroy(); |
| 318 | const storage = yield* stack.deploy(Storage); |
| 319 | const stage = "configured"; |
| 320 | yield* Effect.gen(function* () { |
| 321 | yield* seedS3(storage, stage); |
| 322 | // What `alchemy state migrate` calls on the store alchemy.run.ts configures. |
| 323 | const viaStore = (rollback: boolean) => |
| 324 | Effect.scoped( |
| 325 | Effect.flatMap(stores(storage).dynamodb, (state) => |
| 326 | state.migrate === undefined |
| 327 | ? Effect.die("the DynamoDB store has no migrate") |
| 328 | : state.migrate({ stack: STACK, stage, rollback }), |
| 329 | ), |
| 330 | ); |
| 331 | |
| 332 | expect(yield* viaStore(false)).toEqual([ |
| 333 | { stack: STACK, stage, status: "migrated", records: 2 }, |
| 334 | ]); |
| 335 | // Into the configured table, not the account's default one. |
| 336 | expect( |
| 337 | (yield* dynamodb.getItem({ |
| 338 | TableName: storage.tableName, |
| 339 | Key: { PK: { S: "CATALOG" }, SK: { S: `STAGE#${STACK}#${stage}` } }, |
| 340 | ConsistentRead: true, |
| 341 | })).Item?.migratedFrom?.S, |
| 342 | ).toBe(`s3://${storage.legacyBucketName}/${PREFIX}/`); |
| 343 | |
| 344 | expect(yield* viaStore(true)).toEqual([ |
| 345 | { stack: STACK, stage, status: "rolled-back", records: 2 }, |
| 346 | ]); |
| 347 | yield* Effect.scoped( |
| 348 | Effect.gen(function* () { |
| 349 | const state = yield* stores(storage).s3; |
| 350 | expect(yield* state.get({ stack: STACK, stage, fqn: a.fqn })).toEqual(a); |
| 351 | }), |
| 352 | ); |
| 353 | }).pipe(Effect.ensuring(release(storage))); |
| 354 | yield* stack.destroy(); |
| 355 | }), |
| 356 | { tags, timeout: 180_000 }, |
| 357 | ); |
| 358 | |
| 359 | test.provider( |
| 360 | "a stage an S3 deploy holds is reported as blocked and moves once it is released", |
| 361 | (stack) => |
| 362 | Effect.gen(function* () { |
| 363 | yield* stack.destroy(); |
| 364 | const storage = yield* stack.deploy(Storage); |
| 365 | const stage = "blocked"; |
| 366 | yield* Effect.gen(function* () { |
| 367 | yield* seedS3(storage, stage); |
| 368 | |
| 369 | yield* Effect.scoped( |
| 370 | Effect.gen(function* () { |
| 371 | // An older alchemy, mid-deploy. |
| 372 | const deploying = yield* stores(storage).s3; |
| 373 | yield* deploying.list({ stack: STACK, stage }); |
| 374 | const result = yield* migrate(storage); |
| 375 | expect(result).toHaveLength(1); |
| 376 | expect(result[0]?.status).toBe("blocked"); |
| 377 | // The deploy keeps its lease and can still write. |
| 378 | yield* deploying.set({ |
| 379 | stack: STACK, |
| 380 | stage, |
| 381 | fqn: "ResourceC", |
| 382 | value: resource("ResourceC", {}), |
| 383 | }); |
| 384 | }), |
| 385 | ); |
| 386 | |
| 387 | expect(yield* migrate(storage)).toEqual([ |
| 388 | { stack: STACK, stage, status: "migrated", records: 3 }, |
| 389 | ]); |
| 390 | }).pipe(Effect.ensuring(release(storage))); |
| 391 | yield* stack.destroy(); |
| 392 | }), |
| 393 | { tags, timeout: 180_000 }, |
| 394 | ); |
| 395 | |
| 396 | test.provider( |
| 397 | "running the migration again takes no lease on stages already moved", |
| 398 | (stack) => |
| 399 | Effect.gen(function* () { |
| 400 | yield* stack.destroy(); |
| 401 | const storage = yield* stack.deploy(Storage); |
| 402 | const stage = "rerun"; |
| 403 | yield* Effect.gen(function* () { |
| 404 | yield* seedS3(storage, stage); |
| 405 | expect(yield* migrate(storage)).toEqual([ |
| 406 | { stack: STACK, stage, status: "migrated", records: 2 }, |
| 407 | ]); |
| 408 | yield* Effect.scoped( |
| 409 | Effect.gen(function* () { |
| 410 | // A deploy on DynamoDB holds the stage while the migration runs again. |
| 411 | const deploying = yield* stores(storage).dynamodb; |
| 412 | yield* deploying.list({ stack: STACK, stage }); |
| 413 | expect(yield* migrate(storage)).toEqual([ |
| 414 | { stack: STACK, stage, status: "already-migrated" }, |
| 415 | ]); |
| 416 | yield* deploying.set({ stack: STACK, stage, fqn: b.fqn, value: b }); |
| 417 | }), |
| 418 | ); |
| 419 | }).pipe(Effect.ensuring(release(storage))); |
| 420 | yield* stack.destroy(); |
| 421 | }), |
| 422 | { tags, timeout: 180_000 }, |
| 423 | ); |
| 424 | |
| 425 | test.provider( |
| 426 | "a migrated stage that is destroyed stays destroyed instead of reverting to its S3 copy", |
| 427 | (stack) => |
| 428 | Effect.gen(function* () { |
| 429 | yield* stack.destroy(); |
| 430 | const storage = yield* stack.deploy(Storage); |
| 431 | const stage = "destroyed"; |
| 432 | yield* Effect.gen(function* () { |
| 433 | yield* seedS3(storage, stage); |
| 434 | yield* migrate(storage); |
| 435 | |
| 436 | // What `alchemy destroy` ends with. |
| 437 | yield* Effect.scoped( |
| 438 | Effect.flatMap(stores(storage).dynamodb, (state) => |
| 439 | state.deleteStack({ stack: STACK, stage }), |
| 440 | ), |
| 441 | ); |
| 442 | |
| 443 | yield* Effect.scoped( |
| 444 | Effect.gen(function* () { |
| 445 | const state = yield* stores(storage).dynamodb; |
| 446 | expect(yield* state.listStages(STACK)).not.toContain(stage); |
| 447 | expect(yield* state.getOutput({ stack: STACK, stage })).toBeUndefined(); |
| 448 | // Deploying the stage again starts it empty, not refused. |
| 449 | expect(yield* state.list({ stack: STACK, stage })).toEqual([]); |
| 450 | yield* state.set({ stack: STACK, stage, fqn: b.fqn, value: b }); |
| 451 | }), |
| 452 | ); |
| 453 | |
| 454 | expect(yield* migrate(storage)).toEqual([ |
| 455 | { stack: STACK, stage, status: "already-migrated" }, |
| 456 | ]); |
| 457 | yield* Effect.scoped( |
| 458 | Effect.gen(function* () { |
| 459 | const state = yield* stores(storage).dynamodb; |
| 460 | expect(yield* state.list({ stack: STACK, stage })).toEqual([b.fqn]); |
| 461 | expect(yield* state.listStages(STACK)).toContain(stage); |
| 462 | }), |
| 463 | ); |
| 464 | }).pipe(Effect.ensuring(release(storage))); |
| 465 | yield* stack.destroy(); |
| 466 | }), |
| 467 | { tags, timeout: 180_000 }, |
| 468 | ); |
| 469 | |
| 470 | test.provider( |
| 471 | "a stage that starts in DynamoDB is fenced in S3 against teammates still on the S3 store", |
| 472 | (stack) => |
| 473 | Effect.gen(function* () { |
| 474 | yield* stack.destroy(); |
| 475 | const storage = yield* stack.deploy(Storage); |
| 476 | const stage = "native-fenced"; |
| 477 | yield* Effect.gen(function* () { |
| 478 | yield* Effect.scoped( |
| 479 | Effect.flatMap(stores(storage).dynamodb, (state) => |
| 480 | state.set({ stack: STACK, stage, fqn: a.fqn, value: a }), |
| 481 | ), |
| 482 | ); |
| 483 | const older = yield* Effect.scoped( |
| 484 | Effect.flatMap(stores(storage).s3, (state) => state.list({ stack: STACK, stage })), |
| 485 | ).pipe(Effect.flip); |
| 486 | expect(older.message).toContain("moved to a DynamoDB state store"); |
| 487 | }).pipe(Effect.ensuring(release(storage))); |
| 488 | yield* stack.destroy(); |
| 489 | }), |
| 490 | { tags, timeout: 180_000 }, |
| 491 | ); |
| 492 | |
| 493 | test.provider( |
| 494 | "rollback moves stages, including ones written since, back to the S3 store", |
| 495 | (stack) => |
| 496 | Effect.gen(function* () { |
| 497 | yield* stack.destroy(); |
| 498 | const storage = yield* stack.deploy(Storage); |
| 499 | const moved = "moved"; |
| 500 | const native = "native"; |
| 501 | yield* Effect.gen(function* () { |
| 502 | yield* seedS3(storage, moved); |
| 503 | yield* migrate(storage); |
| 504 | |
| 505 | // Writes after the migration, and a stage that started in DynamoDB. |
| 506 | const c = resource("ResourceC", { value: "c" }); |
| 507 | yield* Effect.scoped( |
| 508 | Effect.gen(function* () { |
| 509 | const state = yield* stores(storage).dynamodb; |
| 510 | yield* state.delete({ stack: STACK, stage: moved, fqn: b.fqn }); |
| 511 | yield* state.set({ stack: STACK, stage: moved, fqn: c.fqn, value: c }); |
| 512 | yield* state.set({ stack: STACK, stage: native, fqn: a.fqn, value: a }); |
| 513 | }), |
| 514 | ); |
| 515 | |
| 516 | const rolledBack = yield* rollback(storage); |
| 517 | expect([...rolledBack].sort((x, y) => x.stage.localeCompare(y.stage))).toEqual([ |
| 518 | { stack: STACK, stage: moved, status: "rolled-back", records: 2 }, |
| 519 | { stack: STACK, stage: native, status: "rolled-back", records: 1 }, |
| 520 | ]); |
| 521 | |
| 522 | // An older alchemy has its stages back, with the writes it missed. |
| 523 | yield* Effect.scoped( |
| 524 | Effect.gen(function* () { |
| 525 | const state = yield* stores(storage).s3; |
| 526 | expect([...(yield* state.list({ stack: STACK, stage: moved }))].sort()).toEqual([ |
| 527 | "Parent/ResourceA", |
| 528 | "ResourceC", |
| 529 | ]); |
| 530 | expect(yield* state.get({ stack: STACK, stage: moved, fqn: c.fqn })).toEqual(c); |
| 531 | expect(yield* state.getOutput({ stack: STACK, stage: moved })).toEqual(output); |
| 532 | expect(yield* state.get({ stack: STACK, stage: native, fqn: a.fqn })).toEqual(a); |
| 533 | yield* state.set({ stack: STACK, stage: native, fqn: b.fqn, value: b }); |
| 534 | }), |
| 535 | ); |
| 536 | |
| 537 | // And the DynamoDB store refuses them until they are migrated again. |
| 538 | const refused = yield* Effect.scoped( |
| 539 | Effect.flatMap(stores(storage).dynamodb, (state) => |
| 540 | state.list({ stack: STACK, stage: native }), |
| 541 | ), |
| 542 | ).pipe(Effect.flip); |
| 543 | expect(refused.requires?.command).toBe(migrateCommand(native)); |
| 544 | const items = yield* dynamodb.query({ |
| 545 | TableName: storage.tableName, |
| 546 | ConsistentRead: true, |
| 547 | KeyConditionExpression: "PK = :pk", |
| 548 | ExpressionAttributeValues: { ":pk": { S: `STAGE#${STACK}#${moved}` } }, |
| 549 | }); |
| 550 | expect(items.Items ?? []).toEqual([]); |
| 551 | }).pipe(Effect.ensuring(release(storage))); |
| 552 | yield* stack.destroy(); |
| 553 | }), |
| 554 | { tags, timeout: 180_000 }, |
| 555 | ); |
| 556 | |
| 557 | test.provider( |
| 558 | "a rollback that stops after switching a stage back finishes on the next run", |
| 559 | (stack) => |
| 560 | Effect.gen(function* () { |
| 561 | yield* stack.destroy(); |
| 562 | const storage = yield* stack.deploy(Storage); |
| 563 | const stage = "rollback-interrupted"; |
| 564 | yield* Effect.gen(function* () { |
| 565 | yield* seedS3(storage, stage); |
| 566 | expect(yield* migrate(storage)).toEqual([ |
| 567 | { stack: STACK, stage, status: "migrated", records: 2 }, |
| 568 | ]); |
| 569 | |
| 570 | const interrupted = yield* rollback(storage, { |
| 571 | afterStep: () => Effect.fail(new StateStoreError({ message: "stopped after switching" })), |
| 572 | }); |
| 573 | expect(interrupted).toEqual([ |
| 574 | { stack: STACK, stage, status: "failed", reason: "stopped after switching" }, |
| 575 | ]); |
| 576 | // Switched back, but still fenced until the cleanup runs. |
| 577 | const fenced = yield* Effect.scoped( |
| 578 | Effect.flatMap(stores(storage).s3, (state) => state.list({ stack: STACK, stage })), |
| 579 | ).pipe(Effect.flip); |
| 580 | expect(fenced.message).toContain("moved to a DynamoDB state store"); |
| 581 | |
| 582 | expect(yield* rollback(storage)).toEqual([{ stack: STACK, stage, status: "rolled-back" }]); |
| 583 | yield* Effect.scoped( |
| 584 | Effect.gen(function* () { |
| 585 | const state = yield* stores(storage).s3; |
| 586 | expect([...(yield* state.list({ stack: STACK, stage }))].sort()).toEqual([ |
| 587 | "Parent/ResourceA", |
| 588 | "ResourceB", |
| 589 | ]); |
| 590 | }), |
| 591 | ); |
| 592 | }).pipe(Effect.ensuring(release(storage))); |
| 593 | yield* stack.destroy(); |
| 594 | }), |
| 595 | { tags, timeout: 180_000 }, |
| 596 | ); |
| 597 | |
| 598 | test.provider( |
| 599 | "a Stack deployed on the S3 store redeploys from DynamoDB after `alchemy state migrate`", |
| 600 | (stack) => |
| 601 | Effect.gen(function* () { |
| 602 | const { accountId, region } = yield* AWSEnvironment.current; |
| 603 | // A named store per stage, so runs never touch the account's default. |
| 604 | const name = `mig-${yield* Effect.sync(() => |
| 605 | createHash("sha256").update(stack.stage).digest("hex").slice(0, 8), |
| 606 | )}`; |
| 607 | const program = Effect.gen(function* () { |
| 608 | const secret = yield* Alchemy.Random("Secret"); |
| 609 | return { secret: secret.text }; |
| 610 | }); |
| 611 | const OnS3 = Alchemy.Stack( |
| 612 | "StateMigrationStack", |
| 613 | { providers: AWS.providers(), state: AWS.state({ backend: "s3", name }) }, |
| 614 | program, |
| 615 | ); |
| 616 | const OnDynamoDB = Alchemy.Stack( |
| 617 | "StateMigrationStack", |
| 618 | { providers: AWS.providers(), state: AWS.state({ name }) }, |
| 619 | program, |
| 620 | ); |
| 621 | const alchemist = Alchemist.layer(); |
| 622 | const awsStore = { type: "provider", backend: "aws", name } as const; |
| 623 | const s3Store = { type: "provider", backend: "aws-s3", name } as const; |
| 624 | const teardown = Effect.all( |
| 625 | [Alchemist.State.teardown(awsStore), Alchemist.State.teardown(s3Store)], |
| 626 | { discard: true }, |
| 627 | ).pipe(Effect.provide(alchemist)); |
| 628 | |
| 629 | yield* teardown; |
| 630 | const first = yield* deploy(OnS3); |
| 631 | |
| 632 | // The upgraded config refuses the stage instead of redeploying it fresh. |
| 633 | const refused = yield* Effect.exit(deploy(OnDynamoDB)); |
| 634 | expect(Exit.isFailure(refused)).toBe(true); |
| 635 | if (Exit.isFailure(refused)) { |
| 636 | expect(Cause.pretty(refused.cause)).toContain( |
| 637 | "alchemy state migrate --stack StateMigrationStack --stage", |
| 638 | ); |
| 639 | } |
| 640 | |
| 641 | const result = yield* Alchemist.State.migrate(awsStore).pipe(Effect.provide(alchemist)); |
| 642 | expect( |
| 643 | result.stages.map(({ stack, status, records }) => ({ stack, status, records })), |
| 644 | ).toEqual([{ stack: "StateMigrationStack", status: "migrated", records: 1 }]); |
| 645 | |
| 646 | // Same resource, same secret: the state moved rather than starting over. |
| 647 | const second = yield* deploy(OnDynamoDB); |
| 648 | expect(Redacted.value(second.secret)).toBe(Redacted.value(first.secret)); |
| 649 | |
| 650 | yield* destroy(OnDynamoDB); |
| 651 | // Tearing down the DynamoDB store keeps the S3 store it replaced. |
| 652 | const torn = yield* Alchemist.State.teardown(awsStore).pipe(Effect.provide(alchemist)); |
| 653 | expect([...torn.deleted].sort()).toEqual( |
| 654 | [`alchemy-state-${name}`, createOverflowBucketName(accountId, region, name)].sort(), |
| 655 | ); |
| 656 | const tornS3 = yield* Alchemist.State.teardown(s3Store).pipe(Effect.provide(alchemist)); |
| 657 | expect(tornS3.deleted).toEqual([`alchemy-state-${name}-${accountId}-${region}-an`]); |
| 658 | }), |
| 659 | { tags, timeout: 300_000 }, |
| 660 | ); |