問題復現:
INFO [alembic.runtime.migration] Context impl SQLiteImpl. INFO [alembic.runtime.migration] Will assume non-transactional DDL. INFO [alembic.env] No changes in schema detected.
配置檔案:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ # # Authors: limanman # 51CTOBG: http://xmdevops.blog.51cto.com/ # Purpose: # """ from __future__ import absolute_import # 說明: 配置基類 class __Config(object): # -- flask-pids PID_FILE = `logs/xmzoomeye-mtr.pid` # -- flask-sqlalchemy SQLALCHEMY_ECHO = True SQLALCHEMY_RECORD_QUERIES = True SQLALCHEMY_NATIVE_UNICODE = True SQLALCHEMY_TRACK_MODIFICATIONS = True SQLALCHEMY_COMMIT_ON_TEARDOWN = True SQLALCHEMY_DATABASE_URI = `sqlite:///app/conf/mtrdata.db` @staticmethod def init_app(app): pass # 說明: 開發環境 class __DevelopmentConfig(__Config): pass # 說明: 預測環境 class __TestingConfig(__Config): pass # 說明: 正式環境 class __ProductionConfig(__Config): pass config = { `default`: __DevelopmentConfig, `develop`: __DevelopmentConfig, `testing`: __TestingConfig, `product`: __ProductionConfig, }
問題排查:
1. 此應用為一個網路檢測展示程式,為了簡化就沒有使用任務佇列,直接後端跑一個mtr檢測,利用協程的方式不影響前端資料獲取和展示
2. 框架寫好後發現遷移命令python xmzoomeye-mtr db init時發現flask-migrate竟然沒有檢測到我定義的表….., 這是什麼鬼?
3. 後來無意間看到網上的一段程式碼突然發現…自己沒有在任何一個檔案中匯入過自定義的表….動手嘗試~ 竟然成功…. 原來flask-migrate是檢測上下文中db.Model的子類來建立表的…
解決方案:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ # # Authors: limanman # OsChina: http://xmdevops.blog.51cto.com/ # Purpose: # """ # 說明: 匯入公共模組 from app import db as _db from app import create_app # 說明: 為資料庫檢測 from app.models import Area, Addr, Info from flask_script import Manager, Command from flask_migrate import Migrate, MigrateCommand # 說明: 匯入其它模組 app = create_app() manager = Manager(app) migrate = Migrate(app, _db) manager.add_command(`db`, MigrateCommand) if __name__ == `__main__`: manager.run()
說明: 既然檢測上下文中的db.Model的子類,所以只要在任意正確位置匯入即可被檢測到,so~ 為了方便我直接在入口檔案中新增了~嘗試再次初始化/遷移/升級~
再次建立:
D:XmDevOps_Py estxmzoomeye-mtr>python xmzoomeye-mtr db init Creating directory D:XmDevOps_Py estxmzoomeye-mtrmigrations ... done Creating directory D:XmDevOps_Py estxmzoomeye-mtrmigrationsversions ... don e Generating D:XmDevOps_Py estxmzoomeye-mtrmigrationsalembic.ini ... done Generating D:XmDevOps_Py estxmzoomeye-mtrmigrationsenv.py ... done Generating D:XmDevOps_Py estxmzoomeye-mtrmigrationsenv.pyc ... done Generating D:XmDevOps_Py estxmzoomeye-mtrmigrationsREADME ... done Generating D:XmDevOps_Py estxmzoomeye-mtrmigrationsscript.py.mako ... done Please edit configuration/connection/logging settings in `D:\XmDevOps_Py\test xmzoomeye-mtr\migrations\alembic.ini` before proceeding. D:XmDevOps_Py estxmzoomeye-mtr>python xmzoomeye-mtr db migrate INFO [alembic.runtime.migration] Context impl SQLiteImpl. INFO [alembic.runtime.migration] Will assume non-transactional DDL. INFO [alembic.autogenerate.compare] Detected added table `areas` INFO [alembic.autogenerate.compare] Detected added index `ix_areas_areaname` on `[`areaname`]` INFO [alembic.autogenerate.compare] Detected added table `addrs` INFO [alembic.autogenerate.compare] Detected added index `ix_addrs_addr` on `[` addr`]` INFO [alembic.autogenerate.compare] Detected added table `infos` Generating D:XmDevOps_Py estxmzoomeye-mtrmigrationsversionse5295ab2586d_.p y ... done D:XmDevOps_Py estxmzoomeye-mtr>python xmzoomeye-mtr db upgrade INFO [alembic.runtime.migration] Context impl SQLiteImpl. INFO [alembic.runtime.migration] Will assume non-transactional DDL. INFO [alembic.runtime.migration] Running upgrade -> e5295ab2586d, empty messag e