Source code for uw.local.deploy.bin.load_binary

"""Load a single value from a file to the database.

This script takes a query and a filename.  The query must contain the value
placeholder %s in exactly one location.  It reads the file as bytes, and
executes the query, inserting the file contents at the placeholder.  This is
intended primarily for debugging and diagnosis purposes.
"""

import argparse
from pathlib import Path
from sys import exit

from uw.sql.wrap import open_psycopg2_db_service_cursor

rawparam = "%s"
param = "%(content)s"

[docs]def main (): parser = argparse.ArgumentParser () parser.add_argument ('query', type=str) parser.add_argument ('file', type=str) args = parser.parse_args () cursor = open_psycopg2_db_service_cursor () rawquery = args.query query = rawquery.replace (rawparam, param) if len (query) - len (rawquery) != len (param) - len (rawparam): print ('Query must contain exactly one instance of %s.') exit (-1) content = Path (args.file).read_bytes () print ('Read %d bytes from %s.' % (len (content), args.file)) cursor.execute_none (query, content=content) cursor.connection.commit () print ('Finished executing query.')