diff --git a/api/debug-db.js b/api/debug-db.js new file mode 100644 index 0000000..45649d3 --- /dev/null +++ b/api/debug-db.js @@ -0,0 +1,26 @@ +import { createClient } from '@supabase/supabase-js'; + +const SUPABASE_URL = process.env.SUPABASE_URL || 'https://hwithddwaapyhnfwcesj.supabase.co'; +const SUPABASE_KEY = process.env.SUPABASE_SERVICE_KEY || process.env.SUPABASE_ANON_KEY; + +const supabase = createClient(SUPABASE_URL, SUPABASE_KEY); + +export default async function handler(req, res) { + try { + const { data: products } = await supabase.from('products').select('*').limit(5); + const { data: history } = await supabase.from('products_history').select('*').limit(5); + const { count } = await supabase.from('products').select('*', { count: 'exact', head: true }); + + return res.json({ + productsCount: count, + sampleProducts: products, + sampleHistory: history, + env: { + hasUrl: !!process.env.SUPABASE_URL, + hasKey: !!process.env.SUPABASE_SERVICE_KEY + } + }); + } catch (err) { + return res.status(500).json({ error: err.message }); + } +} diff --git a/api/migrate-indices.js b/api/migrate-indices.js new file mode 100644 index 0000000..ee2506c --- /dev/null +++ b/api/migrate-indices.js @@ -0,0 +1,71 @@ +import { createClient } from '@supabase/supabase-js'; + +const SUPABASE_URL = process.env.SUPABASE_URL || 'https://hwithddwaapyhnfwcesj.supabase.co'; +const SUPABASE_KEY = process.env.SUPABASE_SERVICE_KEY; + +const supabase = createClient(SUPABASE_URL, SUPABASE_KEY); + +export default async function handler(req, res) { + if (!SUPABASE_KEY) return res.status(500).json({ error: 'Service key missing' }); + + try { + console.log('Starting migration to shift indices...'); + + // 1. Migrate 'products' table + const { data: products } = await supabase + .from('products') + .select('product_id, data') + .eq('status', 'edited'); // Only migrated ones need shifting + + let pCount = 0; + if (products) { + for (const p of products) { + if (p.data.length >= 13 && (typeof p.data[12] === 'number' || (typeof p.data[12] === 'string' && /^[0-9.]+$/.test(p.data[12])))) { + const newData = [...p.data]; + newData.splice(12, 0, ''); // Insert empty PM Classification + await supabase.from('products').update({ data: newData }).eq('product_id', p.product_id); + pCount++; + } + } + } + + // 2. Migrate 'products_history' table (optional but good for consistency) + const { data: history } = await supabase + .from('products_history') + .select('id, old_data, new_data'); + + let hCount = 0; + if (history) { + for (const h of history) { + let changed = false; + let nOld = h.old_data; + let nNew = h.new_data; + + if (nOld && nOld.length >= 13 && (typeof nOld[12] === 'number' || (typeof nOld[12] === 'string' && /^[0-9.]+$/.test(nOld[12])))) { + nOld = [...nOld]; + nOld.splice(12, 0, ''); + changed = true; + } + if (nNew && nNew.length >= 13 && (typeof nNew[12] === 'number' || (typeof nNew[12] === 'string' && /^[0-9.]+$/.test(nNew[12])))) { + nNew = [...nNew]; + nNew.splice(12, 0, ''); + changed = true; + } + + if (changed) { + await supabase.from('products_history').update({ old_data: nOld, new_data: nNew }).eq('id', h.id); + hCount++; + } + } + } + + return res.json({ + success: true, + productsMigrated: pCount, + historyMigrated: hCount, + message: 'Migration completed successfully. Indices >= 12 shifted by +1.' + }); + } catch (err) { + return res.status(500).json({ error: err.message }); + } +}