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, 04:45 UTC
| 1 | import * as Planetscale from "@alchemy.run/planetscale"; |
| 2 | import { describe, expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as ps from "@distilled.cloud/planetscale"; |
| 5 | import { adopt } from "alchemy/AdoptPolicy"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import * as RemovalPolicy from "alchemy/RemovalPolicy"; |
| 8 | import { Data, Schedule } from "effect"; |
| 9 | import * as Cause from "effect/Cause"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import * as Exit from "effect/Exit"; |
| 12 | import { MinimumLogLevel } from "effect/References"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: Planetscale.providers() }); |
| 15 | |
| 16 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 17 | |
| 18 | const fixturesDir = `${import.meta.dirname}/fixtures`; |
| 19 | |
| 20 | describe |
| 21 | .skipIf(!process.env.PLANETSCALE_TEST) |
| 22 | .concurrent( |
| 23 | "MySQLDatabase", |
| 24 | { tags: ["provider:planetscale", "provider:planetscale:mysql", "live"] }, |
| 25 | () => { |
| 26 | // Read-only: exercises the real org-wide enumeration code path without |
| 27 | // provisioning anything (PlanetScale provisioning is extremely slow). |
| 28 | test.provider("list enumerates databases (read-only)", () => |
| 29 | Effect.gen(function* () { |
| 30 | const provider = yield* Provider.findProvider(Planetscale.MySQLDatabase); |
| 31 | const all = yield* provider.list(); |
| 32 | |
| 33 | expect(Array.isArray(all)).toBe(true); |
| 34 | for (const db of all) { |
| 35 | expect(db).toMatchObject({ |
| 36 | id: expect.any(String), |
| 37 | name: expect.any(String), |
| 38 | organization: expect.any(String), |
| 39 | region: { slug: expect.any(String) }, |
| 40 | }); |
| 41 | } |
| 42 | }).pipe(logLevel), |
| 43 | ); |
| 44 | |
| 45 | // Deploy-and-find coverage, opt-in only (slow provisioning). |
| 46 | test.provider.skipIf(!process.env.PLANETSCALE_DEPLOY_TEST)( |
| 47 | "list finds a freshly deployed database", |
| 48 | (stack) => |
| 49 | Effect.gen(function* () { |
| 50 | yield* stack.destroy(); |
| 51 | |
| 52 | const { database } = yield* stack.deploy( |
| 53 | Effect.gen(function* () { |
| 54 | const database = yield* Planetscale.MySQLDatabase("ListDatabase", { |
| 55 | name: "alchemy-mysql-db-list", |
| 56 | clusterSize: "PS_10", |
| 57 | }); |
| 58 | return { database }; |
| 59 | }), |
| 60 | ); |
| 61 | |
| 62 | const provider = yield* Provider.findProvider(Planetscale.MySQLDatabase); |
| 63 | const all = yield* provider.list(); |
| 64 | |
| 65 | expect( |
| 66 | all.some( |
| 67 | (db) => db.organization === database.organization && db.name === database.name, |
| 68 | ), |
| 69 | ).toBe(true); |
| 70 | |
| 71 | yield* stack.destroy(); |
| 72 | }).pipe(logLevel), |
| 73 | 5_000_000, |
| 74 | ); |
| 75 | |
| 76 | test.provider("create database with minimal settings", (stack) => |
| 77 | Effect.gen(function* () { |
| 78 | yield* stack.destroy(); |
| 79 | |
| 80 | const { database } = yield* stack.deploy( |
| 81 | Effect.gen(function* () { |
| 82 | const database = yield* Planetscale.MySQLDatabase("MySQLDatabaseBasic", { |
| 83 | clusterSize: "PS_10", |
| 84 | }); |
| 85 | |
| 86 | return { database }; |
| 87 | }), |
| 88 | ); |
| 89 | |
| 90 | expect(database).toMatchObject({ |
| 91 | id: expect.any(String), |
| 92 | name: expect.any(String), |
| 93 | organization: expect.any(String), |
| 94 | state: expect.any(String), |
| 95 | defaultBranch: "main", |
| 96 | plan: expect.any(String), |
| 97 | createdAt: expect.any(String), |
| 98 | updatedAt: expect.any(String), |
| 99 | htmlUrl: expect.any(String), |
| 100 | region: { slug: expect.any(String) }, |
| 101 | clusterSize: "PS_10", |
| 102 | }); |
| 103 | |
| 104 | const branch = yield* Planetscale.waitForBranchReady( |
| 105 | database.organization, |
| 106 | database.name, |
| 107 | "main", |
| 108 | ); |
| 109 | |
| 110 | expect(branch.cluster_name).toEqual("PS_10"); |
| 111 | |
| 112 | yield* stack.destroy(); |
| 113 | }).pipe(logLevel), |
| 114 | ); |
| 115 | |
| 116 | test.provider( |
| 117 | "create, update, and delete database", |
| 118 | (stack) => |
| 119 | Effect.gen(function* () { |
| 120 | yield* stack.destroy(); |
| 121 | |
| 122 | const { database } = yield* stack.deploy( |
| 123 | Effect.gen(function* () { |
| 124 | const database = yield* Planetscale.MySQLDatabase("MySQLDatabaseCRUD", { |
| 125 | region: { slug: "us-east" }, |
| 126 | clusterSize: "PS_10", |
| 127 | defaultBranch: "main", |
| 128 | allowDataBranching: true, |
| 129 | automaticMigrations: true, |
| 130 | requireApprovalForDeploy: false, |
| 131 | restrictBranchRegion: true, |
| 132 | insightsRawQueries: true, |
| 133 | productionBranchWebConsole: true, |
| 134 | migrationFramework: "rails", |
| 135 | migrationTableName: "schema_migrations", |
| 136 | }); |
| 137 | |
| 138 | return { database }; |
| 139 | }), |
| 140 | ); |
| 141 | |
| 142 | expect(database).toMatchObject({ |
| 143 | id: expect.any(String), |
| 144 | name: expect.any(String), |
| 145 | organization: expect.any(String), |
| 146 | state: expect.any(String), |
| 147 | plan: expect.any(String), |
| 148 | createdAt: expect.any(String), |
| 149 | updatedAt: expect.any(String), |
| 150 | htmlUrl: expect.any(String), |
| 151 | region: { slug: expect.any(String) }, |
| 152 | clusterSize: "PS_10", |
| 153 | defaultBranch: "main", |
| 154 | allowDataBranching: true, |
| 155 | automaticMigrations: true, |
| 156 | requireApprovalForDeploy: false, |
| 157 | restrictBranchRegion: true, |
| 158 | insightsRawQueries: true, |
| 159 | productionBranchWebConsole: true, |
| 160 | migrationFramework: "rails", |
| 161 | migrationTableName: "schema_migrations", |
| 162 | }); |
| 163 | |
| 164 | const { updatedDatabase } = yield* stack.deploy( |
| 165 | Effect.gen(function* () { |
| 166 | const updatedDatabase = yield* Planetscale.MySQLDatabase("MySQLDatabaseCRUD", { |
| 167 | clusterSize: "PS_20", |
| 168 | allowDataBranching: false, |
| 169 | automaticMigrations: true, |
| 170 | requireApprovalForDeploy: true, |
| 171 | restrictBranchRegion: false, |
| 172 | insightsRawQueries: false, |
| 173 | productionBranchWebConsole: false, |
| 174 | defaultBranch: "main", |
| 175 | migrationFramework: "django", |
| 176 | migrationTableName: "django_migrations", |
| 177 | }); |
| 178 | |
| 179 | return { updatedDatabase }; |
| 180 | }), |
| 181 | ); |
| 182 | |
| 183 | expect(updatedDatabase).toMatchObject({ |
| 184 | allowDataBranching: false, |
| 185 | automaticMigrations: true, |
| 186 | requireApprovalForDeploy: true, |
| 187 | restrictBranchRegion: false, |
| 188 | insightsRawQueries: false, |
| 189 | productionBranchWebConsole: false, |
| 190 | defaultBranch: "main", |
| 191 | migrationFramework: "django", |
| 192 | migrationTableName: "django_migrations", |
| 193 | }); |
| 194 | |
| 195 | const branch = yield* Planetscale.waitForBranchReady( |
| 196 | database.organization, |
| 197 | database.name, |
| 198 | "main", |
| 199 | ); |
| 200 | |
| 201 | expect(branch.cluster_name).toEqual("PS_20"); |
| 202 | |
| 203 | yield* stack.destroy(); |
| 204 | |
| 205 | yield* waitForDatabaseToBeDeleted(database.name, database.organization); |
| 206 | }).pipe(logLevel), |
| 207 | 5_000_000, |
| 208 | ); |
| 209 | |
| 210 | test.provider( |
| 211 | "reconciles replicas in place via keyspace resize", |
| 212 | (stack) => |
| 213 | Effect.gen(function* () { |
| 214 | yield* stack.destroy(); |
| 215 | |
| 216 | const { database } = yield* stack.deploy( |
| 217 | Effect.gen(function* () { |
| 218 | const database = yield* Planetscale.MySQLDatabase("MySQLDatabaseReplicas", { |
| 219 | clusterSize: "PS_10", |
| 220 | replicas: 3, |
| 221 | }); |
| 222 | return { database }; |
| 223 | }), |
| 224 | ); |
| 225 | |
| 226 | // PS_10 includes 2 replicas; the third is added in place via a |
| 227 | // keyspace resize request after creation. |
| 228 | expect(database.replicas).toEqual(3); |
| 229 | |
| 230 | const keyspaces = yield* ps.listKeyspaces({ |
| 231 | organization: database.organization, |
| 232 | database: database.name, |
| 233 | branch: "main", |
| 234 | }); |
| 235 | const keyspace = keyspaces.data.find((x) => x.name === database.name); |
| 236 | expect(keyspace?.replicas).toEqual(3); |
| 237 | expect(keyspace?.extra_replicas).toEqual(1); |
| 238 | |
| 239 | // Scale back down — must resize in place, never replace. |
| 240 | const { updatedDatabase } = yield* stack.deploy( |
| 241 | Effect.gen(function* () { |
| 242 | const updatedDatabase = yield* Planetscale.MySQLDatabase("MySQLDatabaseReplicas", { |
| 243 | clusterSize: "PS_10", |
| 244 | replicas: 2, |
| 245 | }); |
| 246 | return { updatedDatabase }; |
| 247 | }), |
| 248 | ); |
| 249 | |
| 250 | expect(updatedDatabase.id).toEqual(database.id); |
| 251 | expect(updatedDatabase.replicas).toEqual(2); |
| 252 | |
| 253 | yield* stack.destroy(); |
| 254 | |
| 255 | yield* waitForDatabaseToBeDeleted(database.name, database.organization); |
| 256 | }).pipe(logLevel), |
| 257 | 5_000_000, |
| 258 | ); |
| 259 | |
| 260 | test.provider( |
| 261 | "creates non-main default branch if specified", |
| 262 | (stack) => |
| 263 | Effect.gen(function* () { |
| 264 | yield* stack.destroy(); |
| 265 | |
| 266 | const { database } = yield* stack.deploy( |
| 267 | Effect.gen(function* () { |
| 268 | const database = yield* Planetscale.MySQLDatabase("MySQLDatabaseCustomBranch", { |
| 269 | clusterSize: "PS_10", |
| 270 | defaultBranch: "custom", |
| 271 | }); |
| 272 | |
| 273 | return { database }; |
| 274 | }), |
| 275 | ); |
| 276 | |
| 277 | expect(database).toMatchObject({ defaultBranch: "custom" }); |
| 278 | |
| 279 | const branch = yield* Planetscale.waitForBranchReady( |
| 280 | database.organization, |
| 281 | database.name, |
| 282 | "custom", |
| 283 | ); |
| 284 | |
| 285 | expect(branch.name).toEqual("custom"); |
| 286 | expect(branch.parent_branch).toEqual("main"); |
| 287 | expect(branch.cluster_name).toEqual("PS_10"); |
| 288 | |
| 289 | yield* stack.destroy(); |
| 290 | |
| 291 | yield* waitForDatabaseToBeDeleted(database.name, database.organization); |
| 292 | }).pipe(logLevel), |
| 293 | 5_000_000, // must wait on multiple resizes and branch creation |
| 294 | ); |
| 295 | |
| 296 | test.provider( |
| 297 | "applies migrations and import files", |
| 298 | (stack) => |
| 299 | Effect.gen(function* () { |
| 300 | yield* stack.destroy(); |
| 301 | |
| 302 | const importFile = `${fixturesDir}/seed.sql`; |
| 303 | const { database } = yield* stack.deploy( |
| 304 | Effect.gen(function* () { |
| 305 | const database = yield* Planetscale.MySQLDatabase("MySQLDatabaseMigrations", { |
| 306 | clusterSize: "PS_10", |
| 307 | migrations: `${fixturesDir}/migrations`, |
| 308 | importFiles: [importFile], |
| 309 | }); |
| 310 | |
| 311 | return { database }; |
| 312 | }), |
| 313 | ); |
| 314 | |
| 315 | expect(database.migrationsTable).toEqual("__alchemy_migrations"); |
| 316 | expect(database.migrationsHashes["0001_create_widgets.sql"]).toEqual( |
| 317 | expect.any(String), |
| 318 | ); |
| 319 | expect(database.importHashes[importFile]).toEqual(expect.any(String)); |
| 320 | |
| 321 | yield* stack.destroy(); |
| 322 | |
| 323 | yield* waitForDatabaseToBeDeleted(database.name, database.organization); |
| 324 | }).pipe(logLevel), |
| 325 | 5_000_000, |
| 326 | ); |
| 327 | |
| 328 | test.provider( |
| 329 | "adopt with wrong kind should throw", |
| 330 | (stack) => |
| 331 | Effect.gen(function* () { |
| 332 | yield* stack.destroy(); |
| 333 | |
| 334 | const { database } = yield* stack.deploy( |
| 335 | Effect.gen(function* () { |
| 336 | const database = yield* Planetscale.PostgresDatabase("PgBaselineWrongKind", { |
| 337 | region: { slug: "us-east" }, |
| 338 | clusterSize: "PS_10", |
| 339 | arch: "arm", // arm is slightly faster |
| 340 | }); |
| 341 | |
| 342 | return { database }; |
| 343 | }), |
| 344 | ); |
| 345 | |
| 346 | yield* Planetscale.waitForDatabaseReady(database.organization, database.name); |
| 347 | const exit = yield* Effect.exit( |
| 348 | stack |
| 349 | .deploy( |
| 350 | Effect.gen(function* () { |
| 351 | return yield* Planetscale.MySQLDatabase("MysqlWrongKind", { |
| 352 | name: database.name, |
| 353 | clusterSize: "PS_10", |
| 354 | }); |
| 355 | }), |
| 356 | ) |
| 357 | .pipe(adopt(true)), |
| 358 | ); |
| 359 | |
| 360 | expect(Exit.isFailure(exit)).toBe(true); |
| 361 | |
| 362 | if (Exit.isFailure(exit)) { |
| 363 | const pretty = Cause.pretty(exit.cause); |
| 364 | |
| 365 | expect(pretty).toContain("postgresql"); |
| 366 | expect(pretty).toContain("PostgresDatabase"); |
| 367 | } |
| 368 | yield* stack.destroy(); |
| 369 | }).pipe(logLevel), |
| 370 | { timeout: 5_000_000, tags: ["provider:planetscale:postgres"] }, |
| 371 | ); |
| 372 | |
| 373 | test.provider( |
| 374 | "database with RemovalPolicy.retain(true) should not be deleted via API", |
| 375 | (stack) => |
| 376 | Effect.gen(function* () { |
| 377 | yield* stack.destroy(); |
| 378 | |
| 379 | const { database } = yield* stack.deploy( |
| 380 | Effect.gen(function* () { |
| 381 | const database = yield* Planetscale.MySQLDatabase("MySQLDatabaseRetainRemoval", { |
| 382 | region: { slug: "us-east" }, |
| 383 | clusterSize: "PS_10", |
| 384 | }).pipe(RemovalPolicy.retain(true)); |
| 385 | |
| 386 | return { database }; |
| 387 | }), |
| 388 | ); |
| 389 | |
| 390 | // Verify database exists |
| 391 | const readyDatabase = yield* Planetscale.waitForDatabaseReady( |
| 392 | database.organization, |
| 393 | database.name, |
| 394 | ); |
| 395 | |
| 396 | expect(readyDatabase.name).toEqual(database.name); |
| 397 | |
| 398 | // When we call destroy, the database should NOT be deleted via API |
| 399 | yield* stack.destroy(); |
| 400 | |
| 401 | yield* Planetscale.waitForDatabaseReady(database.organization, database.name); |
| 402 | |
| 403 | // Verify database still exists (was not deleted via API) |
| 404 | const live = yield* ps.getDatabase({ |
| 405 | organization: database.organization, |
| 406 | database: database.name, |
| 407 | }); |
| 408 | |
| 409 | // Database should still exist |
| 410 | expect(live.name).toEqual(database.name); |
| 411 | expect(live.state).toEqual("ready"); |
| 412 | expect(live.kind).toEqual("mysql"); |
| 413 | |
| 414 | // Clean up manually for the test |
| 415 | yield* ps |
| 416 | .deleteDatabase({ organization: database.organization, database: database.name }) |
| 417 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 418 | }).pipe(logLevel), |
| 419 | 5_000_000, |
| 420 | ); |
| 421 | |
| 422 | test.provider( |
| 423 | "deletionProtection blocks destroy until it is turned off", |
| 424 | (stack) => |
| 425 | Effect.gen(function* () { |
| 426 | const name = `${stack.stage.toLowerCase().replace(/[^a-z0-9-]/g, "-")}-mysql-protect`; |
| 427 | const { organization } = yield* yield* Planetscale.Credentials; |
| 428 | |
| 429 | const program = (deletionProtection: boolean, region?: string) => |
| 430 | Effect.gen(function* () { |
| 431 | const database = yield* Planetscale.MySQLDatabase( |
| 432 | "MySQLDatabaseDeletionProtection", |
| 433 | { |
| 434 | name, |
| 435 | clusterSize: "PS_10", |
| 436 | deletionProtection, |
| 437 | ...(region ? { region: { slug: region } } : {}), |
| 438 | }, |
| 439 | ); |
| 440 | return { database }; |
| 441 | }); |
| 442 | const deploy = (deletionProtection: boolean) => |
| 443 | stack.deploy(program(deletionProtection)); |
| 444 | |
| 445 | yield* Effect.gen(function* () { |
| 446 | // Inside the cleanup scope: state left by an interrupted run |
| 447 | // can still point at a protected database, which makes this |
| 448 | // destroy fail. |
| 449 | yield* stack.destroy(); |
| 450 | |
| 451 | const { database } = yield* deploy(true); |
| 452 | expect(database.deletionProtection).toBe(true); |
| 453 | |
| 454 | const live = yield* ps.getDatabase({ |
| 455 | organization, |
| 456 | database: name, |
| 457 | }); |
| 458 | expect(live.deletion_protected).toBe(true); |
| 459 | |
| 460 | // Create-first replacement under the same explicit name would |
| 461 | // converge onto the old database and then delete it, so the |
| 462 | // plan must refuse a region change that keeps the name. |
| 463 | const replacement = yield* Effect.exit( |
| 464 | stack.plan( |
| 465 | program(true, database.region.slug === "eu-west" ? "us-east" : "eu-west"), |
| 466 | ), |
| 467 | ); |
| 468 | expect(Exit.isFailure(replacement)).toBe(true); |
| 469 | if (Exit.isFailure(replacement)) { |
| 470 | expect(Cause.pretty(replacement.cause)).toContain("requires a new database"); |
| 471 | } |
| 472 | |
| 473 | const exit = yield* Effect.exit(stack.destroy()); |
| 474 | expect(Exit.isFailure(exit)).toBe(true); |
| 475 | if (Exit.isFailure(exit)) { |
| 476 | expect(Cause.pretty(exit.cause)).toContain("deletionProtection: false"); |
| 477 | } |
| 478 | |
| 479 | const stillThere = yield* ps.getDatabase({ |
| 480 | organization, |
| 481 | database: name, |
| 482 | }); |
| 483 | expect(stillThere.id).toEqual(database.id); |
| 484 | |
| 485 | const { database: unprotected } = yield* deploy(false); |
| 486 | expect(unprotected.deletionProtection).toBe(false); |
| 487 | |
| 488 | yield* stack.destroy(); |
| 489 | |
| 490 | yield* waitForDatabaseToBeDeleted(name, organization); |
| 491 | }).pipe( |
| 492 | // Registered before the first destroy and deploy so a failed |
| 493 | // run never leaves a protected database behind; a failed |
| 494 | // cleanup fails the test. |
| 495 | Effect.ensuring(deleteTestDatabase(organization, name).pipe(Effect.orDie)), |
| 496 | ); |
| 497 | }).pipe(logLevel), |
| 498 | 5_000_000, |
| 499 | ); |
| 500 | }, |
| 501 | ); |
| 502 | |
| 503 | const waitForDatabaseToBeDeleted = Effect.fn(function* (database: string, organization: string) { |
| 504 | yield* ps |
| 505 | .getDatabase({ |
| 506 | organization, |
| 507 | database, |
| 508 | }) |
| 509 | .pipe( |
| 510 | Effect.flatMap(() => Effect.fail(new DatabaseStillExists())), |
| 511 | Effect.retry({ |
| 512 | while: (e): e is DatabaseStillExists => e instanceof DatabaseStillExists, |
| 513 | // Bounded so a database that never disappears fails the wait |
| 514 | // (after about 90 seconds) instead of hanging test cleanup. |
| 515 | schedule: Schedule.spaced("2 seconds"), |
| 516 | times: 45, |
| 517 | }), |
| 518 | Effect.catchTag("NotFound", () => Effect.void), |
| 519 | ); |
| 520 | }); |
| 521 | |
| 522 | class DatabaseStillExists extends Data.TaggedError("DatabaseStillExists") {} |
| 523 | |
| 524 | /** |
| 525 | * Turns deletion protection off and deletes a test database by name, then |
| 526 | * waits until it is gone. A database that does not exist is a no-op. |
| 527 | */ |
| 528 | const deleteTestDatabase = Effect.fn(function* (organization: string, database: string) { |
| 529 | const live = yield* ps |
| 530 | .getDatabase({ organization, database }) |
| 531 | .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined))); |
| 532 | if (!live) return; |
| 533 | if (live.deletion_protected) { |
| 534 | yield* ps.updateDatabaseSettings({ |
| 535 | organization, |
| 536 | database, |
| 537 | deletion_protected: false, |
| 538 | }); |
| 539 | } |
| 540 | yield* ps |
| 541 | .deleteDatabase({ organization, database }) |
| 542 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 543 | yield* waitForDatabaseToBeDeleted(database, organization); |
| 544 | }); |