add db path

This commit is contained in:
2025-09-17 10:39:58 +03:00
parent b87569bed6
commit 384c6f1c8c
3 changed files with 21 additions and 11 deletions

View File

@@ -1,12 +1,16 @@
import sqlite3 import sqlite3
import os
class DBApplications: class DBApplications:
def __init__(self): def __init__(self):
self.db_path = 'instance/dev.db' # Allow DB path override via env, default to /app/instance/dev.db inside container
default_path = os.getenv("DB_PATH", "instance/dev.db")
self.db_path = os.path.abspath(default_path)
os.makedirs(os.path.dirname(self.db_path), exist_ok=True)
self.create_table() self.create_table()
def create_table(self): def create_table(self):
with sqlite3.connect(self.db_path) as conn: with sqlite3.connect(self.db_path, check_same_thread=False) as conn:
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(""" cursor.execute("""
CREATE TABLE IF NOT EXISTS applications ( CREATE TABLE IF NOT EXISTS applications (
@@ -23,7 +27,7 @@ class DBApplications:
def insert_application(self, user_id, name, access_code: str) -> bool: def insert_application(self, user_id, name, access_code: str) -> bool:
try: try:
with sqlite3.connect(self.db_path) as conn: with sqlite3.connect(self.db_path, check_same_thread=False) as conn:
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(""" cursor.execute("""
INSERT INTO applications (user_id, name, access_code) INSERT INTO applications (user_id, name, access_code)
@@ -35,7 +39,7 @@ class DBApplications:
return False return False
def get_application_by_id(self, id): def get_application_by_id(self, id):
with sqlite3.connect(self.db_path) as conn: with sqlite3.connect(self.db_path, check_same_thread=False) as conn:
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(""" cursor.execute("""
SELECT * FROM applications SELECT * FROM applications
@@ -55,7 +59,7 @@ class DBApplications:
return None return None
def get_applications(self, user_id): def get_applications(self, user_id):
with sqlite3.connect(self.db_path) as conn: with sqlite3.connect(self.db_path, check_same_thread=False) as conn:
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(""" cursor.execute("""
SELECT * FROM applications SELECT * FROM applications
@@ -79,7 +83,7 @@ class DBApplications:
return all_rows return all_rows
def delete(self, id): def delete(self, id):
with sqlite3.connect(self.db_path) as conn: with sqlite3.connect(self.db_path, check_same_thread=False) as conn:
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(""" cursor.execute("""
DELETE FROM applications WHERE id = ? DELETE FROM applications WHERE id = ?

View File

@@ -1,12 +1,16 @@
import sqlite3 import sqlite3
import os
class Users: class Users:
def __init__(self): def __init__(self):
self.db_path = 'instance/dev.db' # Allow DB path override via env, default to /app/instance/dev.db inside container
default_path = os.getenv("DB_PATH", "instance/dev.db")
self.db_path = os.path.abspath(default_path)
os.makedirs(os.path.dirname(self.db_path), exist_ok=True)
self.create_table() self.create_table()
def create_table(self): def create_table(self):
with sqlite3.connect(self.db_path) as conn: with sqlite3.connect(self.db_path, check_same_thread=False) as conn:
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(""" cursor.execute("""
CREATE TABLE IF NOT EXISTS users ( CREATE TABLE IF NOT EXISTS users (
@@ -22,7 +26,7 @@ class Users:
def insert_user(self, email, password: str) -> bool: def insert_user(self, email, password: str) -> bool:
try: try:
with sqlite3.connect(self.db_path) as conn: with sqlite3.connect(self.db_path, check_same_thread=False) as conn:
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(""" cursor.execute("""
INSERT INTO users (email, password) INSERT INTO users (email, password)
@@ -34,7 +38,7 @@ class Users:
return False return False
def get_user_by_id(self, id): def get_user_by_id(self, id):
with sqlite3.connect(self.db_path) as conn: with sqlite3.connect(self.db_path, check_same_thread=False) as conn:
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(""" cursor.execute("""
SELECT * FROM users SELECT * FROM users
@@ -53,7 +57,7 @@ class Users:
return None return None
def get_user_by_email(self, email): def get_user_by_email(self, email):
with sqlite3.connect(self.db_path) as conn: with sqlite3.connect(self.db_path, check_same_thread=False) as conn:
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(""" cursor.execute("""
SELECT * FROM users SELECT * FROM users

View File

@@ -47,8 +47,10 @@ services:
API_BASE_URL: https://${SOLARDB_API_HOST} API_BASE_URL: https://${SOLARDB_API_HOST}
# If your entry file differs, uncomment and set it: # If your entry file differs, uncomment and set it:
# FLET_ENTRY: client/main.py # FLET_ENTRY: client/main.py
USERS_DB_PATH: /app/instance/dev.db
volumes: volumes:
- solardb_uploads:/app/uploads - solardb_uploads:/app/uploads
- solardb_ui_data:/app/instance
expose: expose:
- "5555" - "5555"
depends_on: depends_on: