mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
- services/data/stage.ts: stage(mode) throws on anything but dev or production, used by all three configs, build.ts and deploy.ts - packages/stats -> packages/data (@opencode/data-app, -core, -server) - StatsSyncWorkflow -> DataSyncWorkflow, instance ids data-hourly-/data-history-, Data* resource keys, data sync log lines - worker, route, workflow and cron names unchanged
35 lines
1.4 KiB
TypeScript
35 lines
1.4 KiB
TypeScript
import { migrate } from "drizzle-orm/planetscale-serverless/migrator"
|
|
import { Config, ConfigProvider, Effect } from "effect"
|
|
import { DrizzleClient, layer } from "./database"
|
|
|
|
// The migrator reads migration files from disk, so it stays out of the
|
|
// Worker-safe database module.
|
|
class MigrationError extends Error {
|
|
readonly _tag = "MigrationError"
|
|
|
|
constructor(input: { message: string; cause?: unknown }) {
|
|
super(input.message, { cause: input.cause })
|
|
this.name = "MigrationError"
|
|
}
|
|
}
|
|
|
|
const applyMigrations = Effect.fn("Database.migrate")(function* () {
|
|
const migrationsDir = yield* Config.NonEmptyString("DATABASE_MIGRATIONS_DIR")
|
|
.pipe(Config.withDefault("./migrations"))
|
|
.parse(ConfigProvider.fromEnv())
|
|
const db = yield* DrizzleClient
|
|
yield* Effect.logInfo("applying database migrations").pipe(Effect.annotateLogs({ migrationsDir }))
|
|
const result = yield* Effect.tryPromise({
|
|
try: () => migrate(db, { migrationsFolder: migrationsDir }),
|
|
catch: (cause) => new MigrationError({ message: "Failed to apply database migrations", cause }),
|
|
})
|
|
if (result)
|
|
return yield* Effect.fail(
|
|
new MigrationError({
|
|
message: `Failed to initialize database migrations: ${result.exitCode}`,
|
|
}),
|
|
)
|
|
yield* Effect.logInfo("database migrations complete").pipe(Effect.annotateLogs({ migrationsDir }))
|
|
})
|
|
|
|
await Effect.runPromise(applyMigrations().pipe(Effect.provide(layer)))
|