Skip to content

Add an --exclude option to the dump command #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ To dump out every table in that database, use `--all`:

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

To dump all table except some specific ones, use `--exclude` one or more times:

sqlite-diffable dump fixtures.db dump/ --all --exclude unwanted_first_table --exclude unwanted_second_table

## Loading a database

To load a previously dumped database, run the following:
Expand Down
13 changes: 10 additions & 3 deletions sqlite_diffable/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,32 @@ def cli():
)
@click.argument("tables", nargs=-1, required=False)
@click.option("--all", is_flag=True, help="Dump all tables")
def dump(dbpath, output, tables, all):
@click.option("--exclude", multiple=True, help="Tables to exclude from the dump")
def dump(dbpath, output, tables, all, exclude):
"""
Dump a SQLite database out as flat files in the directory

Usage:

sqlite-diffable dump my.db output/ --all

--all dumps ever table. Or specify tables like this:
--all dumps every table. Or specify tables like this:

sqlite-diffable dump my.db output/ entries tags

Exclude specific tables:

sqlite-diffable dump my.db output/ --all --exclude table1 --exclude table2
"""
if not tables and not all:
raise click.ClickException("You must pass --all or specify some tables")
output = pathlib.Path(output)
output.mkdir(exist_ok=True)
conn = sqlite_utils.Database(dbpath)
if all:
tables = conn.table_names()
tables = set(conn.table_names()) - set(exclude)
else:
tables = set(tables) - set(exclude)
for table in tables:
tablename = table.replace("/", "")
filepath = output / "{}.ndjson".format(tablename)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ def test_dump_all(two_tables_db, tmpdir):
assert (output_dir / "second_table.metadata.json").exists()


def test_dump_exclude(two_tables_db, tmpdir):
output_dir = tmpdir / "out"
result = CliRunner().invoke(
cli.cli, ["dump", two_tables_db, str(output_dir), "--all", "--exclude", "second_table"]
)
assert result.exit_code == 0, result.output
assert (output_dir / "one_table.ndjson").exists()
assert (output_dir / "one_table.metadata.json").exists()
assert not (output_dir / "second_table.ndjson").exists()
assert not (output_dir / "second_table.metadata.json").exists()


def test_load(two_tables_db, tmpdir):
output_dir = tmpdir / "out"
restore_db = tmpdir / "restore.db"
Expand Down