Skip to content
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

Add table name pattern option #41

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ For PostgreSQL, use this:
--version Show the version and exit.
--all Detect and copy all tables
--table TEXT Specific tables to copy
--skip TEXT When using --all skip these tables
--table-name-pattern TEXT Table name pattern for tables to copy
--skip TEXT When using --all or --table-name-pattern skip these tables
--redact TEXT... (table, column) pairs to redact with ***
--sql TEXT Optional SQL query to run
--output TEXT Table in which to save --sql query results
Expand All @@ -67,7 +68,12 @@ You can also save the data from all of your tables, effectively creating a SQLit
db-to-sqlite "postgresql://localhost/myblog" blog.db \
--all

When running `--all` you can specify tables to skip using `--skip`:
Alternatively, you can specify a regex pattern via `--table-name-pattern` to match against table names. For example:

db-to-sqlite "postgresql://localhost/myblog" blog.db \
--table-name-pattern "tag_\w+"

When running `--all` or `--table-name-pattern` you can specify tables to skip using `--skip`:

db-to-sqlite "postgresql://localhost/myblog" blog.db \
--all \
Expand Down
23 changes: 18 additions & 5 deletions db_to_sqlite/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import itertools
import re

import click
from sqlalchemy import create_engine, inspect
Expand All @@ -11,7 +12,8 @@
@click.argument("path", type=click.Path(exists=False), required=True)
@click.option("--all", help="Detect and copy all tables", is_flag=True)
@click.option("--table", help="Specific tables to copy", multiple=True)
@click.option("--skip", help="When using --all skip these tables", multiple=True)
@click.option("--table-name-pattern", help="Table name pattern for tables to copy")
@click.option("--skip", help="When using --all or --table-name-pattern skip these tables", multiple=True)
@click.option(
"--redact",
help="(table, column) pairs to redact with ***",
Expand All @@ -34,6 +36,7 @@ def cli(
path,
all,
table,
table_name_pattern,
skip,
redact,
sql,
Expand All @@ -58,10 +61,14 @@ def cli(

More: https://docs.sqlalchemy.org/en/13/core/engines.html#database-urls
"""
if not all and not table and not sql:
raise click.ClickException("--all OR --table OR --sql required")
if skip and not all:
raise click.ClickException("--skip can only be used with --all")
if not all and not table and not table_name_pattern and not sql:
raise click.ClickException(
"--all OR --table OR --table-name-pattern OR --sql required"
)
if skip and not (all or table_name_pattern):
raise click.ClickException(
"--skip can only be used with --all OR --table-name-pattern"
)
redact_columns = {}
for table_name, column_name in redact:
redact_columns.setdefault(table_name, set()).add(column_name)
Expand All @@ -78,6 +85,12 @@ def cli(
tables = table
if all:
tables = inspector.get_table_names()
elif table_name_pattern:
tables = [
t
for t in inspector.get_table_names()
if re.match(table_name_pattern, t) is not None
]
if tables:
foreign_keys_to_add = []
for i, table in enumerate(tables):
Expand Down