From 29290e6ac967359e744a08f140d59f3b03c1f5f6 Mon Sep 17 00:00:00 2001 From: Matin Nuhamunada Date: Thu, 18 Apr 2024 15:39:42 +0200 Subject: [PATCH] feat: add script to upload database to motherduck --- workflow/scripts/motherduck.py | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 workflow/scripts/motherduck.py diff --git a/workflow/scripts/motherduck.py b/workflow/scripts/motherduck.py new file mode 100644 index 00000000..e4220c3c --- /dev/null +++ b/workflow/scripts/motherduck.py @@ -0,0 +1,37 @@ +import argparse +import duckdb +import logging + +log_format = "%(levelname)-8s %(asctime)s %(message)s" +date_format = "%d/%m %H:%M:%S" +logging.basicConfig(format=log_format, datefmt=date_format, level=logging.DEBUG) + +def upload_to_motherduck(database_path, database_name): + """ + Connects to a local DuckDB database, loads a remote DuckDB database, + and creates or replaces a remote database from the current local database. + + Args: + database_path (str): The path to the local DuckDB database. + database_name (str): The name of the remote database to create or replace. + """ + logging.info('Connecting to the local DuckDB database...') + local_conn = duckdb.connect(database_path) + + logging.info('Loading the remote DuckDB database...') + local_conn.execute("LOAD 'motherduck'") + + logging.info('Creating or replacing the remote database from the current local database...') + local_conn.execute(f"CREATE OR REPLACE DATABASE {database_name} FROM CURRENT_DATABASE()") + + logging.info('Closing the connection...') + local_conn.close() + +if __name__ == "__main__": + logging.basicConfig(level=logging.INFO) + parser = argparse.ArgumentParser(description='Upload a local DuckDB database to a remote DuckDB database.') + parser.add_argument('database_path', type=str, help='The path to the local DuckDB database.') + parser.add_argument('--database_name', type=str, default='bgcflow', help='The name of the remote database to create or replace. Default is "bgcflow".') + args = parser.parse_args() + + upload_to_motherduck(args.database_path, args.database_name)