| 1 | import { Resource } from "../../Resource.ts"; |
| 2 | import { |
| 3 | type BaseBranchAttributes, |
| 4 | type BaseBranchProps, |
| 5 | makeBranchProvider, |
| 6 | } from "../Branch.ts"; |
| 7 | import type { Providers } from "../Providers.ts"; |
| 8 | import type { MySQLClusterSize } from "./MySQLClusterSize.ts"; |
| 9 | import { runMySQLImports, runMySQLMigrations } from "./MySQLMigrations.ts"; |
| 10 | import type { MySQLDatabase } from "./MySQLDatabase.ts"; |
| 11 | |
| 12 | /** |
| 13 | * Properties for creating or updating a {@link MySQLBranch}. |
| 14 | */ |
| 15 | export interface MySQLBranchProps extends BaseBranchProps { |
| 16 | /** |
| 17 | * Whether the branch should be a production branch. |
| 18 | */ |
| 19 | isProduction: boolean; |
| 20 | |
| 21 | /** |
| 22 | * The MySQL database. Either a string database name or a |
| 23 | * {@link MySQLDatabase} resource. |
| 24 | */ |
| 25 | database: string | MySQLDatabase; |
| 26 | |
| 27 | /** |
| 28 | * MySQL cluster size for the branch. Required if `backupId` is provided. |
| 29 | * For non-production branches must be `"PS_DEV"`. |
| 30 | */ |
| 31 | clusterSize?: MySQLClusterSize; |
| 32 | |
| 33 | /** |
| 34 | * Parent branch — either a string name or another {@link MySQLBranch}. |
| 35 | * @default "main" |
| 36 | */ |
| 37 | parentBranch?: string | MySQLBranch; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Output attributes of a deployed {@link MySQLBranch}. |
| 42 | */ |
| 43 | export interface MySQLBranchAttributes extends BaseBranchAttributes {} |
| 44 | |
| 45 | /** |
| 46 | * A PlanetScale branch of a {@link MySQLDatabase}. For PostgreSQL branches |
| 47 | * use {@link PostgresBranch} instead. |
| 48 | * |
| 49 | * ### Creating a Branch |
| 50 | * **Example:** Branch from main |
| 51 | * ```typescript |
| 52 | * const branch = yield* Planetscale.MySQLBranch("Feature123", { |
| 53 | * database: "my-db", |
| 54 | * parentBranch: "main", |
| 55 | * isProduction: false, |
| 56 | * }); |
| 57 | * ``` |
| 58 | * |
| 59 | * **Example:** Branch from a MySQLDatabase resource |
| 60 | * ```typescript |
| 61 | * const db = yield* Planetscale.MySQLDatabase("MyDb", { clusterSize: "PS_10" }); |
| 62 | * const branch = yield* Planetscale.MySQLBranch("Feature456", { |
| 63 | * database: db, |
| 64 | * parentBranch: "main", |
| 65 | * isProduction: false, |
| 66 | * }); |
| 67 | * ``` |
| 68 | * |
| 69 | * ### Restoring from Backup |
| 70 | * **Example:** Branch restored from a backup |
| 71 | * ```typescript |
| 72 | * const branch = yield* Planetscale.MySQLBranch("Restored", { |
| 73 | * database: "my-db", |
| 74 | * parentBranch: "main", |
| 75 | * isProduction: true, |
| 76 | * backupId: "backup-123", |
| 77 | * clusterSize: "PS_10", |
| 78 | * }); |
| 79 | * ``` |
| 80 | * |
| 81 | * ### Migrations and seed data |
| 82 | * **Example:** Apply migrations on a branch |
| 83 | * ```typescript |
| 84 | * const branch = yield* Planetscale.MySQLBranch("Feature123", { |
| 85 | * database: db, |
| 86 | * parentBranch: "main", |
| 87 | * isProduction: false, |
| 88 | * migrationsDir: "./migrations", |
| 89 | * importFiles: ["./seed.sql"], |
| 90 | * }); |
| 91 | * ``` |
| 92 | */ |
| 93 | export type MySQLBranch = Resource< |
| 94 | "Planetscale.MySQLBranch", |
| 95 | MySQLBranchProps, |
| 96 | MySQLBranchAttributes, |
| 97 | never, |
| 98 | Providers |
| 99 | >; |
| 100 | |
| 101 | /** @resource */ |
| 102 | export const MySQLBranch = Resource<MySQLBranch>("Planetscale.MySQLBranch"); |
| 103 | |
| 104 | export const MySQLBranchProvider = () => |
| 105 | makeBranchProvider({ |
| 106 | resource: MySQLBranch, |
| 107 | expectedKind: "mysql", |
| 108 | engineLabel: "MySQLBranch", |
| 109 | runners: { |
| 110 | runMigrations: runMySQLMigrations, |
| 111 | runImports: runMySQLImports, |
| 112 | }, |
| 113 | }); |