def make_app(global_config, **settings): """ ripped from ptah/__init__.py:initializeSettings """ config = Configurator(settings=settings) try: # This is magic land. ptah.initialize(None, config, global_config) except memphis.config.StopException: memphis.config.shutdown() raise # Pyramid app = config.make_wsgi_app() # Using settings file; create database connections # Then .create_all() schemas whose models are registered but tables do not Base = pyramid_sqla.get_base() Base.metadata.create_all() config.begin() # Memphis, send ApplicationStarting event memphis.config.start(config) # Ptah, app initialized config.registry.notify(ptah.AppInitialized(app, config)) config.end() config.commit() # commit possible transaction transaction.commit() return app
def inspect(ev): engine = pyramid_sqla.get_engine() insp = reflection.Inspector.from_engine(engine) log = logging.getLogger('ptah-sql-migration') missing = [] md = pyramid_sqla.get_base().metadata for name, table in md.tables.items(): names = [rec['name'] for rec in insp.get_columns(name)] for cl in table.columns: if cl.name not in names: missing.append(cl) if missing: import migrate.changeset for cl in missing: log.warning("Adding column '%s' to table '%s'", cl.name, cl.table.name) cl.create(cl.table, populate_default=True)
# -*- coding: utf-8 -*- import logging import pyramid_sqla as psa import sqlalchemy as sa import sqlalchemy.orm as orm import transaction import hashlib import urllib from pylonshq.lib.sqlalchemy_ext import FromCache, RelationshipCache, CachingQuery from beaker.cache import cache_region log = logging.getLogger(__name__) Base = psa.get_base() Session = psa.get_session() Session.configure(extension=[psa._zte], query_cls=CachingQuery) class BaseModel(object): @classmethod def _get_keys(cls): """ return column names for this model """ return orm.class_mapper(cls).c.keys() def get_dict(self): """ return dict with keys and values corresponding to this model data """ d = {} for k in self._get_keys(): d[k] = getattr(self, k)