Skip to content

Commit a597deb

Browse files
authored
Merge pull request #11 from giuli007/exclude_tables
Add an --exclude option to the dump command
2 parents 7f02231 + d70ef4d commit a597deb

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ To dump out every table in that database, use `--all`:
2424

2525
sqlite-diffable dump fixtures.db dump/ --all
2626

27+
To dump all table except some specific ones, use `--exclude` one or more times:
28+
29+
sqlite-diffable dump fixtures.db dump/ --all --exclude unwanted_first_table --exclude unwanted_second_table
30+
2731
## Loading a database
2832

2933
To load a previously dumped database, run the following:

sqlite_diffable/cli.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,32 @@ def cli():
2424
)
2525
@click.argument("tables", nargs=-1, required=False)
2626
@click.option("--all", is_flag=True, help="Dump all tables")
27-
def dump(dbpath, output, tables, all):
27+
@click.option("--exclude", multiple=True, help="Tables to exclude from the dump")
28+
def dump(dbpath, output, tables, all, exclude):
2829
"""
2930
Dump a SQLite database out as flat files in the directory
3031
3132
Usage:
3233
3334
sqlite-diffable dump my.db output/ --all
3435
35-
--all dumps ever table. Or specify tables like this:
36+
--all dumps every table. Or specify tables like this:
3637
3738
sqlite-diffable dump my.db output/ entries tags
39+
40+
Exclude specific tables:
41+
42+
sqlite-diffable dump my.db output/ --all --exclude table1 --exclude table2
3843
"""
3944
if not tables and not all:
4045
raise click.ClickException("You must pass --all or specify some tables")
4146
output = pathlib.Path(output)
4247
output.mkdir(exist_ok=True)
4348
conn = sqlite_utils.Database(dbpath)
4449
if all:
45-
tables = conn.table_names()
50+
tables = set(conn.table_names()) - set(exclude)
51+
else:
52+
tables = set(tables) - set(exclude)
4653
for table in tables:
4754
tablename = table.replace("/", "")
4855
filepath = output / "{}.ndjson".format(tablename)

tests/test_dump.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ def test_dump_all(two_tables_db, tmpdir):
3737
assert (output_dir / "second_table.metadata.json").exists()
3838

3939

40+
def test_dump_exclude(two_tables_db, tmpdir):
41+
output_dir = tmpdir / "out"
42+
result = CliRunner().invoke(
43+
cli.cli, ["dump", two_tables_db, str(output_dir), "--all", "--exclude", "second_table"]
44+
)
45+
assert result.exit_code == 0, result.output
46+
assert (output_dir / "one_table.ndjson").exists()
47+
assert (output_dir / "one_table.metadata.json").exists()
48+
assert not (output_dir / "second_table.ndjson").exists()
49+
assert not (output_dir / "second_table.metadata.json").exists()
50+
51+
4052
def test_load(two_tables_db, tmpdir):
4153
output_dir = tmpdir / "out"
4254
restore_db = tmpdir / "restore.db"

0 commit comments

Comments
 (0)