// scripts/migrate-slugs.js const Database = require('better-sqlite3'); const { join } = require('path'); // Helper to convert string to slug (matching the one in userSync.ts) const slugify = (text) => { if (!text) return ''; return text .toString() .toLowerCase() .trim() .replace(/\s+/g, '-') // Replace spaces with - .replace(/[^\w-]+/g, '') // Remove all non-word chars .replace(/--+/g, '-'); // Replace multiple - with single - }; const dbPath = join(__dirname, '..', 'data', 'users.db'); const db = new Database(dbPath); try { const users = db.prepare('SELECT id, tipeUser FROM users').all(); console.log(`🔍 Found ${users.length} users to check...`); const updateStmt = db.prepare('UPDATE users SET tipeUser = ? WHERE id = ?'); let updatedCount = 0; db.transaction(() => { for (const user of users) { if (!user.tipeUser) continue; const slug = slugify(user.tipeUser); if (slug !== user.tipeUser) { updateStmt.run(slug, user.id); console.log(`✅ Updated User ID ${user.id}: "${user.tipeUser}" -> "${slug}"`); updatedCount++; } } })(); console.log(`🎉 Migration complete. Updated ${updatedCount} user(s).`); } catch (error) { console.error('❌ Migration failed:', error); } finally { db.close(); }