database.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:exac_readviz_scripts 作者: macarthur-lab 项目源码 文件源码
def _create_table(model, fail_silently=True):
    """Utility method for creating a database table and indexes that is a
    work around for unexpected behavior by the peewee ORM module. Specifically,
    peewee create_table doesn't create compound indexes as expected.

    Args:
      db: an active peewee database connection
      model: subclass of peewee.Model
      indexes: a tuple of indexes that would normally be specified in
         the peewee.Mode's class Meta. Example:

         indexes = (
            (('chrom', 'start', 'end'), True),  # True means unique index
          )
      fail_silently: if True, no error will be raised if the table already exists
    """
    # create table as a compressed TokuDB table
    db = model._meta.database
    indexes = model._meta.indexes
    raw_query = db.compiler().create_table(model, safe=fail_silently)
    raw_query = list(raw_query)

    raw_query[0] = raw_query[0] + " engine=TokuDB, compression='tokudb_zlib', charset=latin1"
    db.execute_sql(*raw_query)
    logging.debug(raw_query[0])

    # create indexes
    safe_str = "IF NOT EXISTS" if fail_silently else ""
    model_name = model.__name__.lower()
    for i, (columns, unique) in enumerate(indexes):
        columns_str = ",".join(map(lambda c: "`"+c+"`", columns))
        unique_str = "UNIQUE" if unique else ""
        q = "ALTER TABLE `%(model_name)s` ADD %(unique_str)s KEY %(safe_str)s `index%(i)s`(%(columns_str)s);" % locals()
        logging.debug(q)
        db.execute_sql(q)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号