Skip to content

Commit 1b7ed9c

Browse files
committed
Added --all flag for dumping all tables
1 parent 6320610 commit 1b7ed9c

4 files changed

Lines changed: 35 additions & 6 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ Usage:
1010

1111
$ sqlite-diffable dump fixtures.db out/ facetable
1212

13-
This dumps the table called "facetable" from fixtures.db into the out/ directory.
13+
This dumps the table called "facetable" from fixtures.db into the out/ directory.
14+
15+
To dump out all tables, use `--all`:
16+
17+
$ sqlite-diffable dump fixtures.db out/ --all

sqlite_diffable/cli.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,24 @@ def cli():
1818
@click.argument(
1919
"output", type=click.Path(file_okay=False, dir_okay=True), required=True
2020
)
21-
@click.argument("tables", nargs=-1, required=True)
22-
def dump(path, output, tables):
21+
@click.argument("tables", nargs=-1, required=False)
22+
@click.option("--all", is_flag=True)
23+
def dump(path, output, tables, all):
24+
if not tables and not all:
25+
raise click.ClickException("You must pass --all or specify some tables")
2326
output = pathlib.Path(output)
2427
output.mkdir(exist_ok=True)
2528
conn = sqlite_utils.Database(path)
29+
if all:
30+
tables = conn.table_names()
2631
for table in tables:
27-
filepath = output / "{}.ndjson".format(table)
28-
metapath = output / "{}.metadata.json".format(table)
32+
tablename = table.replace("/", "")
33+
filepath = output / "{}.ndjson".format(tablename)
34+
metapath = output / "{}.metadata.json".format(tablename)
2935
# Dump rows to filepath
3036
with filepath.open("w") as fp:
3137
for row in conn[table].rows:
32-
fp.write(json.dumps(list(row.values())) + "\n")
38+
fp.write(json.dumps(list(row.values()), default=repr) + "\n")
3339
fp.close()
3440
# Dump out metadata
3541
metapath.open("w").write(

tests/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,10 @@ def one_table_db(tmpdir):
1515
pk="id",
1616
)
1717
return path
18+
19+
20+
@pytest.fixture
21+
def two_tables_db(one_table_db):
22+
db = sqlite_utils.Database(one_table_db)
23+
db["second_table"].insert_all([{"id": 1, "name": "Cleo"}], pk="id")
24+
return one_table_db

tests/test_dump.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,15 @@ def test_dump(one_table_db, tmpdir):
2222
"columns": ["id", "name"],
2323
"schema": "CREATE TABLE [one_table] (\n [id] INTEGER PRIMARY KEY,\n [name] TEXT\n)",
2424
} == json.load(metadata)
25+
26+
27+
def test_dump_all(two_tables_db, tmpdir):
28+
output_dir = tmpdir / "out"
29+
result = CliRunner().invoke(
30+
cli.cli, ["dump", two_tables_db, str(output_dir), "--all"]
31+
)
32+
assert 0 == result.exit_code, result.output
33+
assert (output_dir / "one_table.ndjson").exists()
34+
assert (output_dir / "one_table.metadata.json").exists()
35+
assert (output_dir / "second_table.ndjson").exists()
36+
assert (output_dir / "second_table.metadata.json").exists()

0 commit comments

Comments
 (0)