Skip to content

Commit

Permalink
Update database.py
Browse files Browse the repository at this point in the history
- Fix pgsql export issue
  • Loading branch information
BattlefieldDuck committed Oct 31, 2023
1 parent c2ef735 commit 85aeae7
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions discordgsm/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 85aeae7

Please sign in to comment.