simonw / db-to-sqlite

CLI tool for exporting tables or queries from any SQL database to a SQLite file
Apache License 2.0
368 stars 28 forks source link

Let --table be used multiple times to copy multiple tables #6

Closed simonw closed 5 years ago

simonw commented 5 years ago

Split from #3

simonw commented 5 years ago

Started sketching this out:

diff --git a/db_to_sqlite/cli.py b/db_to_sqlite/cli.py
index bea55b7..b718767 100644
--- a/db_to_sqlite/cli.py
+++ b/db_to_sqlite/cli.py
@@ -9,7 +9,7 @@ from sqlite_utils import Database
 @click.argument("path", type=click.Path(exists=False), required=True)
 @click.option("--connection", required=True, help="SQLAlchemy connection string")
 @click.option("--all", help="Detect and copy all tables", is_flag=True)
-@click.option("--table", help="Name of table to save the results (and copy)")
+@click.option("--table", help="Tables to copy, or specific table to save results to", multiple=True)
 @click.option("--skip", help="When using --all skip these tables", multiple=True)
 @click.option("--sql", help="Optional SQL query to run")
 @click.option("--pk", help="Optional column to use as a primary key")
@@ -23,6 +23,8 @@ def cli(path, connection, all, table, skip, sql, pk):
         raise click.ClickException("--all OR --table required")
     if skip and not all:
         raise click.ClickException("--skip can only be used with --all")
+    if sql and len(table) != 1:
+        raise click.ClickException("Provide a single --table when using --sql")
     db = Database(path)
     db_conn = create_engine(connection).connect()
     if all:

Then realized I re-use the table variable a few times, so i need to clean that up first.

simonw commented 5 years ago

I originally had this as an option for --all but that doesn't make sense, because you can already call db-to-sqlite like this:

db-to-sqlite blog.db \
    --connection="postgresql://localhost/myblog" \
    --table=blog_entry
simonw commented 5 years ago

When I do this I should add support for --progress to this mode of operation too (see #7). I should also add test coverage - tests currently only handle --all. And the tests can test --progress too.