将Heroku DATABASE_URL设置为Pyramid Config

时间:2016-10-04 14:17:34

标签: python postgresql heroku sqlalchemy pyramid

目标:将Heroku DATABASE_URL变量设置为sqlalchemy.url=postgres://...__init__.py文件中的development.ini设置。目前,我直接连接到数据库地址(可以更改)。

Heroku支持报告的问题:

  

如果您已将数据库连接字符串硬编码到您的ini中   文件可能不是最好的主意。虽然它现在可以使用,   如果将来某个时候我们需要更改数据库的位置   运行(由于各种原因确实发生)然后你的应用程序   将不再连接到您的数据库。如果您的数据库确实移动了   我们确保更新DATABASE_URL,以便您的应用程序使用   这个。如果这不是已设置的内容,可能会将sqlalchemy.url更改为sqlalchemy.url = os.environ.get('DATABASE_URL')

但是,地址sqlalchemy.url = os.environ.get('DATABASE_URL')执行 NOT 。它崩溃了我的应用程序。我甚至尝试过:sqlalchemy.url = postgresql://os.environ.get('DATABASE_URL')sqlalchemy.url = postgres://os.environ.get('DATABASE_URL'),最后是sqlalchemy.url = postgres://'DATABASE_URL'。所有这些都 NOT 工作。

SQLALCEHMY engine_config设置:docs

  

sqlalchemy.engine_from_config(configuration, prefix='sqlalchemy.',
 **kwargs)
  

使用配置字典创建新的Engine实例。

     

字典通常是从配置文件生成的。

     

engine_from_config()感兴趣的键应该加上前缀,例如   sqlalchemy.url,sqlalchemy.echo等.'prefix'参数表示   要搜索的前缀。每个匹配的密钥(在前缀之后是   stripped)被视为相应的关键字   create_engine()电话的参数。

     

唯一需要的密钥是(假设默认前缀)sqlalchemy.url,   它提供了数据库URL。

     

一组选定的关键字参数将被“强制”为其预期值   基于字符串值键入。参数集是可扩展的   使用engine_config_types访问器进行每个方言。

     

参数:配置 - 字典(通常由a。生成)   配置文件,但这不是必需的)。键开始的项目   使用'prefix'的值会删除该前缀,并且会   然后传递给create_engine。前缀 - 匹配的前缀然后   在“配置”中从键中剥离。 kwargs - 每个关键字参数   engine_from_config()本身会覆盖相应的项目   来自'配置'字典。关键字参数不应该是   前缀。

外部示例(对我不起作用):

我认为问题在于我的应用中设置和引擎的设置方式。我发现本教程很有帮助,但我的代码不同:Environment Variables in Pyramid

  

我们最终想要做的是将sqlalchemy.url动态设置为   我们的DATABASE_URL环境变量的值。   learning_journal / init .py是我们的.ini文件的配置   绑定到我们的金字塔应用程序。在添加当前设置之前   在Configurator中,我们可以使用os.environ来引入我们的   环境的DATABASE_URL。

# __init__.py

import os

from pyramid.config import Configurator


def main(global_config, **settings):
        """ This function returns a Pyramid WSGI application.
        """
        settings["sqlalchemy.url"] = os.environ["DATABASE_URL"]
        config = Configurator(settings=settings)
        config.include('pyramid_jinja2')
        config.include('.models')
        config.include('.routes')
        config.scan()
        return config.make_wsgi_app()
Because we should always try to keep code DRY (and prevent future confusion), remove the sqlalchemy.url keyword from development.ini.

我的代码:

初始化的.py

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application"""
    #global_config argument is a dictionary of key/value pairs mentioned in the [DEFAULT] section of an development.ini file
    # **settings argument collects another set of arbitrary key/value pairs
    #The main function first creates a SQLAlchemy database engine using sqlalchemy.engine_from_config() from the sqlalchemy. prefixed settings in the development.ini file's [app:main] section. This will be a URI (something like sqlite://):
    engine = engine_from_config(settings, 'sqlalchemy.')
    Session.configure(bind=engine)
    Base.metadata.bind = engine
    ...
    config.include('pyramid_jinja2')
    config.include('pyramid_mailer')
    config.add_static_view('static', 'static', cache_max_age=3600)

development.ini

#former db:
#sqlalchemy.url = postgres://localhost/NOTSSdb

#works, but unstable should db move:
sqlalchemy.url = postgres://ijfbcvuyifb.....

initialize_db脚本:

def main(argv=sys.argv):
    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]
    options = parse_vars(argv[2:])
    setup_logging(config_uri)
    settings = get_appsettings(config_uri, options=options)
    engine = engine_from_config(settings)

0 个答案:

没有答案
相关问题