Package Camelot :: Package camelot :: Package core :: Module sql |
[frames] | no frames]

# Source Code for Module Camelot.camelot.core.sql

     1  import logging
     2
     3  import sqlalchemy.sql.operators
     4
     5 -def like_op(column, string):
     6      return sqlalchemy.sql.operators.like_op(column, '%%%s%%'%string)
     7
     8 -def transaction(original_function):
     9      """Decorator for methods on an entity, to make them transactional"""
    10
    11      logger = logging.getLogger('camelot.core.sql.transaction')
    12
    13      def decorated_function(cls, *args, **kwargs):
    14          session = cls.query.session
    15          session.begin()
    16          try:
    17              result = original_function(cls, *args, **kwargs)
    18              session.commit()
    19          except Exception, e:
    20              logger.error('Unhandled exception, rolling back transaction', exc_info=e)
    21              session.rollback()
    22              raise e
    23          return result
    24
    25      return decorated_function
    26
    27 -def update_database_from_model():
    28      """Do some introspection on the model and add missing columns in the database
    29
    30      this function can be ran in setup_model after setup_all(create_tables=True)
    31      """
    32      import settings
    33      migrate_engine = settings.ENGINE()
    34      migrate_connection = migrate_engine.connect()
    35
    36      from camelot.model import metadata
    37      from migrate.versioning.schemadiff import SchemaDiff
    38      from migrate.changeset import create_column
    39      schema_diff = SchemaDiff(metadata, migrate_connection)
    40
    41      for table_with_diff in schema_diff.tablesWithDiff:
    42          missingInDatabase, _missingInModel, _diffDecl = schema_diff.colDiffs[table_with_diff.name]
    43          for col in missingInDatabase:
    44              create_column(col, table_with_diff)
    45