From 47258f4cf597c7a02742a9c79d2cac86b9556d75 Mon Sep 17 00:00:00 2001 From: cuiweizhi <560397@gree.com.cn> Date: Sun, 12 Nov 2023 11:03:27 +0800 Subject: [PATCH] Update project --- application/config/config.yaml | 11 ++--------- application/extensions/init_sqlalchemy.py | 4 ++-- application/libs/tasks/task.py | 2 ++ application/models/user.py | 9 ++++----- 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/application/config/config.yaml b/application/config/config.yaml index 0fa3f99..6e089b4 100644 --- a/application/config/config.yaml +++ b/application/config/config.yaml @@ -9,7 +9,7 @@ Database: Host: localhost Port: 5432 DB: elp - TableName: user + TableName: users Elasticsearch: Host: 172.28.5.39 @@ -32,14 +32,7 @@ Logger: Enqueue: True Retention: -Redis: - Host: localhost - Port: 6379 - Username: - Password: - DB: 13 - Scheduler: - Start: 2023-11-11 18:20:00 + Start: 2023-11-12 10:04:00 End: 2099-11-11 16:00:00 Timezone: Asia/Shanghai diff --git a/application/extensions/init_sqlalchemy.py b/application/extensions/init_sqlalchemy.py index 2ca03dd..bb5480e 100644 --- a/application/extensions/init_sqlalchemy.py +++ b/application/extensions/init_sqlalchemy.py @@ -40,8 +40,8 @@ def init_database(app: Flask) -> None: db.app = app db.init_app(app) with app.app_context(): - db.session.execute(text(""" - CREATE TABLE IF NOT EXISTS users ( + 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, diff --git a/application/libs/tasks/task.py b/application/libs/tasks/task.py index 0007b70..58f3894 100644 --- a/application/libs/tasks/task.py +++ b/application/libs/tasks/task.py @@ -13,6 +13,7 @@ import datetime from application.extensions.init_sqlalchemy import db from application.models import User +from application.common import global_config def task(): @@ -26,4 +27,5 @@ def task(): user = User(username="CWzz", email="Stuu@outlook.com", password="qwe!2345", active=True, created_at=datetime.datetime.utcnow()) user.save() + print(db.app.config) print("created user admin") diff --git a/application/models/user.py b/application/models/user.py index e53a6f1..94d8433 100644 --- a/application/models/user.py +++ b/application/models/user.py @@ -10,7 +10,7 @@ # @Description : """ -from datetime import datetime +import os from sqlalchemy import text from sqlalchemy.ext.hybrid import hybrid_property @@ -20,7 +20,7 @@ from application.extensions.init_sqlalchemy import db class User(db.Model): """Basic user model""" - __tablename__ = 'users' + __tablename__ = os.environ.get('TableName', 'users') id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True, nullable=False) @@ -54,14 +54,13 @@ class User(db.Model): def save(self): partition_date = self.created_at.strftime('%Y_%m') - partition_name = f'users_{partition_date}' + partition_name = f'{self.__tablename__}_{partition_date}' with db.session.begin_nested(): db.session.execute(text(f""" - CREATE TABLE IF NOT EXISTS {partition_name} PARTITION OF users + CREATE TABLE IF NOT EXISTS {partition_name} PARTITION OF {self.__tablename__} FOR VALUES FROM ('{self.created_at.strftime('%Y-%m-01')}') TO ('{self.created_at.strftime('%Y-%m-01')}'::date + interval '1 month'); """)) db.session.add(self) db.session.commit() - -- GitLab