コード例 #1
0
def create_table_for_model_class(model, ):
    """
    创建Model类对应的数据库表

    :param model: Model类
    :return:
            True: success
            False: failure
    """
    try:
        using = router.db_for_write(model)
        with DatabaseSchemaEditor(
                connection=connections[using]) as schema_editor:
            schema_editor.create_model(model)
            if issubclass(model, BucketFileBase):
                try:
                    table_name = schema_editor.quote_name(model.Meta.db_table)
                    sql1 = f"ALTER TABLE {table_name} CHANGE COLUMN `na` `na` LONGTEXT NOT " \
                           f"NULL COLLATE 'utf8_bin' AFTER `id`;"
                    sql2 = f"ALTER TABLE {table_name} CHANGE COLUMN `name` `name` VARCHAR(255) " \
                           f"NOT NULL COLLATE 'utf8_bin' AFTER `na_md5`;"
                    schema_editor.execute(sql=sql1)
                    schema_editor.execute(sql=sql2)
                except Exception as exc:
                    if delete_table_for_model_class(model):
                        raise exc  # model table 删除成功,抛出错误
    except Exception as e:
        msg = traceback.format_exc()
        logger.error(msg)
        return False

    return True
コード例 #2
0
def delete_table_for_model_class(model):
    """
    删除Model类对应的数据库表

    :param model: Model类
    :return:
            True: success
            False: failure
    """
    try:
        using = router.db_for_write(model)
        with DatabaseSchemaEditor(
                connection=connections[using]) as schema_editor:
            schema_editor.delete_model(model)
    except Exception as e:
        logger.error(str(e))
        if e.args[0] in [1051, 1146]:  # unknown table or table not exists
            return True

        return False

    return True
コード例 #3
0
ファイル: base.py プロジェクト: rparent/django
 def schema_editor(self, *args, **kwargs):
     "Returns a new instance of this backend's SchemaEditor"
     return DatabaseSchemaEditor(self, *args, **kwargs)
コード例 #4
0
 def schema_editor(self, *args, **kwargs):
     "Returns a new instance of this backend's SchemaEditor"
     from django.db.backends.mysql.schema import DatabaseSchemaEditor
     return DatabaseSchemaEditor(self, *args, **kwargs)