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 | --- |
| 2 | title: Preview databases |
| 3 | description: Give every pull request its own copy of your staging database. |
| 4 | --- |
| 5 | |
| 6 | Give every pull request a copy of your staging database. Each preview gets real data to test with, and is deleted when you're done. |
| 7 | |
| 8 | ## Read the stage |
| 9 | |
| 10 | ```typescript |
| 11 | import * as Alchemy from "alchemy"; |
| 12 | |
| 13 | const { stage } = yield* Alchemy.Stack; |
| 14 | const preview = stage.startsWith("pr-"); |
| 15 | ``` |
| 16 | |
| 17 | ## Reuse staging's group |
| 18 | |
| 19 | ```typescript |
| 20 | const group = preview |
| 21 | ? yield* Turso.Group.ref("Group", { stage: "staging" }) |
| 22 | : yield* Turso.Group("Group", { location: "aws-us-east-1" }); |
| 23 | ``` |
| 24 | |
| 25 | `Turso.Group.ref` uses the group from the `staging` stage. Previews never create or delete it. |
| 26 | |
| 27 | ## Copy staging's database |
| 28 | |
| 29 | ```typescript |
| 30 | const source = preview ? yield* Turso.Database.ref("Db", { stage: "staging" }) : undefined; |
| 31 | |
| 32 | const db = yield* Turso.Database("Db", { |
| 33 | group: group.name, |
| 34 | seed: source ? { type: "database", name: source.name } : undefined, |
| 35 | migrations: "./migrations", |
| 36 | }); |
| 37 | ``` |
| 38 | |
| 39 | Each preview gets its own copy of staging's database, then runs any new migrations from the pull request. |
| 40 | |
| 41 | ## Tear it down |
| 42 | |
| 43 | ```sh |
| 44 | alchemy destroy --stage pr-42 |
| 45 | ``` |
| 46 | |
| 47 | This deletes the preview's database. Staging's group and database stay. |
| 48 | |
| 49 | ## Where next |
| 50 | |
| 51 | - [Databases](/turso/databases#copy-another-database) |
| 52 | - [Migrations](/turso/migrations) |