将数据操作全部改为迁移脚本

This commit is contained in:
zhengxinonly
2021-06-14 02:10:29 +08:00
parent 9e8a5357be
commit d7c7237cd4
37 changed files with 452 additions and 536 deletions
+6 -11
View File
@@ -3,8 +3,6 @@ from __future__ import with_statement
import logging
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from flask import current_app
from alembic import context
@@ -13,7 +11,7 @@ from alembic import context
# access to the values within the .ini file in use.
config = context.config
# Interpret the configs file for Python logging.
# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)
logger = logging.getLogger('alembic.env')
@@ -24,12 +22,13 @@ logger = logging.getLogger('alembic.env')
# target_metadata = mymodel.Base.metadata
config.set_main_option(
'sqlalchemy.url',
str(current_app.extensions['migrate'].db.engine.url).replace('%', '%%'))
str(current_app.extensions['migrate'].db.get_engine().url).replace(
'%', '%%'))
target_metadata = current_app.extensions['migrate'].db.metadata
# other values from the configs, defined by the needs of env.py,
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = configs.get_main_option("my_important_option")
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
@@ -72,11 +71,7 @@ def run_migrations_online():
directives[:] = []
logger.info('No changes in schema detected.')
connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
poolclass=pool.NullPool,
)
connectable = current_app.extensions['migrate'].db.get_engine()
with connectable.connect() as connection:
context.configure(