Files
promote/backend/scripts/db-inspector/postgres-schema.js
2025-03-11 17:54:57 +08:00

240 lines
6.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 检查数据库结构的脚本
const { Client } = require('pg');
const dotenv = require('dotenv');
const path = require('path');
const fs = require('fs');
// 加载环境变量
dotenv.config({ path: path.resolve(__dirname, '../../.env') });
// 获取数据库连接字符串
const databaseUrl = process.env.DATABASE_URL;
if (!databaseUrl) {
console.error('缺少数据库连接字符串。请确保.env文件中包含DATABASE_URL');
process.exit(1);
}
// 定义输出目录
const DB_REPORTS_DIR = '/Users/liam/code/promote/backend/db-reports';
// 连接数据库
async function connect() {
console.log('使用PostgreSQL连接字符串连接数据库...');
// 创建PostgreSQL客户端
const client = new Client({
connectionString: databaseUrl,
});
try {
await client.connect();
console.log('成功连接到数据库');
return client;
} catch (error) {
console.error('连接数据库失败:', error);
throw error;
}
}
// 断开数据库连接
async function disconnect(client) {
try {
await client.end();
console.log('已断开数据库连接');
} catch (error) {
console.error('断开数据库连接失败:', error);
}
}
// 获取所有表
async function getAllTables(client) {
console.log('\n获取所有表...');
try {
const query = `
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name;
`;
const result = await client.query(query);
if (!result.rows || result.rows.length === 0) {
console.log('没有找到任何表');
return null;
}
console.log('找到以下表:');
result.rows.forEach(row => {
console.log(` - ${row.table_name}`);
});
return result.rows.map(row => row.table_name);
} catch (error) {
console.error('获取所有表时出错:', error);
return null;
}
}
// 获取表结构
async function getTableSchema(client, tableName) {
console.log(`\n获取表 ${tableName} 的结构...`);
try {
const query = `
SELECT
column_name,
data_type,
is_nullable,
column_default
FROM
information_schema.columns
WHERE
table_schema = 'public' AND
table_name = $1
ORDER BY
ordinal_position;
`;
const result = await client.query(query, [tableName]);
if (!result.rows || result.rows.length === 0) {
console.log(`${tableName} 不存在或没有列`);
return null;
}
console.log(`${tableName} 的列:`);
result.rows.forEach(column => {
console.log(` - ${column.column_name} (${column.data_type}, ${column.is_nullable === 'YES' ? '可为空' : '不可为空'}, 默认值: ${column.column_default || 'NULL'})`);
});
return result.rows;
} catch (error) {
console.error(`获取表 ${tableName} 结构时出错:`, error);
return null;
}
}
// 获取表数据示例
async function getTableDataSample(client, tableName, limit = 5) {
console.log(`\n获取表 ${tableName} 的数据示例 (最多 ${limit} 行)...`);
try {
const query = `
SELECT *
FROM "${tableName}"
LIMIT $1;
`;
const result = await client.query(query, [limit]);
if (!result.rows || result.rows.length === 0) {
console.log(`${tableName} 中没有数据`);
return null;
}
console.log(`${tableName} 的数据示例:`);
result.rows.forEach((row, index) => {
console.log(`${index + 1}:`);
Object.entries(row).forEach(([key, value]) => {
console.log(` ${key}: ${value}`);
});
});
return result.rows;
} catch (error) {
console.error(`获取表 ${tableName} 数据示例时出错:`, error);
return null;
}
}
// 主函数
async function main() {
let client = null;
let outputBuffer = '';
const originalConsoleLog = console.log;
// 重定向console.log到buffer和控制台
console.log = function() {
// 调用原始的console.log
originalConsoleLog.apply(console, arguments);
// 写入到buffer
outputBuffer += Array.from(arguments).join(' ') + '\n';
};
try {
// 连接数据库
client = await connect();
// 获取所有表
const tables = await getAllTables(client);
if (!tables) {
console.error('无法获取表列表');
process.exit(1);
}
console.log('\n所有PostgreSQL表:');
console.log(tables.join(', '));
// 获取特定表的结构,但不获取数据示例
const requiredTables = ['projects', 'influencers', 'project_influencers', 'posts'];
for (const tableName of requiredTables) {
if (tables.includes(tableName)) {
await getTableSchema(client, tableName);
// 移除数据示例检查
// await getTableDataSample(client, tableName);
} else {
console.log(`\n${tableName} 不存在`);
}
}
console.log('\n数据库结构检查完成');
// 保存输出到指定目录
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
// 确保目录存在
if (!fs.existsSync(DB_REPORTS_DIR)) {
fs.mkdirSync(DB_REPORTS_DIR, { recursive: true });
}
const outputPath = path.join(DB_REPORTS_DIR, `postgres-schema-${timestamp}.log`);
fs.writeFileSync(outputPath, outputBuffer);
originalConsoleLog(`结果已保存到: ${outputPath}`);
} catch (error) {
console.error('检查数据库结构时出错:', error);
process.exit(1);
} finally {
// 恢复原始的console.log
console.log = originalConsoleLog;
// 关闭数据库连接
if (client) {
await disconnect(client);
}
}
}
// 导出函数
module.exports = {
connect,
disconnect,
getAllTables,
getTableSchema,
getTableDataSample,
main
};
// 如果直接运行此脚本则执行main函数
if (require.main === module) {
main().catch(error => {
console.error('运行脚本时出错:', error);
process.exit(1);
});
}