feat(migration): settings migration from old app all is now in the new app

This commit is contained in:
2025-11-24 15:22:12 -06:00
parent 90920e8fba
commit 40bc19aa6f
6 changed files with 248 additions and 110 deletions

View File

@@ -3,16 +3,14 @@ process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
import { toNodeHandler } from "better-auth/node";
import cors from "cors";
import express from "express";
import fs from "fs";
import { createServer } from "http";
import { createProxyMiddleware, fixRequestBody } from "http-proxy-middleware";
import { createProxyMiddleware } from "http-proxy-middleware";
import morgan from "morgan";
import os from "os";
import { dirname, join } from "path";
import swaggerJsdoc from "swagger-jsdoc";
import swaggerUi from "swagger-ui-express";
import { fileURLToPath } from "url";
import { userMigrate } from "./src/internal/auth/controller/userMigrate.js";
import { schedulerManager } from "./src/internal/logistics/controller/schedulerManager.js";
import { setupMobileRoutes } from "./src/internal/mobile/route.js";
import { printers } from "./src/internal/ocp/printers/printers.js";
@@ -22,6 +20,7 @@ import { baseSettings } from "./src/internal/system/controller/settings/baseSett
import {
addListeners,
manualFixes,
settingsMigrate,
} from "./src/internal/system/utlis/addListeners.js";
import { swaggerOptions } from "./src/pkg/apiDocs/swaggerOptions.js";
import { auth } from "./src/pkg/auth/auth.js";
@@ -77,6 +76,7 @@ const main = async () => {
}
// connect to the prod sql
console.log("Connecting to the sql server");
await initializeProdPool();
// express app
@@ -218,11 +218,16 @@ const main = async () => {
v1Listener();
addListeners();
//userMigrate();
// some temp fixes
// above 230 remove these
manualFixes();
settingsMigrate();
}, 5 * 1000);
// setTimeout(() => {
// startHonoServer();
// }, 8 * 1000);
// start the server up
server.listen(PORT, "0.0.0.0", () =>
log.info(

View File

@@ -1,5 +1,8 @@
import { Client } from "pg";
import { db } from "../../../pkg/db/db.js";
import { type NewSetting, settings } from "../../../pkg/db/schema/settings.js";
import { createLogger } from "../../../pkg/logger/logger.js";
import { tryCatch } from "../../../pkg/utils/tryCatch.js";
export const addListeners = async () => {
const log = createLogger({ module: "utils", subModule: "listeners" });
@@ -60,6 +63,7 @@ export const addListeners = async () => {
}
};
// all the migration stuff that will need to be moved later build 230 and above will need to remove
export const manualFixes = async () => {
const fixQuery = `ALTER TABLE "serverData" ADD CONSTRAINT "serverData_name_unique" UNIQUE("name");`;
@@ -77,3 +81,48 @@ export const manualFixes = async () => {
log.info({ error: e }, "Fix was not completed");
}
};
export const settingsMigrate = async () => {
const log = createLogger({ module: "utils", subModule: "v1Migration" });
const client = new Client({
connectionString: process.env.DATABASE_URL_V1,
});
await client.connect();
let settingsV1: NewSetting[] = [];
try {
log.info({}, "Running the manual fix");
const s = await client.query("SELECT * FROM settings");
settingsV1 = s.rows.map((i) => {
return {
name: i.name,
value: i.value,
description: i.description,
moduleName: i.moduleName,
};
});
} catch (e) {
log.error({ error: e }, "There was an error getting the settings.");
}
const { data, error } = await tryCatch(
db
.insert(settings)
.values(settingsV1)
.onConflictDoNothing()
.returning({ name: settings.name }),
);
if (error) {
log.error({ error }, "There was an error adding new settings");
}
if (data) {
log.info({ newSettingsAdded: data }, "New settings added");
}
};
// migrations after 230 go below here so we can keep this inline.