init_sqlalchemy.py 3.08 KB
Newer Older
Weizhi Cui's avatar
Weizhi Cui committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
# @Version     : Python 3.11.4
# @Software    : Sublime Text 4
# @Author      : StudentCWZ
# @Email       : StudentCWZ@outlook.com
# @Date        : 2023/10/28 22:01
# @File        : init_sqlalchemy.py
# @Description :
"""

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
崔为之's avatar
崔为之 committed
15
from sqlalchemy import text
Weizhi Cui's avatar
Weizhi Cui committed
16

崔为之's avatar
崔为之 committed
17
from application.utils import DatabaseUri
Weizhi Cui's avatar
Weizhi Cui committed
18 19 20 21 22 23 24 25 26 27 28

db = SQLAlchemy()


def init_database(app: Flask) -> None:
    """
    Initialize the database extension

    :param app: flask.Flask application instance
    :return: None
    """
崔为之's avatar
崔为之 committed
29
    cfg = app.config.Database
崔为之's avatar
崔为之 committed
30
    uri = DatabaseUri(
崔为之's avatar
崔为之 committed
31 32 33 34 35 36 37 38 39
        db_type=cfg.Type,
        username=cfg.Username,
        password=cfg.Password,
        host=cfg.Host,
        port=cfg.Port,
        db=cfg.DB,
        driver=cfg.Driver,
    )
    app.config.setdefault('SQLALCHEMY_DATABASE_URI', uri.create())
崔为之's avatar
崔为之 committed
40
    db.app = app
Weizhi Cui's avatar
Weizhi Cui committed
41
    db.init_app(app)
崔为之's avatar
崔为之 committed
42
    with app.app_context():
崔为之's avatar
崔为之 committed
43 44
        db.session.execute(text(f"""
        CREATE TABLE IF NOT EXISTS {cfg.TableName} (
崔为之's avatar
崔为之 committed
45 46 47 48 49 50 51 52 53
            id SERIAL NOT NULL,
            username VARCHAR(80) NOT NULL,
            email VARCHAR(120) NOT NULL,
            password VARCHAR(255) NOT NULL,
            active BOOLEAN,
            created_at TIMESTAMP WITH TIME ZONE NOT NULL,
            PRIMARY KEY (id, created_at)
        ) PARTITION BY RANGE (created_at);
        """))
崔为之's avatar
崔为之 committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        db.session.execute(text("""
        CREATE TABLE IF NOT EXISTS logs (
            id SERIAL NOT NULL,
            date_time TIMESTAMP WITH TIME ZONE NOT NULL,
            uuid varchar(100) DEFAULT NULL,
            mid varchar(50) DEFAULT NULL,
            mid_type varchar(50) DEFAULT NULL,
            mac_wifi varchar(50) DEFAULT NULL,
            mac_voice varchar(50) DEFAULT NULL,
            code smallint DEFAULT NULL,
            query varchar(255) DEFAULT NULL,
            terminal_domain varchar(50) DEFAULT NULL,
            terminal_intent varchar(50) DEFAULT NULL,
            distribution_gree_domain varchar(50) DEFAULT NULL,
            distribution_gree_intent varchar(50) DEFAULT NULL,
            response_text text,
            emotion_class varchar(50) DEFAULT NULL,
            skill_id varchar(100) DEFAULT NULL,
            voice_portal smallint NOT NULL,
            service_nlu varchar(50) DEFAULT NULL,
            service_type smallint NOT NULL,
            slots text,
            yzs_request_id varchar(50) DEFAULT NULL,
            yzs_remote_ip varchar(15) DEFAULT NULL,
            yzs_app_key varchar(50) DEFAULT NULL,
            yzs_ud_id varchar(50) DEFAULT NULL,
            yzs_user_id varchar(50) DEFAULT NULL,
            yzs_intent text,
            yzs_general text,
            yzs_nlu_time varchar(20) DEFAULT NULL,
            get_body_time varchar(20) DEFAULT NULL,
            gree_nlu_time varchar(20) DEFAULT NULL,
            get_homeid_time varchar(20) DEFAULT NULL,
            tencent_nlu_time varchar(20) DEFAULT NULL,
            cost_time varchar(20) DEFAULT NULL,
            PRIMARY KEY (id, date_time)
        ) PARTITION BY RANGE (date_time);
        """))
崔为之's avatar
崔为之 committed
92
        db.session.commit()