#!/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 from sqlalchemy import text from application.utils import DatabaseUri db = SQLAlchemy() def init_database(app: Flask) -> None: """ Initialize the database extension :param app: flask.Flask application instance :return: None """ cfg = app.config.Database uri = DatabaseUri( 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()) db.app = app db.init_app(app) with app.app_context(): db.session.execute(text(f""" CREATE TABLE IF NOT EXISTS {cfg.TableName} ( 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); """)) db.session.commit()