前后端更新
This commit is contained in:
1 parent
a0c8721fbb
commit
24da604727
33 files changed
+1951
-580
No files matched your search
+18
-8
@@ -11,13 +11,12 @@ backend/init_db.py
|
||||
cd backend
|
||||
python init_db.py
|
||||
"""
|
||||
import os
|
||||
import urllib.parse
|
||||
|
||||
import psycopg2
|
||||
|
||||
from app import create_app, db
|
||||
from app.config import DevelopmentConfig
|
||||
|
||||
|
||||
def _parse_db_url(url: str):
|
||||
"""
|
||||
@@ -51,14 +50,20 @@ def _parse_db_url(url: str):
|
||||
|
||||
def ensure_database():
|
||||
"""
|
||||
确保 hxops_db 数据库存在
|
||||
确保数据库存在
|
||||
核心逻辑:
|
||||
1. 解析 DevelopmentConfig.SQLALCHEMY_DATABASE_URI
|
||||
1. 获取当前环境配置的 SQLALCHEMY_DATABASE_URI
|
||||
2. 使用 psycopg2 连接到 postgres 系统数据库
|
||||
3. 查询 pg_database,如果不存在则执行 CREATE DATABASE
|
||||
"""
|
||||
# 从开发配置中获取数据库 URL
|
||||
url = DevelopmentConfig.SQLALCHEMY_DATABASE_URI
|
||||
# 获取当前环境
|
||||
env = os.getenv('FLASK_ENV', 'development')
|
||||
app = create_app(env)
|
||||
|
||||
# 从应用配置中获取数据库 URL
|
||||
url = app.config['SQLALCHEMY_DATABASE_URI']
|
||||
print(f"Checking database for environment '{env}': {url}")
|
||||
|
||||
info = _parse_db_url(url)
|
||||
target_db = info['dbname']
|
||||
|
||||
@@ -94,10 +99,15 @@ def create_tables():
|
||||
1. 创建 Flask 应用实例
|
||||
2. 在应用上下文中调用 db.create_all()
|
||||
"""
|
||||
app = create_app('development')
|
||||
env = os.getenv('FLASK_ENV', 'development')
|
||||
app = create_app(env)
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
print('All tables created in hxops_db')
|
||||
print(f'All tables created in {app.config["SQLALCHEMY_DATABASE_URI"]}')
|
||||
|
||||
if __name__ == '__main__':
|
||||
ensure_database()
|
||||
create_tables()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in new issue
Block a user