Skip to content

Commit

Permalink
fixed create database
Browse files Browse the repository at this point in the history
  • Loading branch information
wuranxu committed Jun 24, 2022
1 parent 88113b0 commit 88e4a89
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
9 changes: 1 addition & 8 deletions app/crud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from datetime import datetime
from typing import Tuple, List

from sqlalchemy import select, update, create_engine
from sqlalchemy import select, update

from app.enums.OperationEnum import OperationType
from app.middleware.RedisManager import RedisHelper
Expand Down Expand Up @@ -488,13 +488,6 @@ async def create_table():
await conn.run_sync(Base.metadata.create_all)


def create_database():
uri = "/".join(Config.SQLALCHEMY_DATABASE_URI.split("/")[:-1])
engine = create_engine(uri)
with engine.connect() as conn:
conn.execute("CREATE DATABASE IF NOT EXISTS pity default character set utf8mb4 collate utf8mb4_unicode_ci")


# 设置项目角色映射关系
init_relation(ProjectRole, PityRelationField(ProjectRole.user_id, (User.id, User.name)),
PityRelationField(ProjectRole.project_id, (Project.id, Project.name)),
Expand Down
14 changes: 14 additions & 0 deletions app/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datetime import datetime
from typing import List

from sqlalchemy import create_engine
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.ext.declarative import declarative_base
Expand All @@ -10,6 +11,19 @@
from app.enums.DatabaseEnum import DatabaseEnum
from config import Config


def create_database():
engine = create_engine('mysql+mysqlconnector://{}:{}@{}:{}'.format(
Config.MYSQL_USER, Config.MYSQL_PWD, Config.MYSQL_HOST, Config.MYSQL_PORT), echo=True)
with engine.connect() as conn:
conn.execute("CREATE DATABASE IF NOT EXISTS pity default character set utf8mb4 collate utf8mb4_unicode_ci")
# close engine
engine.dispose()


# 优先建库
create_database()

# 同步engine
# engine = create_engine(Config.SQLALCHEMY_DATABASE_URI, pool_recycle=1500)
# 异步engine
Expand Down
3 changes: 1 addition & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from app import pity, init_logging
from app.core.msg.wss_msg import WebSocketMessage
from app.core.ws_connection_manager import ws_manage
from app.crud import create_table, create_database
from app.crud import create_table
from app.crud.notification.NotificationDao import PityNotificationDao
from app.enums.MessageEnum import MessageStateEnum, MessageTypeEnum
from app.middleware.RedisManager import RedisHelper
Expand Down Expand Up @@ -164,7 +164,6 @@ async def init_database():
:return:
"""
try:
create_database()
await asyncio.create_task(create_table())
logger.bind(name=None).success("database and tables created success. ✔")
except Exception as e:
Expand Down

0 comments on commit 88e4a89

Please sign in to comment.