Skip to content

Commit

Permalink
Update dump.js
Browse files Browse the repository at this point in the history
  • Loading branch information
emengweb authored Dec 12, 2024
1 parent d88dead commit 1869359
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ async function backupPostgresAllDatabase() {

// 写入文件头部信息
writeStream.write(`-- PostgreSQL dump\n`);
writeStream.write(`-- 创建时间:${moment().format('YYYY-MM-DD HH:mm:ss')}\n\n`);
writeStream.write(`-- Created at: ${moment().format('YYYY-MM-DD HH:mm:ss')}\n\n`);

// 获取所有表名
const { rows: tables } = await pool.query(`
Expand All @@ -412,7 +412,7 @@ async function backupPostgresAllDatabase() {
`, [tableName]);

// 写入表结构
writeStream.write(`-- 表结构: ${tableName}\n`);
writeStream.write(`-- Table structure: ${tableName}\n`);
writeStream.write(`DROP TABLE IF EXISTS "${tableName}";\n`);
writeStream.write(`CREATE TABLE "${tableName}" (\n`);

Expand All @@ -436,7 +436,7 @@ async function backupPostgresAllDatabase() {
// 获取并写入表数据
const { rows: data } = await pool.query(`SELECT * FROM "${tableName}"`);
if (data.length > 0) {
writeStream.write(`-- 表数据: ${tableName}\n`);
writeStream.write(`-- Table data: ${tableName}\n`);
for (const row of data) {
const values = Object.values(row).map(val => {
if (val === null) return 'NULL';
Expand All @@ -454,10 +454,10 @@ async function backupPostgresAllDatabase() {
}

writeStream.end();
console.log(`数据库备份完成!备份文件: ${backupFile}`);
console.log(`Database backup completed! Backup file: ${backupFile}`);

} catch (err) {
console.error('数据库备份过程中出错:', err);
console.error('Error during database backup:', err);
} finally {
if (pool) {
await pool.end();
Expand Down Expand Up @@ -504,7 +504,7 @@ async function backupPostgresDatabase() {

// 写入文件头部信息
writeStream.write(`-- PostgreSQL dump of table ${tableName}\n`);
writeStream.write(`-- 创建时间:${moment().format('YYYY-MM-DD HH:mm:ss')}\n\n`);
writeStream.write(`-- Created at: ${moment().format('YYYY-MM-DD HH:mm:ss')}\n\n`);

// 获取表结构
const { rows: columns } = await pool.query(`
Expand All @@ -516,7 +516,7 @@ async function backupPostgresDatabase() {
`, [tableName]);

// 写入表结构
writeStream.write(`-- 表结构\n`);
writeStream.write(`-- Table structure\n`);
writeStream.write(`DROP TABLE IF EXISTS "${tableName}";\n`);
writeStream.write(`CREATE TABLE "${tableName}" (\n`);

Expand All @@ -540,7 +540,7 @@ async function backupPostgresDatabase() {
// 获取并写入表数据
const { rows: data } = await pool.query(`SELECT * FROM "${tableName}"`);
if (data.length > 0) {
writeStream.write(`-- 表数据\n`);
writeStream.write(`-- Table data\n`);
for (const row of data) {
const values = Object.values(row).map(val => {
if (val === null) return 'NULL';
Expand All @@ -556,11 +556,11 @@ async function backupPostgresDatabase() {
}

writeStream.end();
console.log(` ${tableName} 已备份到 ${backupFile}`);
console.log('数据库备份完成!');
console.log(`Table ${tableName} has been backed up to ${backupFile}`);
console.log('Database backup completed!');

} catch (err) {
console.error('数据库备份过程中出错:', err);
console.error('Error during database backup:', err);
} finally {
if (pool) {
await pool.end();
Expand Down

0 comments on commit 1869359

Please sign in to comment.