Refactored implementation for #731

This commit is contained in:
Rhet Turnbull
2022-07-23 15:33:31 -07:00
parent bd33b61882
commit 855d417e81

View File

@@ -166,23 +166,22 @@ class ReportWriterSQLite(ReportWriterABC):
with suppress(FileNotFoundError): with suppress(FileNotFoundError):
os.unlink(self.output_file) os.unlink(self.output_file)
self.export_id = datetime.datetime.now().isoformat()
self._conn = sqlite3.connect(self.output_file) self._conn = sqlite3.connect(self.output_file)
self._create_tables() self._create_tables()
self.report_id = self._generate_report_id()
def write(self, export_results: ExportResults): def write(self, export_results: ExportResults):
"""Write results to the output file""" """Write results to the output file"""
all_results = prepare_results_for_writing(export_results) all_results = prepare_results_for_writing(export_results)
for data in list(all_results.values()): for data in list(all_results.values()):
data["export_id"] = self.export_id data["report_id"] = self.report_id
cursor = self._conn.cursor() cursor = self._conn.cursor()
cursor.execute( cursor.execute(
"INSERT INTO report " "INSERT INTO report "
"(datetime, filename, exported, new, updated, skipped, exif_updated, touched, converted_to_jpeg, sidecar_xmp, sidecar_json, sidecar_exiftool, missing, error, exiftool_warning, exiftool_error, extended_attributes_written, extended_attributes_skipped, cleanup_deleted_file, cleanup_deleted_directory, exported_album, export_id) " "(datetime, filename, exported, new, updated, skipped, exif_updated, touched, converted_to_jpeg, sidecar_xmp, sidecar_json, sidecar_exiftool, missing, error, exiftool_warning, exiftool_error, extended_attributes_written, extended_attributes_skipped, cleanup_deleted_file, cleanup_deleted_directory, exported_album, report_id) "
"VALUES " "VALUES "
"(:datetime, :filename, :exported, :new, :updated, :skipped, :exif_updated, :touched, :converted_to_jpeg, :sidecar_xmp, :sidecar_json, :sidecar_exiftool, :missing, :error, :exiftool_warning, :exiftool_error, :extended_attributes_written, :extended_attributes_skipped, :cleanup_deleted_file, :cleanup_deleted_directory, :exported_album, :export_id);", "(:datetime, :filename, :exported, :new, :updated, :skipped, :exif_updated, :touched, :converted_to_jpeg, :sidecar_xmp, :sidecar_json, :sidecar_exiftool, :missing, :error, :exiftool_warning, :exiftool_error, :extended_attributes_written, :extended_attributes_skipped, :cleanup_deleted_file, :cleanup_deleted_directory, :exported_album, :report_id);",
data, data,
) )
self._conn.commit() self._conn.commit()
@@ -196,27 +195,27 @@ class ReportWriterSQLite(ReportWriterABC):
c.execute( c.execute(
""" """
CREATE TABLE IF NOT EXISTS report ( CREATE TABLE IF NOT EXISTS report (
datetime text, datetime TEXT,
filename text, filename TEXT,
exported integer, exported INTEGER,
new integer, new INTEGER,
updated integer, updated INTEGER,
skipped integer, skipped INTEGER,
exif_updated integer, exif_updated INTEGER,
touched integer, touched INTEGER,
converted_to_jpeg integer, converted_to_jpeg INTEGER,
sidecar_xmp integer, sidecar_xmp INTEGER,
sidecar_json integer, sidecar_json INTEGER,
sidecar_exiftool integer, sidecar_exiftool INTEGER,
missing integer, missing INTEGER,
error text, error TEXT,
exiftool_warning text, exiftool_warning TEXT,
exiftool_error text, exiftool_error TEXT,
extended_attributes_written integer, extended_attributes_written INTEGER,
extended_attributes_skipped integer, extended_attributes_skipped INTEGER,
cleanup_deleted_file integer, cleanup_deleted_file INTEGER,
cleanup_deleted_directory integer, cleanup_deleted_directory INTEGER,
exported_album text exported_album TEXT
) )
""" """
) )
@@ -231,14 +230,32 @@ class ReportWriterSQLite(ReportWriterABC):
"INSERT INTO about(about) VALUES (?);", "INSERT INTO about(about) VALUES (?);",
(f"OSXPhotos Export Report. {OSXPHOTOS_ABOUT_STRING}",), (f"OSXPhotos Export Report. {OSXPHOTOS_ABOUT_STRING}",),
) )
c.execute(
"""
CREATE TABLE IF NOT EXISTS report_id (
report_id INTEGER PRIMARY KEY,
datetime TEXT
);"""
)
self._conn.commit() self._conn.commit()
# migrate report table to add export_id if needed (#731) # migrate report table to add report_id if needed (#731)
if "export_id" not in sqlite_columns(self._conn, "report"): if "report_id" not in sqlite_columns(self._conn, "report"):
self._conn.cursor().execute("ALTER TABLE report ADD COLUMN export_id text;") self._conn.cursor().execute("ALTER TABLE report ADD COLUMN report_id TEXT;")
self._conn.commit() self._conn.commit()
def _generate_report_id(self) -> int:
"""Get a new report ID for this report"""
c = self._conn.cursor()
c.execute(
"INSERT INTO report_id(datetime) VALUES (?);",
(datetime.datetime.now().isoformat(),),
)
report_id = c.lastrowid
self._conn.commit()
return report_id
def __del__(self): def __del__(self):
with suppress(Exception): with suppress(Exception):
self.close() self.close()