2026-08-17 12:25:42 +08:00
|
|
|
|
const { Sequelize } = require('./node_modules/sequelize');
|
2026-09-23 16:34:08 +08:00
|
|
|
|
// 🔴 口令不再内置明文(2026-09-23 入库口令处置 P2)。运行前请注入 DB_PASSWORD:
|
|
|
|
|
|
// DB_PASSWORD='<本机 DB 口令>' node seed-carousel.js
|
|
|
|
|
|
const sequelize = new Sequelize(
|
|
|
|
|
|
process.env.DB_NAME || 'competition_db',
|
|
|
|
|
|
process.env.DB_USER || 'root',
|
|
|
|
|
|
process.env.DB_PASSWORD,
|
|
|
|
|
|
{
|
|
|
|
|
|
host: process.env.DB_HOST || 'localhost',
|
|
|
|
|
|
port: parseInt(process.env.DB_PORT, 10) || 3306,
|
|
|
|
|
|
dialect: 'mysql',
|
|
|
|
|
|
logging: false,
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
2026-08-17 12:25:42 +08:00
|
|
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
await sequelize.authenticate();
|
|
|
|
|
|
console.log('DB connected');
|
|
|
|
|
|
|
|
|
|
|
|
// MariaDB 5.5 compatible: only one TIMESTAMP can have DEFAULT CURRENT_TIMESTAMP
|
|
|
|
|
|
await sequelize.query(`
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS carousel (
|
|
|
|
|
|
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
|
|
|
|
image_url VARCHAR(500) NOT NULL,
|
|
|
|
|
|
sort_order INT NOT NULL DEFAULT 0,
|
|
|
|
|
|
link_url VARCHAR(500) NULL,
|
|
|
|
|
|
enabled TINYINT(1) NOT NULL DEFAULT 1,
|
|
|
|
|
|
created_at DATETIME NOT NULL,
|
|
|
|
|
|
updated_at DATETIME NOT NULL,
|
|
|
|
|
|
PRIMARY KEY (id),
|
|
|
|
|
|
KEY idx_sort_order (sort_order),
|
|
|
|
|
|
KEY idx_enabled (enabled)
|
|
|
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
|
|
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
|
|
const [rows] = await sequelize.query('SELECT COUNT(*) as cnt FROM carousel');
|
|
|
|
|
|
console.log('Existing:', rows[0].cnt);
|
|
|
|
|
|
|
|
|
|
|
|
if (rows[0].cnt === 0) {
|
|
|
|
|
|
await sequelize.query("INSERT INTO carousel (image_url, sort_order, enabled, created_at, updated_at) VALUES ('https://images.unsplash.com/photo-1446776811953-b23d57bd21aa?w=1200', 1, 1, NOW(), NOW())");
|
|
|
|
|
|
await sequelize.query("INSERT INTO carousel (image_url, sort_order, enabled, created_at, updated_at) VALUES ('https://images.unsplash.com/photo-1451187580459-43490279c0fa?w=1200', 2, 1, NOW(), NOW())");
|
|
|
|
|
|
await sequelize.query("INSERT INTO carousel (image_url, sort_order, enabled, created_at, updated_at) VALUES ('https://images.unsplash.com/photo-1614728263952-84ea256f9679?w=1200', 3, 1, NOW(), NOW())");
|
|
|
|
|
|
console.log('Inserted 3 carousel slides');
|
|
|
|
|
|
}
|
|
|
|
|
|
console.log('Done');
|
|
|
|
|
|
} catch(e) { console.error(e.message); }
|
|
|
|
|
|
finally { await sequelize.close(); }
|
|
|
|
|
|
})();
|