diff --git a/.gitignore b/.gitignore index b204fa0..4f392c7 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ npm-debug.log yarn-error.log public/robots.txt __pycache__ +*.pyc # OS generated files # ###################### diff --git a/backup.py b/backup.py index dbbf772..14e5981 100644 --- a/backup.py +++ b/backup.py @@ -1,4 +1,4 @@ -from src import SQLite, StorageClient +from src import Database, StorageClient -client = SQLite() +client = Database() print("OK") \ No newline at end of file diff --git a/src/__init__.py b/src/__init__.py index f76f657..69526d3 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -1,7 +1,7 @@ from dotenv import load_dotenv from .glob import file_exists -from .db import SQLite +from .db import Database from .gcs import StorageClient if not file_exists(".env"): diff --git a/src/db/__init__.py b/src/db/__init__.py index cff90f1..bae9c71 100644 --- a/src/db/__init__.py +++ b/src/db/__init__.py @@ -1 +1 @@ -from .sqlite import SQLite \ No newline at end of file +from .database import Database \ No newline at end of file diff --git a/src/db/config.sql b/src/db/config.sql index 8b71825..6b96c13 100644 --- a/src/db/config.sql +++ b/src/db/config.sql @@ -1,14 +1,16 @@ CREATE TABLE flags ( - k text PRIMARY KEY, - v real + k TEXT PRIMARY KEY, + v INTEGER ); -CREATE TABLE fileindex ( - anchor text PRIMARY KEY, - chksum text +CREATE TABLE manifest ( + anchor TEXT PRIMARY KEY, + mtime INTEGER, + chksum TEXT ); INSERT INTO flags VALUES ("CALC_CHECKSUM", 1), - ("BUCKET_OK", 0); \ No newline at end of file + ("BUCKET_OK", 0), + ("INIT", 1); \ No newline at end of file diff --git a/src/db/database.py b/src/db/database.py new file mode 100644 index 0000000..03391a3 --- /dev/null +++ b/src/db/database.py @@ -0,0 +1,11 @@ +from .sqlite import SQLite + +class Database(SQLite): + def __init__(self): + super().__init__() + + def backup_candidate(self, anchor: str) -> bool: + sql = f"SELECT anchor, mtime, chksum FROM manifest WHERE anchor = '{anchor}'" + data = self.query(sql) + + return True \ No newline at end of file diff --git a/src/db/sqlite.py b/src/db/sqlite.py index 9e1cc7f..8ee5281 100644 --- a/src/db/sqlite.py +++ b/src/db/sqlite.py @@ -9,18 +9,32 @@ class SQLite(): self.db = sqlite.connect(self.get_db_path()) self.cursor = self.db.cursor() - self.init() + # Check if the database requires configuration + try: + db_exists = self.query("SELECT k FROM flags WHERE k = 'INIT'") + if not db_exists: + self.configure_db() + except sqlite.OperationalError: + self.configure_db() + + # Strip linebreaks from pretty-printed SQL + @staticmethod + def format_query(sql: str) -> str: + return " ".join([s.strip() for s in sql.splitlines()]) + # Run SQL query def query(self, sql: str): - result = self.cursor.execute(sql) + query = self.cursor.execute(sql) + result = query.fetchall() - if result.rowcount < 1: + if len(result) < 1: return False return result + # Get path to database file def get_db_path(self) -> str: - name = ".gcsarchive.db" + name = ".cloudbackup.db" path = os.getenv("SOURCE_FOLDER") # Append db file name if absent @@ -31,17 +45,11 @@ class SQLite(): path += name return path + # Prepare a fresh db with the expected table structure def configure_db(self): cwd = str(pathlib.Path(__file__).parent.resolve()) + sql = open(cwd + "/config.sql") - sql_str = sql.read() + sql_str = SQLite.format_query(sql.read()) return self.cursor.executescript(sql_str) - - def init(self): - # Set up db if it's fresh - hasmeta_sql = "SELECT name FROM sqlite_master WHERE type='table' AND name='flags'" - if not self.query(hasmeta_sql): - self.configure_db() - - return True \ No newline at end of file