mirror of
https://github.com/christianvidalwolf-prog/Craze-Data-check.git
synced 2026-08-03 11:35:24 +02:00
Fix: Final attempt at migration script fix.
- Removed 'new' keyword before error object. - Reverted to sequential processing to ensure stability. - Improved error reporting.
This commit is contained in:
+18
-25
@@ -7,45 +7,41 @@ const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
|
||||
|
||||
export default async function handler(req, res) {
|
||||
try {
|
||||
console.log('Starting migration v2...');
|
||||
console.log('Starting migration v3...');
|
||||
|
||||
// 1. Fetch products
|
||||
const { data: products, error: pErr } = await supabase
|
||||
.from('products')
|
||||
.select('product_id, data')
|
||||
.in('status', ['edited', 'synced', 'pending']); // Broaden to catch everything shifted
|
||||
.in('status', ['edited', 'synced', 'pending']);
|
||||
|
||||
if (pErr) throw new pErr;
|
||||
if (pErr) return res.status(500).json({ error: 'Fetch products failed', details: pErr });
|
||||
|
||||
let pCount = 0;
|
||||
if (products && products.length > 0) {
|
||||
const updates = products.map(p => {
|
||||
for (const p of products) {
|
||||
const oldData = p.data;
|
||||
// Logic: if index 12 is a date/number, it's the old structure.
|
||||
// In the new structure, index 12 is "PM Classification" (string/empty).
|
||||
if (!oldData || oldData.length < 13) continue;
|
||||
|
||||
const valAt12 = oldData[12];
|
||||
const isOld = typeof valAt12 === 'number' || (typeof valAt12 === 'string' && /^[0-9.]+$/.test(valAt12));
|
||||
|
||||
if (isOld && oldData.length < 110) { // Don't shift if already shifted
|
||||
if (isOld && oldData.length < 110) {
|
||||
const newData = [...oldData];
|
||||
newData.splice(12, 0, ''); // Shift
|
||||
pCount++;
|
||||
return supabase.from('products').update({ data: newData }).eq('product_id', p.product_id);
|
||||
newData.splice(12, 0, '');
|
||||
const { error: updateErr } = await supabase.from('products').update({ data: newData }).eq('product_id', p.product_id);
|
||||
if (!updateErr) pCount++;
|
||||
}
|
||||
return null;
|
||||
}).filter(Boolean);
|
||||
|
||||
// Run updates in parallel (limited) or batches if needed, but for 130 rows it's fine
|
||||
await Promise.all(updates);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. History
|
||||
const { data: history, error: hErr } = await supabase.from('products_history').select('*');
|
||||
if (hErr) throw hErr;
|
||||
if (hErr) return res.status(500).json({ error: 'Fetch history failed', details: hErr });
|
||||
|
||||
let hCount = 0;
|
||||
if (history && history.length > 0) {
|
||||
const hUpdates = history.map(h => {
|
||||
for (const h of history) {
|
||||
let changed = false;
|
||||
let nOld = h.old_data;
|
||||
let nNew = h.new_data;
|
||||
@@ -62,22 +58,19 @@ export default async function handler(req, res) {
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
hCount++;
|
||||
return supabase.from('products_history').update({ old_data: nOld, new_data: nNew }).eq('id', h.id);
|
||||
const { error: histErr } = await supabase.from('products_history').update({ old_data: nOld, new_data: nNew }).eq('id', h.id);
|
||||
if (!histErr) hCount++;
|
||||
}
|
||||
return null;
|
||||
}).filter(Boolean);
|
||||
await Promise.all(hUpdates);
|
||||
}
|
||||
}
|
||||
|
||||
return res.json({
|
||||
success: true,
|
||||
productsMigrated: pCount,
|
||||
historyMigrated: hCount,
|
||||
message: 'Migration v2 finished.'
|
||||
message: 'Migration v3 finished.'
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('Migration error:', err);
|
||||
return res.status(500).json({ error: err.message, stack: err.stack });
|
||||
return res.status(500).json({ error: err.message });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user