시작할 때 테이블 생성 lifespan 추가
parent
9196ed08b4
commit
edae77c782
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
Loading…
Reference in New Issue