Fix for #1071, crash with --dry-run (#1072)

This commit is contained in:
Rhet Turnbull
2023-05-14 08:27:51 -07:00
committed by GitHub
parent 55d4be6892
commit 234db3fb54

View File

@@ -921,32 +921,35 @@ class ExportDBInMemory(ExportDB):
returns: connection to the database returns: connection to the database
""" """
if not os.path.isfile(dbfile): if not os.path.isfile(dbfile):
conn = self._get_db_connection() # database doesn't exist so create it in-memory
if not conn: src = self._get_db_connection()
if not src:
raise Exception("Error getting connection to in-memory database") 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_created = True
self.was_upgraded = () self.was_upgraded = ()
else: self.version = OSXPHOTOS_EXPORTDB_VERSION
conn = sqlite3.connect(dbfile, check_same_thread=SQLITE_CHECK_SAME_THREAD) return src
dbdump = self._dump_db(conn)
conn.close()
# Create a database in memory and import from the dump # database exists so copy it to memory
conn = sqlite3.connect( src = sqlite3.connect(dbfile, check_same_thread=SQLITE_CHECK_SAME_THREAD)
":memory:", check_same_thread=SQLITE_CHECK_SAME_THREAD
) # Create a database in memory by backing up the on-disk database
conn.cursor().executescript(dbdump.read()) dst = sqlite3.connect(":memory:", check_same_thread=SQLITE_CHECK_SAME_THREAD)
self.was_created = False with dst:
version_info = self._get_database_version(conn) src.backup(dst, pages=1)
if version_info[1] < OSXPHOTOS_EXPORTDB_VERSION: src.close()
self._create_or_migrate_db_tables(conn)
self.was_upgraded = (version_info[1], OSXPHOTOS_EXPORTDB_VERSION) self.was_created = False
else: version_info = self._get_database_version(dst)
self.was_upgraded = () 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 self.version = OSXPHOTOS_EXPORTDB_VERSION
return conn return dst
def _get_db_connection(self): def _get_db_connection(self):
"""return db connection to in memory database""" """return db connection to in memory database"""