Spaces:
Running
Running
Debug - database
Browse files- app/database.py +16 -19
app/database.py
CHANGED
@@ -10,12 +10,12 @@ load_dotenv()
|
|
10 |
logger = logging.getLogger(__name__)
|
11 |
|
12 |
# --- Database URL Configuration ---
|
13 |
-
# --- CHANGE THIS LINE: Use
|
14 |
-
|
15 |
-
DEFAULT_DB_PATH = "/code/app.db" # Store DB in the main workdir
|
16 |
|
17 |
raw_db_url = os.getenv("DATABASE_URL", f"sqlite+aiosqlite:///{DEFAULT_DB_PATH}")
|
18 |
|
|
|
19 |
final_database_url = raw_db_url
|
20 |
if raw_db_url.startswith("sqlite+aiosqlite"):
|
21 |
parsed_url = urlparse(raw_db_url)
|
@@ -28,7 +28,6 @@ if raw_db_url.startswith("sqlite+aiosqlite"):
|
|
28 |
else:
|
29 |
logger.info(f"Using non-SQLite async DB URL: {final_database_url}")
|
30 |
|
31 |
-
# --- Async Database Instance ---
|
32 |
database = Database(final_database_url)
|
33 |
metadata = MetaData()
|
34 |
users = Table(
|
@@ -47,25 +46,24 @@ engine = create_engine(sync_db_url)
|
|
47 |
# --- Directory and Table Creation Logic ---
|
48 |
db_file_path = ""
|
49 |
if sync_db_url.startswith("sqlite"):
|
|
|
50 |
path_part = sync_db_url.split("sqlite:///")[-1].split("?")[0]
|
51 |
-
|
52 |
-
db_file_path = os.path.abspath(path_part) # Should resolve to /code/app.db
|
53 |
|
54 |
if db_file_path:
|
55 |
-
# --- CHANGE THIS LINE: Check writability of the
|
56 |
-
db_dir = os.path.dirname(db_file_path) # Should be /
|
57 |
logger.info(f"Ensuring database directory exists: {db_dir}")
|
58 |
try:
|
59 |
-
#
|
60 |
-
# We mainly need to check if it's writable
|
61 |
if not os.path.exists(db_dir):
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
|
66 |
if not os.access(db_dir, os.W_OK):
|
67 |
-
# If /
|
68 |
-
logger.error(f"
|
69 |
else:
|
70 |
logger.info(f"Database directory {db_dir} appears writable.")
|
71 |
|
@@ -74,7 +72,7 @@ if db_file_path:
|
|
74 |
except Exception as e:
|
75 |
logger.error(f"Unexpected error checking directory {db_dir}: {e}")
|
76 |
|
77 |
-
#
|
78 |
try:
|
79 |
logger.info("Attempting to connect with sync engine to check/create table...")
|
80 |
with engine.connect() as connection:
|
@@ -87,11 +85,10 @@ try:
|
|
87 |
logger.info("Users table created (or creation attempted).")
|
88 |
|
89 |
except Exception as e:
|
90 |
-
#
|
91 |
logger.exception(f"CRITICAL: Failed to connect/create database tables using sync engine: {e}")
|
92 |
|
93 |
-
|
94 |
-
# --- Async connect/disconnect functions ---
|
95 |
async def connect_db():
|
96 |
try:
|
97 |
await database.connect()
|
|
|
10 |
logger = logging.getLogger(__name__)
|
11 |
|
12 |
# --- Database URL Configuration ---
|
13 |
+
# --- CHANGE THIS LINE: Use the /tmp directory ---
|
14 |
+
DEFAULT_DB_PATH = "/tmp/app.db" # Store DB in the temporary directory
|
|
|
15 |
|
16 |
raw_db_url = os.getenv("DATABASE_URL", f"sqlite+aiosqlite:///{DEFAULT_DB_PATH}")
|
17 |
|
18 |
+
# --- (Rest of the URL parsing and async Database setup remains the same) ---
|
19 |
final_database_url = raw_db_url
|
20 |
if raw_db_url.startswith("sqlite+aiosqlite"):
|
21 |
parsed_url = urlparse(raw_db_url)
|
|
|
28 |
else:
|
29 |
logger.info(f"Using non-SQLite async DB URL: {final_database_url}")
|
30 |
|
|
|
31 |
database = Database(final_database_url)
|
32 |
metadata = MetaData()
|
33 |
users = Table(
|
|
|
46 |
# --- Directory and Table Creation Logic ---
|
47 |
db_file_path = ""
|
48 |
if sync_db_url.startswith("sqlite"):
|
49 |
+
# Path should be absolute starting with /tmp/
|
50 |
path_part = sync_db_url.split("sqlite:///")[-1].split("?")[0]
|
51 |
+
db_file_path = path_part # Should be /tmp/app.db
|
|
|
52 |
|
53 |
if db_file_path:
|
54 |
+
# --- CHANGE THIS LINE: Check writability of the /tmp directory ---
|
55 |
+
db_dir = os.path.dirname(db_file_path) # Should be /tmp
|
56 |
logger.info(f"Ensuring database directory exists: {db_dir}")
|
57 |
try:
|
58 |
+
# /tmp should always exist, but check writability
|
|
|
59 |
if not os.path.exists(db_dir):
|
60 |
+
# This would be very strange, but log it.
|
61 |
+
logger.error(f"CRITICAL: Directory {db_dir} does not exist!")
|
62 |
+
# No need to create /tmp usually
|
63 |
|
64 |
if not os.access(db_dir, os.W_OK):
|
65 |
+
# If even /tmp isn't writable, something is very wrong with the environment
|
66 |
+
logger.error(f"CRITICAL: Directory {db_dir} is not writable! Cannot create database.")
|
67 |
else:
|
68 |
logger.info(f"Database directory {db_dir} appears writable.")
|
69 |
|
|
|
72 |
except Exception as e:
|
73 |
logger.error(f"Unexpected error checking directory {db_dir}: {e}")
|
74 |
|
75 |
+
# --- (Rest of the table creation logic and async functions remain the same) ---
|
76 |
try:
|
77 |
logger.info("Attempting to connect with sync engine to check/create table...")
|
78 |
with engine.connect() as connection:
|
|
|
85 |
logger.info("Users table created (or creation attempted).")
|
86 |
|
87 |
except Exception as e:
|
88 |
+
# This *should* finally succeed if /tmp is writable
|
89 |
logger.exception(f"CRITICAL: Failed to connect/create database tables using sync engine: {e}")
|
90 |
|
91 |
+
# Async connect/disconnect functions
|
|
|
92 |
async def connect_db():
|
93 |
try:
|
94 |
await database.connect()
|