mirror of
https://codeberg.org/vlw/cloud-backup.git
synced 2025-09-14 01:53:42 +02:00
wip(22w8d): add sqlite abstraction
This commit is contained in:
parent
265d9ca6ed
commit
4a5ec18e05
7 changed files with 45 additions and 23 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -14,6 +14,7 @@ npm-debug.log
|
|||
yarn-error.log
|
||||
public/robots.txt
|
||||
__pycache__
|
||||
*.pyc
|
||||
|
||||
# OS generated files #
|
||||
######################
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from src import SQLite, StorageClient
|
||||
from src import Database, StorageClient
|
||||
|
||||
client = SQLite()
|
||||
client = Database()
|
||||
print("OK")
|
|
@ -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"):
|
||||
|
|
|
@ -1 +1 @@
|
|||
from .sqlite import SQLite
|
||||
from .database import Database
|
|
@ -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);
|
||||
("BUCKET_OK", 0),
|
||||
("INIT", 1);
|
11
src/db/database.py
Normal file
11
src/db/database.py
Normal 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
|
|
@ -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
|
Loading…
Add table
Reference in a new issue