From edae77c782bb785f3d752e754c54a80dcf07eb68 Mon Sep 17 00:00:00 2001 From: jaehwang Date: Wed, 12 Nov 2025 09:30:56 +0900 Subject: [PATCH] =?UTF-8?q?=EC=8B=9C=EC=9E=91=ED=95=A0=20=EB=95=8C=20?= =?UTF-8?q?=ED=85=8C=EC=9D=B4=EB=B8=94=20=EC=83=9D=EC=84=B1=20lifespan=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api-server/app/main.py | 23 ++++++++++++ api-server/app/module/mysql_utils.py | 55 ++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/api-server/app/main.py b/api-server/app/main.py index 105dcfa..260291a 100644 --- a/api-server/app/main.py +++ b/api-server/app/main.py @@ -1,3 +1,4 @@ +from contextlib import asynccontextmanager from fastapi import FastAPI, status, Depends from fastapi.responses import JSONResponse from module.pydantic_models import * @@ -5,6 +6,28 @@ import module.mysql_utils as sql app = FastAPI() +async def startup(): + table_list = await sql.get_table_list() + print(table_list) + if 'user_table' not in table_list: + await sql.create_user_table() + print("user_table created") + else: + print("user_table already exist") + + if 'blog_table'not in table_list: + await sql.create_blog_table() + print("user_table created") + else: + print("user_table already exist") + +@asynccontextmanager +async def lifespan(app : FastAPI): + await startup() + yield + +app = FastAPI(lifespan=lifespan) + @app.post("/user/create", status_code=status.HTTP_201_CREATED) async def create_user(params: UserCreateForm): user_id = await sql.create_user(params.user_name, params.phone_number) diff --git a/api-server/app/module/mysql_utils.py b/api-server/app/module/mysql_utils.py index a298ca1..eabcf06 100644 --- a/api-server/app/module/mysql_utils.py +++ b/api-server/app/module/mysql_utils.py @@ -239,4 +239,59 @@ async def truncate_user_table(): ''' await cur.execute(query) await cnx.commit() + return + +async def get_table_list(): + async with await get_cnx() as cnx: + async with await cnx.cursor() as cur: + query = ''' + SHOW TABLES + ''' + await cur.execute(query) + table_list = [result[0] for result in await cur.fetchall()] + await cnx.commit() + return table_list + + +async def create_user_table(): + async with await get_cnx() as cnx: + async with await cnx.cursor() as cur: + query = ''' +-- 테이블 생성 SQL - user_table +CREATE TABLE user_table +( + `user_id` INT NOT NULL AUTO_INCREMENT, + `user_name` VARCHAR(20) NOT NULL, + `phone_number` VARCHAR(20) NULL, + `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (user_id) +); + +-- Unique Index 설정 SQL - user_table(user_name) +CREATE UNIQUE INDEX UQ_user_table_1 + ON user_table(user_name); + ''' + await cur.execute(query) + await cnx.commit() + return + +async def create_blog_table(): + async with await get_cnx() as cnx: + async with await cnx.cursor() as cur: + query = ''' +-- 테이블 생성 SQL - blog_table +CREATE TABLE blog_table +( + `blog_id` INT NOT NULL AUTO_INCREMENT, + `blog_title` VARCHAR(50) NOT NULL, + `blog_content` TEXT NULL, + `blog_owner` INT NOT NULL, + `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (blog_id) +); + ''' + await cur.execute(query) + await cnx.commit() return \ No newline at end of file