import { Client } from 'pg'; import config from '../../src/config/config.js'; async function main() { const client = new Client({ host: config.db.host, port: config.db.port, user: config.db.user, password: config.db.password, database: config.db.database, }); await client.connect(); // 预创建今日分区,避免“无分区时 INSERT 直接失败” await client.query('SELECT heartbeat.ensure_partitions(current_date, current_date)'); const ts = Date.now(); await client.query( `INSERT INTO heartbeat.heartbeat_events ( ts_ms, hotel_id, room_id, device_id, ip, power_state, guest_type, cardless_state, service_mask, pms_state, carbon_state, device_count, comm_seq, extra ) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14)`, [ ts, 1, 101, 'dev-1', '192.168.0.1', 1, 0, 0, 5, 0, 0, 1, 1, { source: 'smoke-test' }, ] ); const partitions = await client.query( `SELECT c.relname AS partition FROM pg_inherits i JOIN pg_class c ON c.oid = i.inhrelid JOIN pg_class p ON p.oid = i.inhparent JOIN pg_namespace n ON n.oid = p.relnamespace WHERE n.nspname = 'heartbeat' AND p.relname = 'heartbeat_events' ORDER BY c.relname` ); const cnt = await client.query( 'SELECT count(*)::int AS n FROM heartbeat.heartbeat_events' ); console.log('partitions:', partitions.rows.map((r) => r.partition)); console.log('rows:', cnt.rows[0].n); await client.end(); } main().catch((err) => { console.error('smoke test failed:', err); process.exit(1); });