Shyamnath commited on
Commit
f0fe332
·
1 Parent(s): 0702b8b

Fix database paths to use relative paths correctly in Docker container

Browse files
Files changed (2) hide show
  1. app.py +4 -8
  2. app/database.py +6 -6
app.py CHANGED
@@ -22,17 +22,13 @@ def main():
22
 
23
  # Ensure database file is in the correct location for Docker
24
  source_db = "Tabble.db"
25
- target_db = "/app/Tabble.db"
26
 
27
  try:
28
- if os.path.exists(source_db) and not os.path.exists(target_db):
29
- import shutil
30
- shutil.copy2(source_db, target_db)
31
- print(f"✅ Database copied from {source_db} to {target_db}")
32
- elif os.path.exists(target_db):
33
- print(f"✅ Database found at {target_db}")
34
  else:
35
- print(f"⚠️ Database not found, will create new one at {target_db}")
36
  except Exception as e:
37
  print(f"⚠️ Database setup warning: {e}")
38
 
 
22
 
23
  # Ensure database file is in the correct location for Docker
24
  source_db = "Tabble.db"
25
+ target_db = "Tabble.db" # Same location in Docker
26
 
27
  try:
28
+ if os.path.exists(source_db):
29
+ print(f"✅ Database found at {source_db}")
 
 
 
 
30
  else:
31
+ print(f"⚠️ Database not found at {source_db}, will create new one")
32
  except Exception as e:
33
  print(f"⚠️ Database setup warning: {e}")
34
 
app/database.py CHANGED
@@ -49,7 +49,7 @@ class DatabaseManager:
49
 
50
  def _create_connection(self, hotel_id: Optional[int] = None) -> dict:
51
  """Create a new database connection to unified database"""
52
- database_url = f"sqlite:////app/Tabble.db"
53
  engine = create_engine(database_url, connect_args={"check_same_thread": False})
54
  session_factory = sessionmaker(autocommit=False, autoflush=False, bind=engine)
55
  session_local = scoped_session(session_factory)
@@ -126,7 +126,7 @@ db_manager = DatabaseManager()
126
 
127
  # Global variables for database connection (unified database)
128
  CURRENT_DATABASE = "Tabble.db"
129
- DATABASE_URL = f"sqlite:////app/Tabble.db" # Using the unified database
130
  engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
131
  session_factory = sessionmaker(autocommit=False, autoflush=False, bind=engine)
132
  SessionLocal = scoped_session(session_factory)
@@ -409,9 +409,9 @@ def get_current_database():
409
  # Create tables
410
  def create_tables():
411
  try:
412
- # Ensure database directory exists
413
  import os
414
- os.makedirs("/app", exist_ok=True)
415
 
416
  # Create all tables (only creates tables that don't exist)
417
  Base.metadata.create_all(bind=engine)
@@ -422,8 +422,8 @@ def create_tables():
422
  try:
423
  print("Attempting to create new database...")
424
  import sqlite3
425
- # Create the database file if it doesn't exist
426
- with sqlite3.connect("/app/Tabble.db") as conn:
427
  conn.execute("SELECT 1")
428
  # Try creating tables again
429
  Base.metadata.create_all(bind=engine)
 
49
 
50
  def _create_connection(self, hotel_id: Optional[int] = None) -> dict:
51
  """Create a new database connection to unified database"""
52
+ database_url = f"sqlite:///Tabble.db"
53
  engine = create_engine(database_url, connect_args={"check_same_thread": False})
54
  session_factory = sessionmaker(autocommit=False, autoflush=False, bind=engine)
55
  session_local = scoped_session(session_factory)
 
126
 
127
  # Global variables for database connection (unified database)
128
  CURRENT_DATABASE = "Tabble.db"
129
+ DATABASE_URL = f"sqlite:///Tabble.db" # Using the unified database
130
  engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
131
  session_factory = sessionmaker(autocommit=False, autoflush=False, bind=engine)
132
  SessionLocal = scoped_session(session_factory)
 
409
  # Create tables
410
  def create_tables():
411
  try:
412
+ # Ensure database directory exists (current working directory)
413
  import os
414
+ # We're already in /app in Docker, so just ensure file can be created
415
 
416
  # Create all tables (only creates tables that don't exist)
417
  Base.metadata.create_all(bind=engine)
 
422
  try:
423
  print("Attempting to create new database...")
424
  import sqlite3
425
+ # Create the database file if it doesn't exist (in current directory)
426
+ with sqlite3.connect("Tabble.db") as conn:
427
  conn.execute("SELECT 1")
428
  # Try creating tables again
429
  Base.metadata.create_all(bind=engine)