Skip to content

A modern and easy to use dashboard. 80+ integrations. 20K+ icons built in. Authentication out of the box. No YAML, drag and drop configuration.

1.9 KB
Raw Blame
import fs from "fs";
import path from "path";

import { NextResponse } from "next/server";

import { createLogger } from "@homarr/core/infrastructure/logs";
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";

import { findMigrationsFolder } from "../shared";

const logger = createLogger({ module: "backupMigrationsRoute" });

interface JournalEntry {
  idx: number;
  version: string;
  when: number;
  tag: string;
  breakpoints: boolean;
}

interface MigrationFile {
  idx: number;
  tag: string;
  sql: string;
  when: number;
}

export async function GET() {
  try {
    const migrationsFolder = findMigrationsFolder();
    if (!migrationsFolder) {
      return NextResponse.json({ error: "Migration files not found" }, { status: 500 });
    }

    const journalPath = path.join(migrationsFolder, "meta", "_journal.json");
    if (!fs.existsSync(journalPath)) {
      return NextResponse.json({ error: "Migration journal not found" }, { status: 500 });
    }

    const journal = JSON.parse(fs.readFileSync(journalPath, "utf-8")) as { entries: JournalEntry[] };

    const migrations: MigrationFile[] = [];
    for (const entry of journal.entries) {
      const sqlPath = path.join(migrationsFolder, `${entry.tag}.sql`);
      if (!fs.existsSync(sqlPath)) continue;

      migrations.push({
        idx: entry.idx,
        tag: entry.tag,
        sql: fs.readFileSync(sqlPath, "utf-8"),
        when: entry.when,
      });
    }

    return NextResponse.json(
      { total: migrations.length, migrations },
      {
        headers: {
          "Cache-Control": "public, max-age=3600",
        },
      },
    );
  } catch (error) {
    logger.error(new ErrorWithMetadata("Failed to read migration files", {}, { cause: error }));
    const message = error instanceof Error ? error.message : "Unknown error";
    return NextResponse.json({ error: `Failed to read migrations: ${message}` }, { status: 500 });
  }
}