Migrate major controllers and services to PostgreSQL (gpi schema), fix build errors, add file and audit log support
This commit is contained in:
49
api/app.ts
49
api/app.ts
@@ -46,11 +46,14 @@ app.get('/api/admin/migrate-to-gpi', async (req, res) => {
|
||||
"user_organizations",
|
||||
"projects",
|
||||
"parts",
|
||||
"technical_data_sheets",
|
||||
"painting_schemes",
|
||||
"inspections",
|
||||
"instruments",
|
||||
"stock_items",
|
||||
"stock_movements"
|
||||
"stock_movements",
|
||||
"application_records",
|
||||
"application_record_items"
|
||||
];
|
||||
|
||||
try {
|
||||
@@ -60,14 +63,52 @@ app.get('/api/admin/migrate-to-gpi', async (req, res) => {
|
||||
const results = [];
|
||||
for (const table of TABLES) {
|
||||
try {
|
||||
// Try to move from public to gpi
|
||||
await client.query(`ALTER TABLE public.${table} SET SCHEMA gpi;`);
|
||||
results.push({ table, status: 'success' });
|
||||
results.push({ table, action: 'moved', status: 'success' });
|
||||
} catch (err: any) {
|
||||
results.push({ table, status: 'failed', error: err.message });
|
||||
// If it fails, maybe it's already in gpi?
|
||||
try {
|
||||
await client.query(`SELECT 1 FROM gpi.${table} LIMIT 1`);
|
||||
results.push({ table, action: 'check', status: 'already_in_gpi' });
|
||||
} catch (checkErr: any) {
|
||||
results.push({ table, action: 'move', status: 'failed', error: err.message });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure new tables exist
|
||||
await client.query(`
|
||||
CREATE TABLE IF NOT EXISTS gpi.files (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
filename TEXT NOT NULL,
|
||||
content_type TEXT NOT NULL,
|
||||
data BYTEA NOT NULL,
|
||||
size INTEGER NOT NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
`);
|
||||
results.push({ table: 'files', action: 'create', status: 'success_or_exists' });
|
||||
|
||||
await client.query(`
|
||||
CREATE TABLE IF NOT EXISTS gpi.stock_audit_logs (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
organization_id UUID,
|
||||
stock_item_id UUID,
|
||||
movement_id UUID,
|
||||
movement_number INTEGER,
|
||||
user_id TEXT,
|
||||
user_name TEXT,
|
||||
action TEXT,
|
||||
details TEXT,
|
||||
old_values JSONB,
|
||||
new_values JSONB,
|
||||
timestamp TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
`);
|
||||
results.push({ table: 'stock_audit_logs', action: 'create', status: 'success_or_exists' });
|
||||
client.release();
|
||||
res.json({ message: "Migration completed", results });
|
||||
res.json({ message: "Migration check completed", results });
|
||||
} catch (error: any) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user