165 lines
4.1 KiB
JavaScript
165 lines
4.1 KiB
JavaScript
// 检查数据库结构的脚本
|
|
const { Client } = require('pg');
|
|
const dotenv = require('dotenv');
|
|
const path = require('path');
|
|
|
|
// 加载环境变量
|
|
dotenv.config({ path: path.resolve(__dirname, '../.env') });
|
|
|
|
// 获取数据库连接字符串
|
|
const databaseUrl = process.env.DATABASE_URL;
|
|
|
|
if (!databaseUrl) {
|
|
console.error('缺少数据库连接字符串。请确保.env文件中包含DATABASE_URL');
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log('使用PostgreSQL连接字符串连接数据库...');
|
|
|
|
// 创建PostgreSQL客户端
|
|
const client = new Client({
|
|
connectionString: databaseUrl,
|
|
});
|
|
|
|
// 获取所有表
|
|
async function getAllTables() {
|
|
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(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(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() {
|
|
try {
|
|
// 连接数据库
|
|
await client.connect();
|
|
console.log('成功连接到数据库');
|
|
|
|
// 获取所有表
|
|
const tables = await getAllTables();
|
|
|
|
if (!tables) {
|
|
console.error('无法获取表列表');
|
|
process.exit(1);
|
|
}
|
|
|
|
// 获取我们需要的表的结构和数据示例
|
|
const requiredTables = ['projects', 'influencers', 'project_influencers', 'posts'];
|
|
|
|
for (const tableName of requiredTables) {
|
|
if (tables.includes(tableName)) {
|
|
await getTableSchema(tableName);
|
|
await getTableDataSample(tableName);
|
|
} else {
|
|
console.log(`\n表 ${tableName} 不存在`);
|
|
}
|
|
}
|
|
|
|
console.log('\n数据库结构检查完成');
|
|
} catch (error) {
|
|
console.error('检查数据库结构时出错:', error);
|
|
process.exit(1);
|
|
} finally {
|
|
// 关闭数据库连接
|
|
await client.end();
|
|
}
|
|
}
|
|
|
|
// 运行主函数
|
|
main();
|