From 85aeae7cecd948efe01a98f926e25155d6d445e2 Mon Sep 17 00:00:00 2001 From: Battlefield Duck Date: Wed, 1 Nov 2023 04:00:44 +0800 Subject: [PATCH] Update database.py - Fix pgsql export issue --- discordgsm/database.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/discordgsm/database.py b/discordgsm/database.py index 42d74e0..d26bbda 100644 --- a/discordgsm/database.py +++ b/discordgsm/database.py @@ -571,21 +571,20 @@ def export(self, *, to_driver: str): else: file = os.path.join(export_path, 'servers.sql') - if self.driver == Driver.SQLite.value: + if self.driver == Driver.SQLite: # Export data to SQL file with open(file, 'w', encoding='utf-8') as f: for line in self.conn.iterdump(): f.write('%s\n' % line) - elif self.driver == Driver.PostgreSQL.value: - cursor = self.cursor() + elif self.driver == Driver.PostgreSQL: + DATABASE_URL: str = os.getenv('DATABASE_URL', '') - # Export data to SQL file - with open(file, 'w', encoding='utf-8') as f: - cursor.copy_expert( - "COPY servers TO STDOUT WITH CSV DELIMITER ';'", f) + # Define the command to export the table + cmd = f"pg_dump {DATABASE_URL} -f {file}" - cursor.close() - elif self.driver == Driver.MongoDB.value: + # Execute the command + os.system(cmd) + elif self.driver == Driver.MongoDB: print("MongoDB does not support exporting to SQL file directly.") return