Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
explicit delete of .sqlite.previous on --flush-index #199
Browse files Browse the repository at this point in the history
  • Loading branch information
gilesknap committed Feb 17, 2020
1 parent 9cd6242 commit 1603dbc
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions gphotos/LocalData.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
#!/usr/bin/env python3
# coding: utf8
from pathlib import Path
import logging
import platform
import sqlite3 as lite
from sqlite3.dbapi2 import Connection, Cursor
from datetime import datetime
from os import unlink
from pathlib import Path
from sqlite3.dbapi2 import Connection, Cursor
from typing import Iterator, Type

# todo this module could be tidied quite a bit
# too much application logic at this level in some cases
# also the generic functions seem a bit ugly and could do with rework
import gphotos.Queries as Queries
from gphotos import Utils
from gphotos.DatabaseMedia import DatabaseMedia
from gphotos.DbRow import DbRow
from gphotos.GoogleAlbumsRow import GoogleAlbumsRow
from gphotos.LocalFilesRow import LocalFilesRow
from gphotos.GooglePhotosRow import GooglePhotosRow
from gphotos.DbRow import DbRow
from gphotos.DatabaseMedia import DatabaseMedia

import logging
from gphotos.LocalFilesRow import LocalFilesRow

log = logging.getLogger(__name__)

Expand All @@ -44,7 +42,7 @@ def __init__(self, root_folder: Path, flush_index: bool = False):
clean_db = True
elif flush_index:
clean_db = True
self.db_file.rename(self.db_file.parent / (self.db_file.name + ".previous"))
self.backup_sql_file()

self.con: Connection = lite.connect(str(self.db_file), check_same_thread=False)
self.con.row_factory = lite.Row
Expand All @@ -59,6 +57,12 @@ def __init__(self, root_folder: Path, flush_index: bool = False):
def __enter__(self):
return self

def backup_sql_file(self):
backup = self.db_file.parent / (self.db_file.name + ".previous")
if backup.exists():
backup.unlink()
self.db_file.rename(backup)

def __exit__(self, exc_type, exc_val, exc_tb):
""" Always clean up and close the connection when this object is
destroyed. """
Expand All @@ -84,7 +88,7 @@ def check_schema_version(self):
)
self.con.commit()
self.con.close()
self.db_file.rename(self.db_file.parent / (self.db_file.name + ".previous"))
self.backup_sql_file()
self.con = lite.connect(str(self.db_file))
self.con.row_factory = lite.Row
self.cur = self.con.cursor()
Expand Down

0 comments on commit 1603dbc

Please sign in to comment.