我试图创建一个这样做的功能
def get_current_database_version(): path = os.path.join(os.path.dirname(__file__),os.path.pardir) alembic_cfg = Config(os.path.join(path,'alembic.ini')) current_rev = command.current(alembic_cfg,head_only=True) return current_rev
此函数返回NoSectionError:No section:’formatters’
然后我去了我的alembic.ini文件,检查它是否有格式化区域.这是我的alembic.ini文件:
# A generic,single database configuration.[alembic]# path to migration scriptsscript_location = alembicpyramID_config_file = ../../development.ini# template used to generate migration files# file_template = %%(rev)s_%%(slug)s# max length of characters to apply to the# "slug" fIEld#truncate_slug_length = 40# set to 'true' to run the environment during# the 'revision' command,regardless of autogenerate# revision_environment = false# set to 'true' to allow .pyc and .pyo files without# a source .py file to be detected as revisions in the# versions/ directory# sourceless = falsesqlalchemy.url = sqlite:///%(here)s/mgo.sqlite# Logging configuration[loggers]keys = root,sqlalchemy,alembic[handlers]keys = console[formatters]keys = generic[logger_root]level = WARNhandlers = consolequalname =[logger_sqlalchemy]level = WARNhandlers =qualname = sqlalchemy.engine[logger_alembic]level = INFOhandlers =qualname = alembic[handler_console]class = StreamHandlerargs = (sys.stderr,)level = NOTSETformatter = generic[formatter_generic]format = %(levelname)-5.5s [%(name)s] %(message)sdatefmt = %H:%M:%s
谁知道我做错了什么?谢谢
编辑:
这是我尝试使用MigrationContext来解决问题:
def get_database_revision(): engine = create_engine("sqlite:///../mgo.db") conn = engine.connect() context = MigrationContext.configure(conn) current_rev = context.get_current_revision() return current_rev
它连接但没有返回.使用sqlite浏览器我可以看到数据库中的版本未设置为none.
解决方法 您可以将MigrationContext用于 get the current version:from alembic.migration import MigrationContextfrom sqlalchemy import create_engineengine = create_engine("postgresql://mydatabase")conn = engine.connect()context = MigrationContext.configure(conn)current_rev = context.get_current_revision()
在env.py中你可以使用:
from alembic import contextmigration_context = context.get_context()current_rev = context.get_current_revision()
最后,它基本上归结为连接到数据库并查看alembic_version表.它包含迁移版本作为值,这是数据库当前所在的位置(根据alembic).所以你可以用你想要的任何方式编写代码,只要这最终你正在做什么.
总结以上是内存溢出为你收集整理的python – 以编程方式获取Alembic数据库版本全部内容,希望文章能够帮你解决python – 以编程方式获取Alembic数据库版本所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)