Skip to content

Commit

Permalink
Fix for #1071, crash with --dry-run
Browse files Browse the repository at this point in the history
  • Loading branch information
RhetTbull committed May 14, 2023
1 parent 55d4be6 commit 5d25a9e
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions osxphotos/export_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,32 +921,35 @@ def _open_export_db(self, dbfile: str): # sourcery skip: raise-specific-error
returns: connection to the database
"""
if not os.path.isfile(dbfile):
conn = self._get_db_connection()
if not conn:
# database doesn't exist so create it in-memory
src = self._get_db_connection()
if not src:
raise Exception("Error getting connection to in-memory database")
self._create_or_migrate_db_tables(conn)
self._create_or_migrate_db_tables(src)
self.was_created = True
self.was_upgraded = ()
else:
conn = sqlite3.connect(dbfile, check_same_thread=SQLITE_CHECK_SAME_THREAD)
dbdump = self._dump_db(conn)
conn.close()
self.version = OSXPHOTOS_EXPORTDB_VERSION
return src

# Create a database in memory and import from the dump
conn = sqlite3.connect(
":memory:", check_same_thread=SQLITE_CHECK_SAME_THREAD
)
conn.cursor().executescript(dbdump.read())
self.was_created = False
version_info = self._get_database_version(conn)
if version_info[1] < OSXPHOTOS_EXPORTDB_VERSION:
self._create_or_migrate_db_tables(conn)
self.was_upgraded = (version_info[1], OSXPHOTOS_EXPORTDB_VERSION)
else:
self.was_upgraded = ()
# database exists so copy it to memory
src = sqlite3.connect(dbfile, check_same_thread=SQLITE_CHECK_SAME_THREAD)

# Create a database in memory by backing up the on-disk database
dst = sqlite3.connect(":memory:", check_same_thread=SQLITE_CHECK_SAME_THREAD)
with dst:
src.backup(dst, pages=1)
src.close()

self.was_created = False
version_info = self._get_database_version(dst)
if version_info[1] < OSXPHOTOS_EXPORTDB_VERSION:
self._create_or_migrate_db_tables(dst)
self.was_upgraded = (version_info[1], OSXPHOTOS_EXPORTDB_VERSION)
else:
self.was_upgraded = ()
self.version = OSXPHOTOS_EXPORTDB_VERSION

return conn
return dst

def _get_db_connection(self):
"""return db connection to in memory database"""
Expand Down

0 comments on commit 5d25a9e

Please sign in to comment.