增加微信支付,商城逻辑,公告
This commit is contained in:
87
run_migration.js
Normal file
87
run_migration.js
Normal file
@@ -0,0 +1,87 @@
|
||||
const mysql = require('mysql2/promise');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
require('dotenv').config();
|
||||
|
||||
async function runMigration() {
|
||||
let connection;
|
||||
|
||||
try {
|
||||
// 创建数据库连接
|
||||
connection = await mysql.createConnection({
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD,
|
||||
database: process.env.DB_NAME,
|
||||
charset: 'utf8mb4'
|
||||
});
|
||||
|
||||
console.log('数据库连接成功');
|
||||
|
||||
// 直接定义SQL语句
|
||||
const sqlStatements = [
|
||||
`CREATE TABLE IF NOT EXISTS \`payment_orders\` (
|
||||
\`id\` int(11) NOT NULL AUTO_INCREMENT COMMENT '订单ID',
|
||||
\`user_id\` int(11) NOT NULL COMMENT '用户ID',
|
||||
\`out_trade_no\` varchar(64) NOT NULL COMMENT '商户订单号',
|
||||
\`transaction_id\` varchar(64) DEFAULT NULL COMMENT '微信支付订单号',
|
||||
\`total_fee\` int(11) NOT NULL COMMENT '订单金额(分)',
|
||||
\`body\` varchar(128) NOT NULL COMMENT '商品描述',
|
||||
\`trade_type\` varchar(16) NOT NULL COMMENT '交易类型',
|
||||
\`prepay_id\` varchar(64) DEFAULT NULL COMMENT '预支付交易会话标识',
|
||||
\`mweb_url\` text DEFAULT NULL COMMENT 'H5支付跳转链接',
|
||||
\`status\` enum('pending','paid','failed','cancelled') NOT NULL DEFAULT 'pending' COMMENT '支付状态',
|
||||
\`paid_at\` datetime DEFAULT NULL COMMENT '支付完成时间',
|
||||
\`created_at\` datetime NOT NULL COMMENT '创建时间',
|
||||
\`updated_at\` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||||
PRIMARY KEY (\`id\`),
|
||||
UNIQUE KEY \`uk_out_trade_no\` (\`out_trade_no\`),
|
||||
KEY \`idx_user_id\` (\`user_id\`),
|
||||
KEY \`idx_status\` (\`status\`),
|
||||
KEY \`idx_created_at\` (\`created_at\`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='支付订单表'`,
|
||||
|
||||
`ALTER TABLE \`users\` ADD COLUMN \`payment_status\` enum('unpaid','paid') NOT NULL DEFAULT 'unpaid' COMMENT '支付状态'`,
|
||||
|
||||
`ALTER TABLE \`users\` ADD KEY \`idx_payment_status\` (\`payment_status\`)`
|
||||
];
|
||||
|
||||
console.log(`准备执行 ${sqlStatements.length} 条SQL语句`);
|
||||
|
||||
// 执行每条SQL语句
|
||||
for (let i = 0; i < sqlStatements.length; i++) {
|
||||
const statement = sqlStatements[i];
|
||||
console.log(`执行第 ${i + 1} 条语句...`);
|
||||
|
||||
try {
|
||||
await connection.execute(statement);
|
||||
console.log(`✓ 第 ${i + 1} 条语句执行成功`);
|
||||
} catch (error) {
|
||||
if (error.code === 'ER_DUP_FIELDNAME') {
|
||||
console.log(`⚠ 第 ${i + 1} 条语句跳过(字段已存在): ${error.message}`);
|
||||
} else if (error.code === 'ER_TABLE_EXISTS_ERROR') {
|
||||
console.log(`⚠ 第 ${i + 1} 条语句跳过(表已存在): ${error.message}`);
|
||||
} else if (error.code === 'ER_DUP_KEYNAME') {
|
||||
console.log(`⚠ 第 ${i + 1} 条语句跳过(索引已存在): ${error.message}`);
|
||||
} else {
|
||||
console.error(`✗ 第 ${i + 1} 条语句执行失败:`, error.message);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log('\n✅ 数据库迁移完成!');
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 数据库迁移失败:', error.message);
|
||||
process.exit(1);
|
||||
} finally {
|
||||
if (connection) {
|
||||
await connection.end();
|
||||
console.log('数据库连接已关闭');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 运行迁移
|
||||
runMigration();
|
||||
Reference in New Issue
Block a user