Fix database path configuration and SQLite settings
This commit is contained in:
parent
16cfcd783e
commit
4a891f1dad
@ -55,7 +55,7 @@ version_path_separator = os # Use os.pathsep. Default configuration used for ne
|
|||||||
# are written from script.py.mako
|
# are written from script.py.mako
|
||||||
# output_encoding = utf-8
|
# output_encoding = utf-8
|
||||||
|
|
||||||
sqlalchemy.url = sqlite:///./app/storage/db/db.sqlite
|
sqlalchemy.url = sqlite:///app/storage/db/db.sqlite
|
||||||
|
|
||||||
[post_write_hooks]
|
[post_write_hooks]
|
||||||
# post_write_hooks defines scripts or Python functions that are run
|
# post_write_hooks defines scripts or Python functions that are run
|
||||||
|
@ -7,7 +7,6 @@ from alembic import context
|
|||||||
|
|
||||||
from app.core.config import settings
|
from app.core.config import settings
|
||||||
from app.db.session import Base
|
from app.db.session import Base
|
||||||
from app.models import User, Conversation, Message, conversation_participants
|
|
||||||
|
|
||||||
# this is the Alembic Config object, which provides
|
# this is the Alembic Config object, which provides
|
||||||
# access to the values within the .ini file in use.
|
# access to the values within the .ini file in use.
|
||||||
@ -69,10 +68,20 @@ def run_migrations_online() -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
with connectable.connect() as connection:
|
with connectable.connect() as connection:
|
||||||
|
# For SQLite, we need to enable foreign keys and use batch mode for migrations
|
||||||
|
is_sqlite = connection.dialect.name == "sqlite"
|
||||||
|
|
||||||
context.configure(
|
context.configure(
|
||||||
connection=connection, target_metadata=target_metadata
|
connection=connection,
|
||||||
|
target_metadata=target_metadata,
|
||||||
|
# This is important for SQLite to support ALTER operations properly
|
||||||
|
render_as_batch=is_sqlite
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Enable foreign key constraints for SQLite
|
||||||
|
if is_sqlite:
|
||||||
|
connection.execute("PRAGMA foreign_keys=ON")
|
||||||
|
|
||||||
with context.begin_transaction():
|
with context.begin_transaction():
|
||||||
context.run_migrations()
|
context.run_migrations()
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from typing import Generator, Optional
|
|
||||||
|
|
||||||
from fastapi import Depends, HTTPException, status
|
from fastapi import Depends, HTTPException, status
|
||||||
from fastapi.security import OAuth2PasswordBearer
|
from fastapi.security import OAuth2PasswordBearer
|
||||||
@ -7,7 +6,6 @@ from pydantic import ValidationError
|
|||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from app.core.config import settings
|
from app.core.config import settings
|
||||||
from app.core.security import verify_password
|
|
||||||
from app.db.repositories.user import user_repository
|
from app.db.repositories.user import user_repository
|
||||||
from app.db.session import get_db
|
from app.db.session import get_db
|
||||||
from app.models.user import User
|
from app.models.user import User
|
||||||
|
@ -8,7 +8,7 @@ from app.db.repositories.message import message_repository
|
|||||||
from app.db.repositories.conversation import conversation_repository
|
from app.db.repositories.conversation import conversation_repository
|
||||||
from app.db.session import get_db
|
from app.db.session import get_db
|
||||||
from app.models.user import User
|
from app.models.user import User
|
||||||
from app.schemas.message import Message, MessageCreate, MessageUpdate
|
from app.schemas.message import Message, MessageCreate
|
||||||
|
|
||||||
router = APIRouter(tags=["messages"])
|
router = APIRouter(tags=["messages"])
|
||||||
|
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
import json
|
import json
|
||||||
from typing import Dict, List, Any
|
from typing import Dict, Any
|
||||||
import asyncio
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, WebSocket, WebSocketDisconnect, HTTPException, status
|
from fastapi import APIRouter, Depends, WebSocket, WebSocketDisconnect
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from jose import jwt, JWTError
|
from jose import jwt, JWTError
|
||||||
|
|
||||||
@ -231,7 +230,7 @@ async def websocket_endpoint(websocket: WebSocket, db: Session = Depends(get_db)
|
|||||||
# Remove user from connected users
|
# Remove user from connected users
|
||||||
if user and user.id in connected_users:
|
if user and user.id in connected_users:
|
||||||
del connected_users[user.id]
|
del connected_users[user.id]
|
||||||
except Exception as e:
|
except Exception:
|
||||||
# Handle any other exceptions
|
# Handle any other exceptions
|
||||||
if user and user.id in connected_users:
|
if user and user.id in connected_users:
|
||||||
del connected_users[user.id]
|
del connected_users[user.id]
|
@ -1,3 +1,4 @@
|
|||||||
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import secrets
|
import secrets
|
||||||
from typing import List
|
from typing import List
|
||||||
@ -16,7 +17,9 @@ class Settings(BaseSettings):
|
|||||||
CORS_ORIGINS: List[str] = ["*"]
|
CORS_ORIGINS: List[str] = ["*"]
|
||||||
|
|
||||||
# Database
|
# Database
|
||||||
DB_DIR = Path("/app/storage/db")
|
# Use current working directory as base by default, or app directory if in container
|
||||||
|
BASE_DIR = Path(os.getenv("APP_BASE_DIR", os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))))
|
||||||
|
DB_DIR = BASE_DIR / "app" / "storage" / "db"
|
||||||
DB_DIR.mkdir(parents=True, exist_ok=True)
|
DB_DIR.mkdir(parents=True, exist_ok=True)
|
||||||
SQLALCHEMY_DATABASE_URL: str = f"sqlite:///{DB_DIR}/db.sqlite"
|
SQLALCHEMY_DATABASE_URL: str = f"sqlite:///{DB_DIR}/db.sqlite"
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ class ConversationRepository(BaseRepository[Conversation, ConversationCreate, Co
|
|||||||
# Find direct conversations (not groups) where both users are participants
|
# Find direct conversations (not groups) where both users are participants
|
||||||
conversations = (
|
conversations = (
|
||||||
db.query(Conversation)
|
db.query(Conversation)
|
||||||
.filter(Conversation.is_group == False)
|
.filter(not Conversation.is_group)
|
||||||
.join(conversation_participants, Conversation.id == conversation_participants.c.conversation_id)
|
.join(conversation_participants, Conversation.id == conversation_participants.c.conversation_id)
|
||||||
.filter(conversation_participants.c.user_id.in_([user_id_1, user_id_2]))
|
.filter(conversation_participants.c.user_id.in_([user_id_1, user_id_2]))
|
||||||
.all()
|
.all()
|
||||||
|
@ -63,7 +63,7 @@ class MessageRepository(BaseRepository[Message, MessageCreate, MessageUpdate]):
|
|||||||
return (
|
return (
|
||||||
db.query(Message)
|
db.query(Message)
|
||||||
.filter(Message.recipient_id == user_id)
|
.filter(Message.recipient_id == user_id)
|
||||||
.filter(Message.is_read == False)
|
.filter(not Message.is_read)
|
||||||
.count()
|
.count()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
from app.models.user import User
|
from app.models.user import User # noqa: F401
|
||||||
from app.models.conversation import Conversation, conversation_participants
|
from app.models.conversation import Conversation, conversation_participants # noqa: F401
|
||||||
from app.models.message import Message
|
from app.models.message import Message # noqa: F401
|
@ -1,3 +1,3 @@
|
|||||||
from app.schemas.user import User, UserCreate, UserUpdate, UserInDB
|
from app.schemas.user import User, UserCreate, UserUpdate, UserInDB # noqa: F401
|
||||||
from app.schemas.message import Message, MessageCreate, MessageUpdate, MessageInDB
|
from app.schemas.message import Message, MessageCreate, MessageUpdate, MessageInDB # noqa: F401
|
||||||
from app.schemas.conversation import Conversation, ConversationCreate, ConversationUpdate, ConversationInDB
|
from app.schemas.conversation import Conversation, ConversationCreate, ConversationUpdate, ConversationInDB # noqa: F401
|
@ -1,7 +1,7 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Optional, List
|
from typing import Optional
|
||||||
|
|
||||||
from pydantic import BaseModel, EmailStr, Field
|
from pydantic import BaseModel, EmailStr
|
||||||
|
|
||||||
class UserBase(BaseModel):
|
class UserBase(BaseModel):
|
||||||
username: str
|
username: str
|
||||||
|
Loading…
x
Reference in New Issue
Block a user