-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: replace mongodb with sqllite * feat: update docker compose to drop mongo * chore: drop logs * chore: cleanup * fix: unit tests * fix: workflow * fix: workflow run
- Loading branch information
Showing
26 changed files
with
295 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -187,3 +187,4 @@ cython_debug/ | |
postgres_data | ||
.vscode | ||
ollama | ||
data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DATABASE_PATH = "data/database.db" |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .common import insert, QUERIES, update | ||
|
||
__all__ = ["insert", "QUERIES", "update"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import sqlite3 | ||
from typing import Any, Optional | ||
from api.backend.constants import DATABASE_PATH | ||
from api.backend.utils import format_json, format_sql_row_to_python | ||
from api.backend.database.schema import INIT_QUERY | ||
from api.backend.database.queries import JOB_INSERT_QUERY, DELETE_JOB_QUERY | ||
import logging | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
def connect(): | ||
connection = sqlite3.connect(DATABASE_PATH) | ||
connection.set_trace_callback(print) | ||
cursor = connection.cursor() | ||
return cursor | ||
|
||
|
||
def insert(query: str, values: tuple[Any, ...]): | ||
connection = sqlite3.connect(DATABASE_PATH) | ||
cursor = connection.cursor() | ||
copy = list(values) | ||
format_json(copy) | ||
|
||
try: | ||
_ = cursor.execute(query, copy) | ||
connection.commit() | ||
except sqlite3.Error as e: | ||
LOG.error(f"An error occurred: {e}") | ||
finally: | ||
cursor.close() | ||
connection.close() | ||
|
||
|
||
def query(query: str, values: Optional[tuple[Any, ...]] = None): | ||
connection = sqlite3.connect(DATABASE_PATH) | ||
connection.row_factory = sqlite3.Row | ||
cursor = connection.cursor() | ||
rows = [] | ||
try: | ||
if values: | ||
_ = cursor.execute(query, values) | ||
else: | ||
_ = cursor.execute(query) | ||
|
||
rows = cursor.fetchall() | ||
|
||
finally: | ||
cursor.close() | ||
connection.close() | ||
|
||
formatted_rows: list[dict[str, Any]] = [] | ||
|
||
for row in rows: | ||
row = dict(row) | ||
formatted_row = format_sql_row_to_python(row) | ||
formatted_rows.append(formatted_row) | ||
|
||
return formatted_rows | ||
|
||
|
||
def update(query: str, values: Optional[tuple[Any, ...]] = None): | ||
connection = sqlite3.connect(DATABASE_PATH) | ||
cursor = connection.cursor() | ||
|
||
copy = None | ||
|
||
if values: | ||
copy = list(values) | ||
format_json(copy) | ||
|
||
try: | ||
if copy: | ||
res = cursor.execute(query, copy) | ||
else: | ||
res = cursor.execute(query) | ||
connection.commit() | ||
return res.rowcount | ||
except sqlite3.Error as e: | ||
LOG.error(f"An error occurred: {e}") | ||
finally: | ||
cursor.close() | ||
connection.close() | ||
|
||
return 0 | ||
|
||
|
||
QUERIES = { | ||
"init": INIT_QUERY, | ||
"insert_job": JOB_INSERT_QUERY, | ||
"delete_job": DELETE_JOB_QUERY, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .queries import JOB_INSERT_QUERY, DELETE_JOB_QUERY | ||
|
||
__all__ = ["JOB_INSERT_QUERY", "DELETE_JOB_QUERY"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
JOB_INSERT_QUERY = """ | ||
INSERT INTO jobs | ||
(id, url, elements, user, time_created, result, status, chat, job_options) | ||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) | ||
""" | ||
|
||
DELETE_JOB_QUERY = """ | ||
DELETE FROM jobs WHERE id IN () | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .schema import INIT_QUERY | ||
|
||
__all__ = ["INIT_QUERY"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
INIT_QUERY = """ | ||
CREATE TABLE IF NOT EXISTS jobs ( | ||
id STRING PRIMARY KEY NOT NULL, | ||
url STRING NOT NULL, | ||
elements JSON NOT NULL, | ||
user STRING, | ||
time_created DATETIME NOT NULL, | ||
result JSON NOT NULL, | ||
status STRING NOT NULL, | ||
chat JSON, | ||
job_options JSON | ||
); | ||
CREATE TABLE IF NOT EXISTS users ( | ||
email STRING PRIMARY KEY NOT NULL, | ||
hashed_password STRING NOT NULL, | ||
full_name STRING, | ||
disabled BOOLEAN | ||
); | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from api.backend.database.common import connect, QUERIES | ||
import logging | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
def init_database(): | ||
cursor = connect() | ||
|
||
for query in QUERIES["init"].strip().split(";"): | ||
if query.strip(): | ||
LOG.info(f"Executing query: {query}") | ||
_ = cursor.execute(query) | ||
|
||
cursor.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.