wip(22w8d): add sqlite abstraction

This commit is contained in:
Cloud Shell 2022-02-25 15:50:42 +00:00
parent 265d9ca6ed
commit 4a5ec18e05
7 changed files with 45 additions and 23 deletions

1
.gitignore vendored
View file

@ -14,6 +14,7 @@ npm-debug.log
yarn-error.log yarn-error.log
public/robots.txt public/robots.txt
__pycache__ __pycache__
*.pyc
# OS generated files # # OS generated files #
###################### ######################

View file

@ -1,4 +1,4 @@
from src import SQLite, StorageClient from src import Database, StorageClient
client = SQLite() client = Database()
print("OK") print("OK")

View file

@ -1,7 +1,7 @@
from dotenv import load_dotenv from dotenv import load_dotenv
from .glob import file_exists from .glob import file_exists
from .db import SQLite from .db import Database
from .gcs import StorageClient from .gcs import StorageClient
if not file_exists(".env"): if not file_exists(".env"):

View file

@ -1 +1 @@
from .sqlite import SQLite from .database import Database

View file

@ -1,14 +1,16 @@
CREATE TABLE flags ( CREATE TABLE flags (
k text PRIMARY KEY, k TEXT PRIMARY KEY,
v real v INTEGER
); );
CREATE TABLE fileindex ( CREATE TABLE manifest (
anchor text PRIMARY KEY, anchor TEXT PRIMARY KEY,
chksum text mtime INTEGER,
chksum TEXT
); );
INSERT INTO flags INSERT INTO flags
VALUES VALUES
("CALC_CHECKSUM", 1), ("CALC_CHECKSUM", 1),
("BUCKET_OK", 0); ("BUCKET_OK", 0),
("INIT", 1);

11
src/db/database.py Normal file
View file

@ -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

View file

@ -9,18 +9,32 @@ class SQLite():
self.db = sqlite.connect(self.get_db_path()) self.db = sqlite.connect(self.get_db_path())
self.cursor = self.db.cursor() 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): 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 False
return result return result
# Get path to database file
def get_db_path(self) -> str: def get_db_path(self) -> str:
name = ".gcsarchive.db" name = ".cloudbackup.db"
path = os.getenv("SOURCE_FOLDER") path = os.getenv("SOURCE_FOLDER")
# Append db file name if absent # Append db file name if absent
@ -31,17 +45,11 @@ class SQLite():
path += name path += name
return path return path
# Prepare a fresh db with the expected table structure
def configure_db(self): def configure_db(self):
cwd = str(pathlib.Path(__file__).parent.resolve()) cwd = str(pathlib.Path(__file__).parent.resolve())
sql = open(cwd + "/config.sql") sql = open(cwd + "/config.sql")
sql_str = sql.read() sql_str = SQLite.format_query(sql.read())
return self.cursor.executescript(sql_str) 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