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")
19 .toLowerCase() as MigrationState;
20
21 switch (state) {
22 case "issue":
23 return {
24 state: "issue",
25 message:
26 "Migration services are temporarily unavailable as we investigate an issue. Please try again later.",
27 allowMigration: false,
28 };
29
30 case "maintenance":
31 return {
32 state: "maintenance",
33 message:
34 "Migration services are temporarily unavailable for maintenance. Please try again later.",
35 allowMigration: false,
36 };
37
38 case "up":
39 default:
40 return {
41 state: "up",
42 message: "Migration services are operational.",
43 allowMigration: true,
44 };
45 }
46}
47
48/**
49 * Check if migrations are currently allowed.
50 * @returns True if migrations are allowed, false otherwise
51 */
52export function isMigrationAllowed(): boolean {
53 return getMigrationState().allowMigration;
54}
55
56/**
57 * Get a user-friendly message for the current migration state.
58 * @returns The message to display to users
59 */
60export function getMigrationStateMessage(): string {
61 return getMigrationState().message;
62}
63
64/**
65 * Throw an error if migrations are not allowed.
66 * Used in API endpoints to prevent migration operations when disabled.
67 */
68export function assertMigrationAllowed(): void {
69 const stateInfo = getMigrationState();
70 if (!stateInfo.allowMigration) {
71 throw new Error(stateInfo.message);
72 }
73}