2 Helper functions for handling DB accesses.
7 from .connection import get_pg_env
8 from ..errors import UsageError
10 LOG = logging.getLogger()
12 def execute_file(dsn, fname, ignore_errors=False):
13 """ Read an SQL file and run its contents against the given database
18 cmd.extend(('-v', 'ON_ERROR_STOP=1'))
19 proc = subprocess.Popen(cmd, env=get_pg_env(dsn), stdin=subprocess.PIPE)
21 if not LOG.isEnabledFor(logging.INFO):
22 proc.stdin.write('set client_min_messages to WARNING;'.encode('utf-8'))
24 with fname.open('rb') as fdesc:
25 chunk = fdesc.read(2048)
26 while chunk and proc.poll() is None:
27 proc.stdin.write(chunk)
28 chunk = fdesc.read(2048)
35 raise UsageError("Failed to execute SQL file.")