I noticed in the Pyramid + SQLAlchemy + URL Dispatch Wiki Tutorial that the database is initialized in the main function when the application is run.
def main(global_config, **settings):
""" This function returns a WSGI application.
"""
engine = engine_from_config(settings, 'sqlalchemy.')
initialize_sql(engine)
# -- and so on ---
where initialize_sql
is defined as follows:
def initialize_sql(engine):
DBSession.configure(bind=engine)
Base.metadata.bind = engine
Base.metadata.create_all(engine)
try:
session = DBSession()
page = Page('FrontPage', 'initial data')
session.add(page)
transaction.commit()
except IntegrityError:
# already created
pass
which essentially creates all of the tables (if they don't exist) and populates it with some initial values. Easy enough to understand, BUT...
This is just a tutorial to demonstrate a small application, so how it is typically done in production may differ (or not...). This brings me to my question:
When using Pyramid with SQLAlchemy, is it a typical pattern in production for a database to be initialized this way, or is it typical to use something equivalent to a manage syncdb
command in Django that is invoked manually?
Since Pyramid does not make any assumptions about data models, it does not attempt to manage them for you. This is entirely up to you and what specific data layer you are using.
With respect to using SQLAlchemy, it is possible to manage migrations using the SQLAlchemy-migrate package. When you set this up, it provides you with a manage
command to perform migrations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With