Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backup and restore NULL values in tables #89

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
def backup_geocml_db():
try:
conn = psycopg2.connect(dbname="geocml_db",
user="geocml",
password="geocml",
user="postgres",
password="admin",
host="geocml-postgres",
port=5432)
except psycopg2.OperationalError:
Expand All @@ -25,7 +25,7 @@ def backup_geocml_db():

# Write table schemata to .tabor file
out = subprocess.run(["tabor", "write", "--db", "geocml_db",
"--username", "postgres", "--password", "admin",
"--username", "geocml", "--password", "geocml",
"--host", "geocml-postgres",
"--file", os.path.join(path_to_backup_dir, "geocml_db.tabor")],
capture_output=True)
Expand Down Expand Up @@ -56,7 +56,7 @@ def backup_geocml_db():

data_file_path = os.path.join(path_to_backup_dir, "data:{}.{}.csv".format(schema[0], table[2]))
data_file = open(data_file_path, "w")
cursor.copy_expert(f"""COPY {schema[0]}."{table[2]}" TO STDOUT WITH (FORMAT csv, DELIMITER ',', HEADER);""", data_file)
cursor.copy_expert(f"""COPY {schema[0]}."{table[2]}" TO STDOUT WITH (FORMAT csv, DELIMITER ',', HEADER, NULL 'NULL');""", data_file)
data_file.close()

if delete_backup_dir: # nothing to back up
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def restore_geocml_db_from_backups():
file_name_split = file_name_split[1].split(".")
data_file = open(os.path.join(db_backups_dir, most_recent_backup, csv_data_file), "r").readlines()
cursor.copy_from(StringIO("".join(data_file[1::])), f"{file_name_split[1]}", sep=",",
columns=tuple(data_file[0].replace("\n", "").split(",")))
columns=tuple(data_file[0].replace("\n", "").split(",")), null="NULL")
log("Finished loading data!")

conn.commit()
Expand Down
Loading