feat: 实现RCU固件升级服务核心功能
- 添加升级服务主逻辑,包括定时触发升级、状态查询和日志记录 - 实现数据库初始化脚本和日志表结构 - 添加PM2部署配置文件 - 实现环境变量配置系统 - 添加API客户端模块处理外部接口调用 - 实现升级状态轮询和超时处理机制 - 添加测试用例验证核心功能
This commit is contained in:
1
scripts/alter_drop_id.sql
Normal file
1
scripts/alter_drop_id.sql
Normal file
@@ -0,0 +1 @@
|
||||
ALTER TABLE upgrade_log DROP COLUMN IF EXISTS id;
|
||||
45
scripts/init_db.js
Normal file
45
scripts/init_db.js
Normal file
@@ -0,0 +1,45 @@
|
||||
require('dotenv').config();
|
||||
const { Client } = require('pg');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const getDbConfig = (database) => ({
|
||||
host: process.env.DB_HOST,
|
||||
port: process.env.DB_PORT,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD,
|
||||
database
|
||||
});
|
||||
|
||||
const ensureDatabase = async () => {
|
||||
const adminClient = new Client(getDbConfig('postgres'));
|
||||
await adminClient.connect();
|
||||
const dbName = process.env.DB_NAME;
|
||||
const exists = await adminClient.query('SELECT 1 FROM pg_database WHERE datname = $1', [dbName]);
|
||||
if (exists.rowCount === 0) {
|
||||
await adminClient.query(`CREATE DATABASE ${dbName}`);
|
||||
}
|
||||
await adminClient.end();
|
||||
};
|
||||
|
||||
const ensureSchema = async () => {
|
||||
const dbClient = new Client(getDbConfig(process.env.DB_NAME));
|
||||
await dbClient.connect();
|
||||
const sqlPath = path.join(__dirname, 'init_db.sql');
|
||||
const sql = fs.readFileSync(sqlPath, 'utf8');
|
||||
await dbClient.query(sql);
|
||||
await dbClient.end();
|
||||
};
|
||||
|
||||
const run = async () => {
|
||||
try {
|
||||
await ensureDatabase();
|
||||
await ensureSchema();
|
||||
console.log('Database initialization complete.');
|
||||
} catch (err) {
|
||||
console.error('Database initialization failed:', err.message);
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
run();
|
||||
25
scripts/init_db.sql
Normal file
25
scripts/init_db.sql
Normal file
@@ -0,0 +1,25 @@
|
||||
-- Create database (run manually or via script if user has permissions)
|
||||
-- CREATE DATABASE test_upgrade;
|
||||
|
||||
-- Connect to test_upgrade before running the following:
|
||||
|
||||
CREATE TABLE IF NOT EXISTS upgrade_log (
|
||||
uuid UUID NOT NULL,
|
||||
start_time TIMESTAMP NOT NULL,
|
||||
roomtype_id INTEGER NOT NULL,
|
||||
host_str TEXT NOT NULL,
|
||||
filename TEXT NOT NULL,
|
||||
status TEXT,
|
||||
end_time TIMESTAMP,
|
||||
file_type TEXT,
|
||||
config_version TEXT,
|
||||
firmware_version TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS upgrade_state (
|
||||
state_key TEXT PRIMARY KEY,
|
||||
current_roomtype_index INTEGER DEFAULT 0,
|
||||
execution_count INTEGER DEFAULT 0,
|
||||
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
Reference in New Issue
Block a user