92 lines
2.3 KiB
TypeScript
92 lines
2.3 KiB
TypeScript
import { Context } from "fresh";
|
|
import { db } from "../../../db/db.ts";
|
|
import type { State } from "../../../utils.ts";
|
|
|
|
/**
|
|
* Debug endpoint to verify database schema
|
|
* Returns list of tables that exist in the database
|
|
*/
|
|
export const handler = async (
|
|
_req: Request,
|
|
_ctx: Context<State>,
|
|
): Promise<Response> => {
|
|
try {
|
|
// Query PostgreSQL information_schema to get all tables
|
|
const query = `
|
|
SELECT table_name
|
|
FROM information_schema.tables
|
|
WHERE table_schema = 'memory'
|
|
OR table_schema = 'checkpoints'
|
|
OR table_schema = 'public'
|
|
ORDER BY table_schema, table_name
|
|
`;
|
|
|
|
const result = await db.execute(query);
|
|
|
|
// Expected tables based on schema.ts
|
|
const expectedTables = [
|
|
"users",
|
|
"roles",
|
|
"user_roles",
|
|
"state_keys",
|
|
"user_tokens",
|
|
"web_tokens",
|
|
"obligations",
|
|
"incomes",
|
|
"financial_balances",
|
|
"properties",
|
|
"rent_schedules",
|
|
"leases",
|
|
"payment_methods",
|
|
"property_media",
|
|
"lease_documents",
|
|
"tenant_applications",
|
|
"tenant_invites",
|
|
"payment_transactions",
|
|
"reminders",
|
|
"messages",
|
|
"maintenance_requests",
|
|
"user_pins",
|
|
"session_configs",
|
|
];
|
|
|
|
// Extract table names from query result
|
|
const existingTables = result.rows.map((row: Record<string, unknown>) =>
|
|
String(row["table_name"])
|
|
);
|
|
|
|
// Find missing tables
|
|
const missingTables = expectedTables.filter(
|
|
(table) => !existingTables.includes(table),
|
|
);
|
|
|
|
return new Response(
|
|
JSON.stringify({
|
|
status: "success",
|
|
existing_tables: existingTables,
|
|
expected_tables: expectedTables,
|
|
missing_tables: missingTables,
|
|
all_tables_present: missingTables.length === 0,
|
|
total_existing: existingTables.length,
|
|
total_expected: expectedTables.length,
|
|
}),
|
|
{
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
},
|
|
);
|
|
} catch (error) {
|
|
console.error("Error checking database schema:", error);
|
|
return new Response(
|
|
JSON.stringify({
|
|
status: "error",
|
|
message: error instanceof Error ? error.message : "Unknown error",
|
|
}),
|
|
{
|
|
status: 500,
|
|
headers: { "Content-Type": "application/json" },
|
|
},
|
|
);
|
|
}
|
|
};
|