def load(): _redis = redis.Redis(REDIS_HOST, REDIS_PORT, db=1) _session_class = getDBSessionClass() _session = _session_class() logging.basicConfig(level=logging.DEBUG) if len(sys.argv) == 1: print("provide table names") return _table_names = sys.argv[1:] try: for _table in _table_names: logging.info("Loading %s...", _table) _cls = _table_2_class(_table) if _cls != None: _load_generic(_cls, _redis, _session) logging.info("Loading .... done.") else: logging.error("no such table: %s" % _table) except: traceback.print_exc() finally: _session_class.remove() logging.info("$$$$$$$$$$$ LOAD DONE $$$$$$$$$$$$$") return
def _bootstrap(): _levels = [API_LEVEL.PPCOM, API_LEVEL.PPKEFU, API_LEVEL.PPCONSOLE, API_LEVEL.THIRD_PARTY_KEFU, API_LEVEL.THIRD_PARTY_CONSOLE] _config = _check_bootstrap_config() if _config == None: return _session_class = getDBSessionClass() _session = _session_class() try: _clean_bootstrap_db_data(_session) _config = _create_bootstrap_first_user(_session, _config) _config = _create_bootstrap_first_team(_session, _config) _config = _create_bootstrap_team_data(_session, _config) for _level in _levels: _config = _create_bootstrap_api(_level, _session, _config) _config = _update_api_uuid_with_ppconsole(_session, _config) _config = _create_apns_settings(_session, _config) _config = _create_server_config(_session, _config) _config = _create_nginx_config(_session, _config) _create_log_dir() except: traceback.print_exc() finally: _session_class.remove() _print_bootstrap_result(_config) return
def load(): _redis = redis.Redis(REDIS_HOST, REDIS_PORT, db=1) _redis.flushdb() _session_class = getDBSessionClass() _session = _session_class() _cls_list = [ AdminUser, DeviceUser, DeviceInfo, ConversationInfo, ConversationUserData, FileInfo, MaterialRefInfo, AppInfo, AppUserData, AppBillingData, MessagePushTask, MessagePush, APNSSetting, AppPackageInfo, PCSocketInfo, PCSocketDeviceData, PortalWebSession, OrgGroup, OrgUserGroupData, ApiInfo, ApiTokenData, PredefinedScript, PredefinedScriptGroup, ] logging.basicConfig(level=logging.DEBUG) try: for _i in _cls_list: logging.info("Loading %s...", _i.__tablename__) _load_generic(_i, _redis, _session) logging.info("Loading .... done.") except: traceback.print_exc() finally: _session_class.remove() logging.info("$$$$$$$$$$$ LOAD DONE $$$$$$$$$$$$$") return
def _insert_into(_row): from ppmessage.db.dbinstance import getDBSessionClass _class = getDBSessionClass() _session = _class() try: _session.add(_row) _session.commit() except: _session.rollback() traceback.print_exc() finally: _class.remove() return
def load(_redis): _redis.flushdb() _session_class = getDBSessionClass() _session = _session_class() _cls_list = [ DeviceUser, DeviceInfo, ConversationInfo, ConversationUserData, FileInfo, MaterialRefInfo, AppInfo, AppUserData, MessagePushTask, MessagePush, APNSSetting, AppPackageInfo, PCSocketInfo, PCSocketDeviceData, OrgGroup, OrgUserGroupData, ApiInfo, ApiTokenData, PredefinedScript, PredefinedScriptGroup, ] try: for _i in _cls_list: logging.info("Loading %s...", _i.__tablename__) _load_generic(_i, _redis, _session) logging.info("Loading .... done.") except: traceback.print_exc() finally: _session_class.remove() logging.info("$$$$$$$$$$$ LOAD DONE $$$$$$$$$$$$$") return
def load(_redis): _redis.flushdb() _session_class = getDBSessionClass() _session = _session_class() _cls_list = [ DeviceUser, DeviceInfo, ConversationInfo, ConversationUserData, FileInfo, MaterialRefInfo, AppInfo, AppUserData, MessagePushTask, MessagePush, PCSocketInfo, PCSocketDeviceData, OrgGroup, OrgGroupUserData, ApiInfo, ApiTokenData, PredefinedScript, PredefinedScriptGroup, ] try: for _i in _cls_list: logging.info("Loading %s...", _i.__tablename__) _load_generic(_i, _redis, _session) logging.info("Loading .... done.") except: traceback.print_exc() finally: _session_class.remove() logging.info("$$$$$$$$$$$ LOAD DONE $$$$$$$$$$$$$") return
def load(_redis): _redis.flushdb() _session_class = getDBSessionClass() _session = _session_class() _cls_list = [ DeviceUser, DeviceInfo, ConversationInfo, ConversationUserData, FileInfo, ApiInfo, AppInfo, MessagePush, MessagePushTask ] try: for _i in _cls_list: _load_generic(_i, _redis, _session) except: traceback.print_exc() finally: _session_class.remove() return
def load(_redis): _redis.flushdb() _session_class = getDBSessionClass() _session = _session_class() _cls_list = [ DeviceUser, DeviceInfo, ConversationInfo, ConversationUserData, FileInfo, AppInfo, MessagePushTask, MessagePush, PCSocketInfo, PCSocketDeviceData, ApiInfo ] try: for _i in _cls_list: logging.info("Loading %s...", _i.__tablename__) _load_generic(_i, _redis, _session) logging.info("Loading .... done.") except: traceback.print_exc() finally: _session_class.remove() logging.info("$$$$$$$$$$$ LOAD DONE $$$$$$$$$$$$$") return
def task(self, _data): if "table" not in _data or \ "values" not in _data: logging.error("Error add request %s.", str(_data)) return _cls = _get_class_by_tablename(_data["table"]) if _cls == None: logging.error("Error for not db class for %s.", _data["table"]) return _rowdata = _data["values"] _sessionclass = getDBSessionClass() _session = _sessionclass() try: _session.query(_cls).filter_by(**_rowdata).delete() _session.commit() except: _session.rollback() traceback.print_exc() finally: _sessionclass.remove() return
def task(self, _data): if "table" not in _data or\ "values" not in _data: logging.error("Error add request %s.", str(_data)) return _cls = _get_class_by_tablename(_data["table"]) if _cls == None: logging.error("Error for not db class for %s.", _data["table"]) return _rowdata = _data["values"] _sessionclass = getDBSessionClass() _session = _sessionclass() try: _session.query(_cls).filter_by(**_rowdata).delete() _session.commit() except: _session.rollback() traceback.print_exc() finally: _sessionclass.remove() return
def _cache(dbsession): _redis = redis.Redis(REDIS_HOST, REDIS_PORT, db=1) _keys = _redis.keys("apns_settings.uuid.*") for _key in _keys: _redis.delete(_key) _cache_generic(APNSSetting, _redis, dbsession) return if __name__ == "__main__": import sys reload(sys) sys.setdefaultencoding('utf8') import codecs codecs.register(lambda name: codecs.lookup('utf8') if name == 'utf8mb4' else None) if len(sys.argv) > 1: if sys.argv[1] == "pro": is_dev = False print(is_dev) dbsession_class = getDBSessionClass() dbsession = dbsession_class() try: _clean(dbsession) _create(dbsession) _cache(dbsession) except: traceback.print_exc() finally: dbsession_class.remove()
def task(self, _data): if "table" not in _data or \ "values" not in _data or \ "key" not in _data: logging.error("update not enough parameters: %s" % str(_data)) return #logging.info("update with %s" % str(_data)) _cls = _get_class_by_tablename(_data["table"]) if _cls == None: logging.error("no db class for %s.", _data["table"]) return _kd = {} _key = _data["key"].split(".") _kd[_key[0]] = _key[1] _rowdata = _data["values"] if "createtime" in _rowdata: del _rowdata["createtime"] if "updatetime" in _rowdata: del _rowdata["updatetime"] for i in _cls.__table__.columns: if i.name not in _rowdata: continue if isinstance(i.type, String): continue if isinstance(i.type, Boolean): continue if isinstance(i.type, DateTime): _v = _rowdata.get(i.name) if len(_v) > 19: _v = datetime.datetime.strptime(_v, "%Y-%m-%d %H:%M:%S %f") else: _v = datetime.datetime.strptime(_v, "%Y-%m-%d %H:%M:%S") _rowdata[i.name] = _v continue if not isinstance(_rowdata[i.name], str): continue _rowdata[i.name] = eval(_rowdata[i.name]) _sessionclass = getDBSessionClass() _session = _sessionclass() try: _i = _session.query(_cls).filter_by(**_kd).scalar() if not _i: logging.error("update %s to:%s with:%s" % (_data["table"], str(_rowdata), str(_kd))) return for _k in _rowdata: setattr(_i, _k, _rowdata[_k]) _i.updatetime = datetime.datetime.now() _session.commit() except: _session.rollback() traceback.print_exc() finally: _sessionclass.remove() return
def task(self, _data): if "table" not in _data or \ "values" not in _data or \ "key" not in _data: logging.error("update not enough parameters: %s" % str(_data)) return logging.info("update with %s" % str(_data)) _cls = _get_class_by_tablename(_data["table"]) if _cls == None: logging.error("no db class for %s.", _data["table"]) return _kd = {} _key = _data["key"].split(".") _kd[_key[0]] = _key[1] _rowdata = _data["values"] if "createtime" in _rowdata: del _rowdata["createtime"] if "updatetime" in _rowdata: del _rowdata["updatetime"] for i in _cls.__table__.columns: if i.name not in _rowdata: continue if isinstance(i.type, String): continue if isinstance(i.type, Boolean): continue if isinstance(i.type, DateTime): _v = _rowdata.get(i.name) if len(_v) > 19: _v = datetime.datetime.strptime(_v, "%Y-%m-%d %H:%M:%S %f") else: _v = datetime.datetime.strptime(_v, "%Y-%m-%d %H:%M:%S") _rowdata[i.name] = _v continue if not isinstance(_rowdata[i.name], str): continue _rowdata[i.name] = eval(_rowdata[i.name]) _sessionclass = getDBSessionClass() _session = _sessionclass() try: _i = _session.query(_cls).filter_by(**_kd).scalar() if not _i: logging.error("error to update %s to:%s with:%s" % (_data["table"], str(_rowdata), str(_kd))) return for _k in _rowdata: setattr(_i, _k, _rowdata[_k]) _i.updatetime = datetime.datetime.now() _session.commit() except: _session.rollback() traceback.print_exc() finally: _sessionclass.remove() return
def task(self, _data): if "table" not in _data or "values" not in _data: logging.error("Error add request %s.", str(_data)) return _cls = _class(_data["table"]) if _cls == None: logging.error("Error for not db class for %s.", _data["table"]) return _rowdata = _data["values"] if "createtime" in _rowdata: del (_rowdata["createtime"]) if "updatetime" in _rowdata: del (_rowdata["updatetime"]) for i in _cls.__table__.columns: if i.name not in _rowdata: continue if isinstance(i.type, String): continue if isinstance(i.type, Boolean): continue if isinstance(i.type, DateTime): _v = _rowdata.get(i.name) if len(_v) > 19: _v = datetime.datetime.strptime(_v, "%Y-%m-%d %H:%M:%S %f") else: _v = datetime.datetime.strptime(_v, "%Y-%m-%d %H:%M:%S") _rowdata[i.name] = _v continue if not isinstance(_rowdata[i.name], str): continue _rowdata[i.name] = eval(_rowdata[i.name]) #logging.info("Adding db: %s. " % _data) _rowdata["createtime"] = datetime.datetime.now() _rowdata["updatetime"] = _rowdata["createtime"] _o = _cls(**_rowdata) _sessionclass = getDBSessionClass() _session = _sessionclass() _session.add(_o) try: _session.commit() except: _session.rollback() traceback.print_exc() finally: _sessionclass.remove() return
def task(self, _data): if "table" not in _data or "values" not in _data: logging.error("Error add request %s.", str(_data)) return _cls = _class(_data["table"]) if _cls == None: logging.error("Error for not db class for %s.", _data["table"]) return logging.info("create with %s." % str(_data)) _rowdata = _data["values"] if "createtime" in _rowdata: del(_rowdata["createtime"]) if "updatetime" in _rowdata: del(_rowdata["updatetime"]) for i in _cls.__table__.columns: if i.name not in _rowdata: continue if isinstance(i.type, String): continue if isinstance(i.type, Boolean): continue if isinstance(i.type, DateTime): _v = _rowdata.get(i.name) if len(_v) > 19: _v = datetime.datetime.strptime(_v, "%Y-%m-%d %H:%M:%S %f") else: _v = datetime.datetime.strptime(_v, "%Y-%m-%d %H:%M:%S") _rowdata[i.name] = _v continue if not isinstance(_rowdata[i.name], str): continue _rowdata[i.name] = eval(_rowdata[i.name]) #logging.info("Adding db: %s. " % _data) _rowdata["createtime"] = datetime.datetime.now() _rowdata["updatetime"] = _rowdata["createtime"] _o = _cls(**_rowdata) _sessionclass = getDBSessionClass() _session = _sessionclass() _session.add(_o) try: _session.commit() except: _session.rollback() traceback.print_exc() finally: _sessionclass.remove() return