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

exportdb: Implemented --dry-run on some options which change data. #1682

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
157 changes: 97 additions & 60 deletions osxphotos/cli/exportdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@
@click.option(
"--check-signatures",
is_flag=True,
help="Check signatures for all exported photos in the database to find signatures that don't match.",
help="Check signatures for all exported photos in the database to find signatures that don't match. "
"See also option --export-dir.",
)
@click.option(
"--update-signatures",
is_flag=True,
help="Update signatures for all exported photos in the database to match on-disk signatures.",
help="Update signatures for all exported photos in the database to match on-disk signatures. "
"See also option --export-dir.",
)
@click.option(
"--touch-file",
Expand Down Expand Up @@ -268,6 +270,7 @@ def exportdb(
info,
last_run,
last_export_dir,
migrate_photos_library,
upgrade,
repair,
report,
Expand Down Expand Up @@ -323,47 +326,63 @@ def exportdb(
)
sys.exit(1)

try:
ExportDB(export_db, export_dir, create)
except Exception as e:
rich_echo_error(f"[error]Error: {e}[/error]")
sys.exit(1)
if not dry_run:
try:
ExportDB(export_db, export_dir, create)
except Exception as e:
rich_echo_error(f"[error]Error: {e}[/error]")
sys.exit(1)
else:
rich_echo(f"Created export database [filepath]{export_db}[/]")
sys.exit(0)
else:
rich_echo(f"Created export database [filepath]{export_db}[/]")
rich_echo(f"[Dryrun] Created export database [filepath]{export_db}[/]")
sys.exit(0)

if check:
errors = sqlite_check_integrity(export_db)
if not errors:
rich_echo(f"Ok: [filepath]{export_db}[/]")
sys.exit(0)
if not dry_run:
errors = sqlite_check_integrity(export_db)
if not errors:
rich_echo(f"Ok: [filepath]{export_db}[/]")
sys.exit(0)
else:
rich_echo_error(f"[error]Errors: [filepath]{export_db}[/][/error]")
for error in errors:
rich_echo_error(error)
sys.exit(1)
else:
rich_echo_error(f"[error]Errors: [filepath]{export_db}[/][/error]")
for error in errors:
rich_echo_error(error)
sys.exit(1)
rich_echo(f"[Dryrun] Check [filepath]{export_db}[/]")
sys.exit(0)

if repair:
try:
sqlite_repair_db(export_db)
except Exception as e:
rich_echo_error(f"[error]Error: {e}[/error]")
sys.exit(1)
if not dry_run:
try:
sqlite_repair_db(export_db)
except Exception as e:
rich_echo_error(f"[error]Error: {e}[/error]")
sys.exit(1)
else:
rich_echo(f"Ok: [filepath]{export_db}[/]")
sys.exit(0)
else:
rich_echo(f"Ok: [filepath]{export_db}[/]")
rich_echo(f"[Dryrun] Repair [filepath]{export_db}[/]")
sys.exit(0)

if vacuum:
try:
start_size = pathlib.Path(export_db).stat().st_size
export_db_vacuum(export_db)
except Exception as e:
rich_echo_error(f"[error]Error: {e}[/error]")
sys.exit(1)
if not dry_run:
try:
start_size = pathlib.Path(export_db).stat().st_size
export_db_vacuum(export_db)
except Exception as e:
rich_echo_error(f"[error]Error: {e}[/error]")
sys.exit(1)
else:
rich_echo(
f"Vacuumed {export_db}! [num]{start_size}[/] bytes -> [num]{pathlib.Path(export_db).stat().st_size}[/] bytes"
)
sys.exit(0)
else:
rich_echo(
f"Vacuumed {export_db}! [num]{start_size}[/] bytes -> [num]{pathlib.Path(export_db).stat().st_size}[/] bytes"
)
rich_echo(f"[Dryrun] Vacuum [filepath]{export_db}[/]")
sys.exit(0)

if update_signatures:
Expand Down Expand Up @@ -417,13 +436,17 @@ def exportdb(
sys.exit(1)

if save_config:
try:
export_db_save_config_to_file(export_db, save_config)
except Exception as e:
rich_echo_error(f"[error]Error: {e}[/error]")
sys.exit(1)
if not dry_run:
try:
export_db_save_config_to_file(export_db, save_config)
except Exception as e:
rich_echo_error(f"[error]Error: {e}[/error]")
sys.exit(1)
else:
rich_echo(f"Saved configuration to [filepath]{save_config}")
sys.exit(0)
else:
rich_echo(f"Saved configuration to [filepath]{save_config}")
rich_echo(f"[Dryrun] Saved configuration to [filepath]{save_config}")
sys.exit(0)

if check_signatures:
Expand Down Expand Up @@ -561,21 +584,23 @@ def exportdb(
exportdb = ExportDB(export_db, export_dir)
for uuid in delete_uuid:
rich_echo(f"Deleting uuid [uuid]{uuid}[/] from database.")
count = exportdb.delete_data_for_uuid(uuid)
rich_echo(
f"Deleted [num]{count}[/] {pluralize(count, 'record', 'records')}."
)
if not dry_run:
count = exportdb.delete_data_for_uuid(uuid)
rich_echo(
f"Deleted [num]{count}[/] {pluralize(count, 'record', 'records')}."
)
sys.exit(0)

if delete_file:
# delete information associated with a file from the export database
exportdb = ExportDB(export_db, export_dir)
for filepath in delete_file:
rich_echo(f"Deleting file [filepath]{filepath}[/] from database.")
count = exportdb.delete_data_for_filepath(filepath)
rich_echo(
f"Deleted [num]{count}[/] {pluralize(count, 'record', 'records')}."
)
if not dry_run:
count = exportdb.delete_data_for_filepath(filepath)
rich_echo(
f"Deleted [num]{count}[/] {pluralize(count, 'record', 'records')}."
)
sys.exit(0)

if report:
Expand All @@ -599,28 +624,40 @@ def exportdb(
sys.exit(0)

if upgrade:
exportdb = ExportDB(export_db, export_dir)
if upgraded := exportdb.was_upgraded:
rich_echo(
f"Upgraded export database [filepath]{export_db}[/] from version [num]{upgraded[0]}[/] to [num]{upgraded[1]}[/]"
)
if not dry_run:
exportdb = ExportDB(export_db, export_dir)
if upgraded := exportdb.was_upgraded:
rich_echo(
f"Upgraded export database [filepath]{export_db}[/] from version [num]{upgraded[0]}[/] to [num]{upgraded[1]}[/]"
)
else:
rich_echo(
f"Export database [filepath]{export_db}[/] is already at latest version [num]{OSXPHOTOS_EXPORTDB_VERSION}[/]"
)
else:
# Does OSXPHOTOS_EXPORTDB_VERSION reflect the actual exportdb file version?
rich_echo(
f"Export database [filepath]{export_db}[/] is already at latest version [num]{OSXPHOTOS_EXPORTDB_VERSION}[/]"
f"[Dryrun] Upgrading database [filepath]{export_db}[/]"
)
sys.exit(0)

if sql:
exportdb = ExportDB(export_db, export_dir)
try:
c = exportdb._conn.cursor()
results = c.execute(sql)
except Exception as e:
rich_echo_error(f"[error]Error: {e}[/error]")
sys.exit(1)
if not dry_run:
exportdb = ExportDB(export_db, export_dir)
try:
c = exportdb._conn.cursor()
results = c.execute(sql)
except Exception as e:
rich_echo_error(f"[error]Error: {e}[/error]")
sys.exit(1)
else:
for row in results:
print(row)
sys.exit(0)
else:
for row in results:
print(row)
rich_echo(
f"[Dryrun] SQL_STATEMENT: [filepath]{sql}[/]"
)
sys.exit(0)

if migrate_photos_library:
Expand Down
Loading