File size: 1,169 Bytes
1ef9436
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@File    :   init_db.py
@Time    :   2024/09/06
@Project :   https://github.com/PeterH0323/Streamer-Sales
@Author  :   HinGwenWong
@Version :   1.0
@Desc    :   数据库初始化
"""

from loguru import logger
from pydantic import PostgresDsn
from pydantic_core import MultiHostUrl
from sqlmodel import SQLModel, create_engine

from ...web_configs import WEB_CONFIGS

ECHO_DB_MESG = True  # 数据库执行中是否回显,for debug


def sqlalchemy_db_url() -> PostgresDsn:
    """生成数据库 URL

    Returns:
        PostgresDsn: 数据库地址
    """
    return MultiHostUrl.build(
        scheme="postgresql+psycopg",
        username=WEB_CONFIGS.POSTGRES_USER,
        password=WEB_CONFIGS.POSTGRES_PASSWORD,
        host=WEB_CONFIGS.POSTGRES_SERVER,
        port=WEB_CONFIGS.POSTGRES_PORT,
        path=WEB_CONFIGS.POSTGRES_DB,
    )


logger.info(f"connecting to db: {str(sqlalchemy_db_url())}")
DB_ENGINE = create_engine(str(sqlalchemy_db_url()), echo=ECHO_DB_MESG)


def create_db_and_tables():
    """创建所有数据库和对应的表,有则跳过"""
    SQLModel.metadata.create_all(DB_ENGINE)