Alembic 사용법 Alembic은 데이터베이스 변경 이력을 코드로 관리할 수 있게 해주는 툴입니다.DB 스키마(테이블 구조)가 바뀔 때마다,직접 SQL 쓰지 않고,Python 코드로 변경 내용을 버전 관리합니다.마치 Git이 코드 이력을 관리하듯, Alembic은 DB 구조 변경 이력을 관리합니다.User 테이블에 is_active 컬럼을 추가했을 때:Alembic으로 이렇게 처리:alembic revision --autogenerate -m "Add is_active to User"alembic upgrade head자동으로 변경사항을 감지해서 마이그레이션 파일 생성upgrade() / downgrade()로 쉽게 적용/되돌리기 가능alembic upgrade alembic upgrade 1234abcd5678alemb.. 2025. 4. 21. SQLAlchemy 관계 종류 1:1 (일대일)예: 유저(User)는 하나의 프로필(Profile)만 가짐# user.pyclass User(Base): __tablename__ = "users" id = Column(Integer, primary_key=True) username = Column(String) profile = relationship("Profile", back_populates="user", uselist=False)# profile.pyclass Profile(Base): __tablename__ = "profiles" id = Column(Integer, primary_key=True) bio = Column(String) user_id = Column(Integer,.. 2025. 4. 17. SQLAlchemy DB 처리 방식 1. SQLAlchemy + Pydantic 방식Pydantic과 함께 쓰면 API/DB 역할 분리가 명확함DB 데이터를 "객체"로 다룰 수 있음 (User, Post 등)relationship, ForeignKey로 테이블 간 관계 표현 가능# 1. Pydantic 모델 (입출력용)class UserCreate(BaseModel): username: str email: str# 2. SQLAlchemy ORM 모델 (DB 테이블 대응)class User(Base): __tablename__ = "users" id = Column(Integer, primary_key=True) username = Column(String) email = Column(String)# 3. .. 2025. 4. 17. Flask 프레임워크 thread WSGI Server (Gunicorn, uWSGI, mod_wsgi 또는 내장 werkzeug 벡자이크) 출처: etloveguitar.tistory/92 import randomimport threadingimport timefrom werkzeug.local import LocalStackthread_data_stack = LocalStack()def long_running_function(thread_index): thread_data_stack.push(f'index: {thread_index}, thread_id: {threading.get_native_id()}') print(f'Starting thread #{thread_index}... {thread_data_stack}') .. 2024. 4. 12. 파이썬 flask - redis - celery 구조 파이썬Flask webframeworkredis brokercelery async worker 구조로 사용할 것이다.title excel upload apiparticipant Userparticipant Web App (Flask)participant Redisparticipant Celery Workerparticipant DBUser->Web App (Flask):GET /excelUserWeb App (Flask):POST /excel/uploadWeb App (Flask)->Redis:Enqueue a new task to the brokerRedisDB:Update resultUser->Web App (Flask):Check the status of the taskUser 2024. 3. 14. 이전 1 다음