Skip to content

Added --no_data argument #57

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

Open
wants to merge 2 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
__pycache__
dist
build
venv
venv
.idea
63 changes: 38 additions & 25 deletions db_to_sqlite/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
)
@click.option("-p", "--progress", help="Show progress bar", is_flag=True)
@click.option("--postgres-schema", help="PostgreSQL schema to use")
@click.option("--no_data", help="When using --no_data only copy schema for these tables", multiple=True)

def cli(
connection,
path,
Expand All @@ -42,6 +44,7 @@ def cli(
index_fks,
progress,
postgres_schema,
no_data,
):
"""
Load data from any database into SQLite.
Expand Down Expand Up @@ -105,34 +108,44 @@ def cli(
)
count = None
table_quoted = db_conn.dialect.identifier_preparer.quote_identifier(table)
if progress:
count = db_conn.execute(
text("select count(*) from {}".format(table_quoted))
).fetchone()[0]
results = db_conn.execute(text("select * from {}".format(table_quoted)))
redact_these = redact_columns.get(table) or set()
rows = (redacted_dict(r, redact_these) for r in results)
# Make sure generator is not empty
try:
first = next(rows)
except StopIteration:
# This is an empty table - create an empty copy
if not db[table].exists():
create_columns = {}
for column in inspector.get_columns(table):
try:
column_type = column["type"].python_type
except NotImplementedError:
column_type = str
create_columns[column["name"]] = column_type
db[table].create(create_columns)
if table in no_data:
create_columns = {}
for column in inspector.get_columns(table):
try:
column_type = column["type"].python_type
except NotImplementedError:
column_type = str
create_columns[column["name"]] = column_type
db[table].create(create_columns,pks)
else:
rows = itertools.chain([first], rows)
if progress:
with click.progressbar(rows, length=count) as bar:
db[table].insert_all(bar, pk=pks, replace=True)
count = db_conn.execute(
text("select count(*) from {}".format(table_quoted))
).fetchone()[0]
results = db_conn.execute(text("select * from {}".format(table_quoted)))
redact_these = redact_columns.get(table) or set()
rows = (redacted_dict(r, redact_these) for r in results)
# Make sure generator is not empty
try:
first = next(rows)
except StopIteration:
# This is an empty table - create an empty copy
if not db[table].exists():
create_columns = {}
for column in inspector.get_columns(table):
try:
column_type = column["type"].python_type
except NotImplementedError:
column_type = str
create_columns[column["name"]] = column_type
db[table].create(create_columns)
else:
db[table].insert_all(rows, pk=pks, replace=True)
rows = itertools.chain([first], rows)
if progress:
with click.progressbar(rows, length=count) as bar:
db[table].insert_all(bar, pk=pks, replace=True)
else:
db[table].insert_all(rows, pk=pks, replace=True)
foreign_keys_to_add_final = []
for table, column, other_table, other_column in foreign_keys_to_add:
# Make sure both tables exist and are not skipped - they may not
Expand Down