Skip to content

Commit e3276e2

Browse files
committed
exportdb: Implemented --dry-run on some options which change data: create, check, repair, vacuum, save-config, delete-uuid, delete-file, upgrade, sql
Used "[Dryrun]" prefix in some output messages when using --dry-run. Don't know if it is aligned with the message output philosophy. ;) migrate_photos_library added to sub_commands (to run only one of sub_commands)
1 parent 50e22ea commit e3276e2

File tree

1 file changed

+97
-60
lines changed

1 file changed

+97
-60
lines changed

osxphotos/cli/exportdb.py

+97-60
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,14 @@
5959
@click.option(
6060
"--check-signatures",
6161
is_flag=True,
62-
help="Check signatures for all exported photos in the database to find signatures that don't match.",
62+
help="Check signatures for all exported photos in the database to find signatures that don't match. "
63+
"See also option --export-dir.",
6364
)
6465
@click.option(
6566
"--update-signatures",
6667
is_flag=True,
67-
help="Update signatures for all exported photos in the database to match on-disk signatures.",
68+
help="Update signatures for all exported photos in the database to match on-disk signatures. "
69+
"See also option --export-dir.",
6870
)
6971
@click.option(
7072
"--touch-file",
@@ -268,6 +270,7 @@ def exportdb(
268270
info,
269271
last_run,
270272
last_export_dir,
273+
migrate_photos_library,
271274
upgrade,
272275
repair,
273276
report,
@@ -323,47 +326,63 @@ def exportdb(
323326
)
324327
sys.exit(1)
325328

326-
try:
327-
ExportDB(export_db, export_dir, create)
328-
except Exception as e:
329-
rich_echo_error(f"[error]Error: {e}[/error]")
330-
sys.exit(1)
329+
if not dry_run:
330+
try:
331+
ExportDB(export_db, export_dir, create)
332+
except Exception as e:
333+
rich_echo_error(f"[error]Error: {e}[/error]")
334+
sys.exit(1)
335+
else:
336+
rich_echo(f"Created export database [filepath]{export_db}[/]")
337+
sys.exit(0)
331338
else:
332-
rich_echo(f"Created export database [filepath]{export_db}[/]")
339+
rich_echo(f"[Dryrun] Created export database [filepath]{export_db}[/]")
333340
sys.exit(0)
334341

335342
if check:
336-
errors = sqlite_check_integrity(export_db)
337-
if not errors:
338-
rich_echo(f"Ok: [filepath]{export_db}[/]")
339-
sys.exit(0)
343+
if not dry_run:
344+
errors = sqlite_check_integrity(export_db)
345+
if not errors:
346+
rich_echo(f"Ok: [filepath]{export_db}[/]")
347+
sys.exit(0)
348+
else:
349+
rich_echo_error(f"[error]Errors: [filepath]{export_db}[/][/error]")
350+
for error in errors:
351+
rich_echo_error(error)
352+
sys.exit(1)
340353
else:
341-
rich_echo_error(f"[error]Errors: [filepath]{export_db}[/][/error]")
342-
for error in errors:
343-
rich_echo_error(error)
344-
sys.exit(1)
354+
rich_echo(f"[Dryrun] Check [filepath]{export_db}[/]")
355+
sys.exit(0)
345356

346357
if repair:
347-
try:
348-
sqlite_repair_db(export_db)
349-
except Exception as e:
350-
rich_echo_error(f"[error]Error: {e}[/error]")
351-
sys.exit(1)
358+
if not dry_run:
359+
try:
360+
sqlite_repair_db(export_db)
361+
except Exception as e:
362+
rich_echo_error(f"[error]Error: {e}[/error]")
363+
sys.exit(1)
364+
else:
365+
rich_echo(f"Ok: [filepath]{export_db}[/]")
366+
sys.exit(0)
352367
else:
353-
rich_echo(f"Ok: [filepath]{export_db}[/]")
368+
rich_echo(f"[Dryrun] Repair [filepath]{export_db}[/]")
354369
sys.exit(0)
355370

356371
if vacuum:
357-
try:
358-
start_size = pathlib.Path(export_db).stat().st_size
359-
export_db_vacuum(export_db)
360-
except Exception as e:
361-
rich_echo_error(f"[error]Error: {e}[/error]")
362-
sys.exit(1)
372+
if not dry_run:
373+
try:
374+
start_size = pathlib.Path(export_db).stat().st_size
375+
export_db_vacuum(export_db)
376+
except Exception as e:
377+
rich_echo_error(f"[error]Error: {e}[/error]")
378+
sys.exit(1)
379+
else:
380+
rich_echo(
381+
f"Vacuumed {export_db}! [num]{start_size}[/] bytes -> [num]{pathlib.Path(export_db).stat().st_size}[/] bytes"
382+
)
383+
sys.exit(0)
363384
else:
364-
rich_echo(
365-
f"Vacuumed {export_db}! [num]{start_size}[/] bytes -> [num]{pathlib.Path(export_db).stat().st_size}[/] bytes"
366-
)
385+
rich_echo(f"[Dryrun] Vacuum [filepath]{export_db}[/]")
367386
sys.exit(0)
368387

369388
if update_signatures:
@@ -417,13 +436,17 @@ def exportdb(
417436
sys.exit(1)
418437

419438
if save_config:
420-
try:
421-
export_db_save_config_to_file(export_db, save_config)
422-
except Exception as e:
423-
rich_echo_error(f"[error]Error: {e}[/error]")
424-
sys.exit(1)
439+
if not dry_run:
440+
try:
441+
export_db_save_config_to_file(export_db, save_config)
442+
except Exception as e:
443+
rich_echo_error(f"[error]Error: {e}[/error]")
444+
sys.exit(1)
445+
else:
446+
rich_echo(f"Saved configuration to [filepath]{save_config}")
447+
sys.exit(0)
425448
else:
426-
rich_echo(f"Saved configuration to [filepath]{save_config}")
449+
rich_echo(f"[Dryrun] Saved configuration to [filepath]{save_config}")
427450
sys.exit(0)
428451

429452
if check_signatures:
@@ -561,21 +584,23 @@ def exportdb(
561584
exportdb = ExportDB(export_db, export_dir)
562585
for uuid in delete_uuid:
563586
rich_echo(f"Deleting uuid [uuid]{uuid}[/] from database.")
564-
count = exportdb.delete_data_for_uuid(uuid)
565-
rich_echo(
566-
f"Deleted [num]{count}[/] {pluralize(count, 'record', 'records')}."
567-
)
587+
if not dry_run:
588+
count = exportdb.delete_data_for_uuid(uuid)
589+
rich_echo(
590+
f"Deleted [num]{count}[/] {pluralize(count, 'record', 'records')}."
591+
)
568592
sys.exit(0)
569593

570594
if delete_file:
571595
# delete information associated with a file from the export database
572596
exportdb = ExportDB(export_db, export_dir)
573597
for filepath in delete_file:
574598
rich_echo(f"Deleting file [filepath]{filepath}[/] from database.")
575-
count = exportdb.delete_data_for_filepath(filepath)
576-
rich_echo(
577-
f"Deleted [num]{count}[/] {pluralize(count, 'record', 'records')}."
578-
)
599+
if not dry_run:
600+
count = exportdb.delete_data_for_filepath(filepath)
601+
rich_echo(
602+
f"Deleted [num]{count}[/] {pluralize(count, 'record', 'records')}."
603+
)
579604
sys.exit(0)
580605

581606
if report:
@@ -599,28 +624,40 @@ def exportdb(
599624
sys.exit(0)
600625

601626
if upgrade:
602-
exportdb = ExportDB(export_db, export_dir)
603-
if upgraded := exportdb.was_upgraded:
604-
rich_echo(
605-
f"Upgraded export database [filepath]{export_db}[/] from version [num]{upgraded[0]}[/] to [num]{upgraded[1]}[/]"
606-
)
627+
if not dry_run:
628+
exportdb = ExportDB(export_db, export_dir)
629+
if upgraded := exportdb.was_upgraded:
630+
rich_echo(
631+
f"Upgraded export database [filepath]{export_db}[/] from version [num]{upgraded[0]}[/] to [num]{upgraded[1]}[/]"
632+
)
633+
else:
634+
rich_echo(
635+
f"Export database [filepath]{export_db}[/] is already at latest version [num]{OSXPHOTOS_EXPORTDB_VERSION}[/]"
636+
)
607637
else:
638+
# Does OSXPHOTOS_EXPORTDB_VERSION reflect the actual exportdb file version?
608639
rich_echo(
609-
f"Export database [filepath]{export_db}[/] is already at latest version [num]{OSXPHOTOS_EXPORTDB_VERSION}[/]"
640+
f"[Dryrun] Upgrading database [filepath]{export_db}[/]"
610641
)
611642
sys.exit(0)
612643

613644
if sql:
614-
exportdb = ExportDB(export_db, export_dir)
615-
try:
616-
c = exportdb._conn.cursor()
617-
results = c.execute(sql)
618-
except Exception as e:
619-
rich_echo_error(f"[error]Error: {e}[/error]")
620-
sys.exit(1)
645+
if not dry_run:
646+
exportdb = ExportDB(export_db, export_dir)
647+
try:
648+
c = exportdb._conn.cursor()
649+
results = c.execute(sql)
650+
except Exception as e:
651+
rich_echo_error(f"[error]Error: {e}[/error]")
652+
sys.exit(1)
653+
else:
654+
for row in results:
655+
print(row)
656+
sys.exit(0)
621657
else:
622-
for row in results:
623-
print(row)
658+
rich_echo(
659+
f"[Dryrun] SQL_STATEMENT: [filepath]{sql}[/]"
660+
)
624661
sys.exit(0)
625662

626663
if migrate_photos_library:

0 commit comments

Comments
 (0)