feat: Implement file upload and management system with database integration
This commit is contained in:
60
migrate-to-username.js
Normal file
60
migrate-to-username.js
Normal file
@@ -0,0 +1,60 @@
|
||||
import Database from "better-sqlite3";
|
||||
|
||||
const db = new Database("./data/database.sqlite");
|
||||
|
||||
console.log("🔄 Migrating database to username-based authentication...\n");
|
||||
|
||||
try {
|
||||
// Check current table structure
|
||||
const tableInfo = db.prepare("PRAGMA table_info(users)").all();
|
||||
console.log("Current users table columns:");
|
||||
tableInfo.forEach(col => console.log(` - ${col.name}: ${col.type}`));
|
||||
|
||||
const hasUsername = tableInfo.some(col => col.name === 'username');
|
||||
const hasEmail = tableInfo.some(col => col.name === 'email');
|
||||
|
||||
if (hasUsername) {
|
||||
console.log("✅ Username column already exists!");
|
||||
} else if (hasEmail) {
|
||||
console.log("\n📝 Adding username column...");
|
||||
|
||||
// Add username column
|
||||
db.exec(`ALTER TABLE users ADD COLUMN username TEXT;`);
|
||||
console.log("✅ Username column added");
|
||||
|
||||
// Copy email data to username for existing users
|
||||
console.log("📋 Migrating existing email data to username...");
|
||||
const result = db.exec(`UPDATE users SET username = email WHERE username IS NULL;`);
|
||||
console.log("✅ Data migrated");
|
||||
|
||||
// Create unique index on username
|
||||
console.log("🔍 Creating unique index on username...");
|
||||
try {
|
||||
db.exec(`CREATE UNIQUE INDEX idx_users_username_unique ON users(username);`);
|
||||
console.log("✅ Unique index created");
|
||||
} catch (e) {
|
||||
console.log("ℹ️ Index already exists or couldn't be created:", e.message);
|
||||
}
|
||||
|
||||
// Verify migration
|
||||
console.log("\n🔍 Verifying migration...");
|
||||
const users = db.prepare("SELECT id, name, username, email FROM users LIMIT 3").all();
|
||||
console.log("Sample users after migration:");
|
||||
users.forEach(user => {
|
||||
console.log(` - ${user.name}: username="${user.username}", email="${user.email || 'NULL'}"`);
|
||||
});
|
||||
|
||||
console.log("\n✅ Migration completed successfully!");
|
||||
console.log("ℹ️ You can now log in using usernames instead of emails");
|
||||
|
||||
} else {
|
||||
console.log("❌ Neither username nor email column found. Database may be corrupted.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error("❌ Migration failed:", error.message);
|
||||
process.exit(1);
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
Reference in New Issue
Block a user