Implement --save plus tests for --save and --dump, refs #272

This commit is contained in:
Simon Willison 2021-06-16 07:43:55 -07:00
commit 988d363657
2 changed files with 50 additions and 0 deletions

View file

@ -1117,6 +1117,11 @@ def query(
help="Named :parameters for SQL query",
)
@click.option("--dump", is_flag=True, help="Dump SQL for in-memory database")
@click.option(
"--save",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
help="Save in-memory database to this file",
)
@load_extension_option
def memory(
paths,
@ -1133,6 +1138,7 @@ def memory(
raw,
param,
dump,
save,
load_extension,
):
"Execute SQL query against an in-memory database, optionally populated by imported data"
@ -1159,6 +1165,12 @@ def memory(
click.echo(line)
return
if save:
db2 = sqlite_utils.Database(save)
for line in db.conn.iterdump():
db2.execute(line)
return
for alias, attach_path in attach:
db.attach(alias, attach_path)
_load_extensions(db, load_extension)