feat: add postgres config and migration route
This commit is contained in:
39
api/app.ts
39
api/app.ts
@@ -35,7 +35,44 @@ app.use(extractUser);
|
||||
// Static Uploads
|
||||
app.use('/uploads', express.static(path.join(process.cwd(), 'uploads')));
|
||||
|
||||
// Routes
|
||||
import pool from '../src/server/config/postgres.js';
|
||||
|
||||
// ... (existing routes)
|
||||
|
||||
app.get('/api/admin/migrate-to-gpi', async (req, res) => {
|
||||
const TABLES = [
|
||||
"organizations",
|
||||
"users",
|
||||
"user_organizations",
|
||||
"projects",
|
||||
"parts",
|
||||
"painting_schemes",
|
||||
"inspections",
|
||||
"instruments",
|
||||
"stock_items",
|
||||
"stock_movements"
|
||||
];
|
||||
|
||||
try {
|
||||
const client = await pool.connect();
|
||||
await client.query("CREATE SCHEMA IF NOT EXISTS gpi;");
|
||||
|
||||
const results = [];
|
||||
for (const table of TABLES) {
|
||||
try {
|
||||
await client.query(`ALTER TABLE public.${table} SET SCHEMA gpi;`);
|
||||
results.push({ table, status: 'success' });
|
||||
} catch (err: any) {
|
||||
results.push({ table, status: 'failed', error: err.message });
|
||||
}
|
||||
}
|
||||
client.release();
|
||||
res.json({ message: "Migration completed", results });
|
||||
} catch (error: any) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.use('/api/auth', authRoutes);
|
||||
app.use('/api/users', userRoutes);
|
||||
app.use('/api/projects', projectRoutes);
|
||||
|
||||
Reference in New Issue
Block a user