시작할 때 테이블 생성 lifespan 추가

This commit is contained in:
jaehwang committed 2025-11-12 09:30:56 +09:00
1 parent 9196ed08b4
commit edae77c782
2 files changed
+78

No files matched your search

+55
View File
@@ -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