Graphical PDS migrator for AT Protocol
1/**
2 * Migration state types and utilities for controlling migration availability.
3 */
4
5export type MigrationState = "up" | "issue" | "maintenance";
6
7export interface MigrationStateInfo {
8 state: MigrationState;
9 message: string;
10 allowMigration: boolean;
11}
12
13/**
14 * Get the current migration state from environment variables.
15 * @returns The migration state information
16 */
17export function getMigrationState(): MigrationStateInfo {
18 const state = (Deno.env.get("MIGRATION_STATE") || "up").toLowerCase() as MigrationState;
19
20 switch (state) {
21 case "issue":
22 return {
23 state: "issue",
24 message: "Migration services are temporarily unavailable as we investigate an issue. Please try again later.",
25 allowMigration: false,
26 };
27
28 case "maintenance":
29 return {
30 state: "maintenance",
31 message: "Migration services are temporarily unavailable for maintenance. Please try again later.",
32 allowMigration: false,
33 };
34
35 case "up":
36 default:
37 return {
38 state: "up",
39 message: "Migration services are operational.",
40 allowMigration: true,
41 };
42 }
43}
44
45/**
46 * Check if migrations are currently allowed.
47 * @returns True if migrations are allowed, false otherwise
48 */
49export function isMigrationAllowed(): boolean {
50 return getMigrationState().allowMigration;
51}
52
53/**
54 * Get a user-friendly message for the current migration state.
55 * @returns The message to display to users
56 */
57export function getMigrationStateMessage(): string {
58 return getMigrationState().message;
59}
60
61/**
62 * Throw an error if migrations are not allowed.
63 * Used in API endpoints to prevent migration operations when disabled.
64 */
65export function assertMigrationAllowed(): void {
66 const stateInfo = getMigrationState();
67 if (!stateInfo.allowMigration) {
68 throw new Error(stateInfo.message);
69 }
70}