2024-01-14 14:06:31 +00:00
|
|
|
from typing import List
|
|
|
|
|
2023-07-29 07:35:40 +00:00
|
|
|
from tortoise import Tortoise, run_async
|
2023-07-24 12:59:43 +00:00
|
|
|
|
|
|
|
from config.db_config import *
|
|
|
|
from tools import utils
|
|
|
|
|
|
|
|
|
2024-01-14 14:06:31 +00:00
|
|
|
def get_platform_models() -> List[str]:
|
|
|
|
models = ["store.xhs", "store.douyin", "store.bilibili", "store.kuaishou", "store.weibo"]
|
|
|
|
return models
|
|
|
|
|
|
|
|
|
2023-07-24 12:59:43 +00:00
|
|
|
async def init_db(create_db: bool = False) -> None:
|
|
|
|
await Tortoise.init(
|
|
|
|
db_url=RELATION_DB_URL,
|
2024-01-14 14:06:31 +00:00
|
|
|
modules={'models': get_platform_models()},
|
2023-07-24 12:59:43 +00:00
|
|
|
_create_db=create_db
|
|
|
|
)
|
|
|
|
|
2024-02-21 16:11:41 +00:00
|
|
|
async def close() -> None:
|
|
|
|
await Tortoise.close_connections()
|
2023-07-24 12:59:43 +00:00
|
|
|
|
|
|
|
async def init():
|
|
|
|
await init_db(create_db=True)
|
|
|
|
await Tortoise.generate_schemas()
|
2023-12-22 17:04:08 +00:00
|
|
|
utils.logger.info("[db.init] Init DB Success!")
|
2023-07-24 12:59:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
run_async(init())
|