rename auth and start work separating config

This commit is contained in:
Milo Schwartz
2025-01-01 16:40:01 -05:00
parent d447de9e8a
commit b199595100
15 changed files with 153 additions and 120 deletions

View File

@@ -3,10 +3,11 @@ import { orgs } from "../db/schema";
import config from "@server/config";
import { ne } from "drizzle-orm";
import logger from "@server/logger";
import { extractBaseDomain } from "@server/utils/extractBaseDomain";
export async function copyInConfig() {
// create a url from config.app.base_url and get the hostname
const domain = new URL(config.app.base_url).hostname;
const domain = extractBaseDomain(config.app.base_url);
// update the domain on all of the orgs where the domain is not equal to the new domain
// TODO: eventually each org could have a unique domain that we do not want to overwrite, so this will be unnecessary

View File

@@ -1,18 +1,23 @@
import { ensureActions } from "./ensureActions";
import { copyInConfig } from "./copyInConfig";
import logger from "@server/logger";
import { runMigrations } from "./migrations";
import { setupServerAdmin } from "./setupServerAdmin";
import { loadConfig } from "@server/config";
export async function runSetupFunctions() {
try {
logger.info(`Setup for version ${process.env.APP_VERSION}`);
await runMigrations(); // run the migrations
console.log("Migrations completed successfully.")
// ANYTHING BEFORE THIS LINE CANNOT USE THE CONFIG
loadConfig();
await copyInConfig(); // copy in the config to the db as needed
await setupServerAdmin();
await ensureActions(); // make sure all of the actions are in the db and the roles
} catch (error) {
logger.error("Error running setup functions", error);
console.error("Error running setup functions:", error);
process.exit(1);
}
}

View File

@@ -1,4 +1,3 @@
import logger from "@server/logger";
import { __DIRNAME } from "@server/config";
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
import db, { exists } from "@server/db";
@@ -8,12 +7,12 @@ import { versionMigrations } from "@server/db/schema";
import { desc } from "drizzle-orm";
// Import all migrations explicitly
import migration100beta1 from "./scripts/1.0.0-beta1";
import m1 from "./scripts/1.0.0-beta1";
// Add new migration imports here as they are created
// Define the migration list with versions and their corresponding functions
const migrations = [
{ version: "1.0.0-beta.1", run: migration100beta1 }
{ version: "1.0.0-beta.1", run: m1 }
// Add new migrations here as they are created
] as const;
@@ -23,21 +22,21 @@ export async function runMigrations() {
}
if (process.env.ENVIRONMENT !== "prod") {
logger.info("Skipping migrations in non-prod environment");
console.info("Skipping migrations in non-prod environment");
return;
}
if (exists) {
await executeScripts();
} else {
logger.info("Running migrations...");
console.info("Running migrations...");
try {
migrate(db, {
migrationsFolder: path.join(__DIRNAME, "init") // put here during the docker build
});
logger.info("Migrations completed successfully.");
console.info("Migrations completed successfully.");
} catch (error) {
logger.error("Error running migrations:", error);
console.error("Error running migrations:", error);
}
// insert process.env.APP_VERSION into the versionMigrations table
@@ -61,7 +60,7 @@ async function executeScripts() {
.limit(1);
const startVersion = lastExecuted[0]?.version ?? "0.0.0";
logger.info(`Starting migrations from version ${startVersion}`);
console.info(`Starting migrations from version ${startVersion}`);
// Filter and sort migrations
const pendingMigrations = migrations
@@ -70,7 +69,7 @@ async function executeScripts() {
// Run migrations in order
for (const migration of pendingMigrations) {
logger.info(`Running migration ${migration.version}`);
console.info(`Running migration ${migration.version}`);
try {
await migration.run();
@@ -84,11 +83,11 @@ async function executeScripts() {
})
.execute();
logger.info(
console.info(
`Successfully completed migration ${migration.version}`
);
} catch (error) {
logger.error(
console.error(
`Failed to run migration ${migration.version}:`,
error
);
@@ -96,9 +95,9 @@ async function executeScripts() {
}
}
logger.info("All migrations completed successfully");
console.info("All migrations completed successfully");
} catch (error) {
logger.error("Migration process failed:", error);
console.error("Migration process failed:", error);
throw error;
}
}

View File

@@ -1,7 +1,7 @@
import logger from "@server/logger";
export default async function migration100beta1() {
export default async function migration() {
logger.info("Running setup script 1.0.0-beta.1");
// SQL operations would go here in ts format
logger.info("Done...");
}
}