Files
api-debug/server/migrate.js
jason 96bdc292bb Initial commit: API Debug Tool
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-12 04:32:35 +08:00

45 lines
1.6 KiB
JavaScript

const pool = require('./db');
const bcrypt = require('bcryptjs');
async function migrate() {
try {
console.log('Starting migration...');
// 1. Create users table
await pool.query(`
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
`);
console.log('users table ensured.');
// 2. Add default admin user if no users exist
const [users] = await pool.query('SELECT * FROM users');
if (users.length === 0) {
const hashedPassword = await bcrypt.hash('123456', 10);
await pool.query('INSERT INTO users (username, password) VALUES (?, ?)', ['admin', hashedPassword]);
console.log('Default admin user created.');
}
// 3. Add category column to endpoints if it doesn't exist
const [columns] = await pool.query(`SHOW COLUMNS FROM endpoints LIKE 'category'`);
if (columns.length === 0) {
await pool.query(`ALTER TABLE endpoints ADD COLUMN category VARCHAR(50) DEFAULT '旗舰版PP' AFTER name`);
console.log('Added category column to endpoints table.');
} else {
console.log('category column already exists in endpoints.');
}
console.log('Migration completed successfully.');
process.exit(0);
} catch (error) {
console.error('Migration failed:', error);
process.exit(1);
}
}
migrate();