def __init__(self, directory, configuration_files, configuration_values, *args, **kwargs): super(ConfigurationExperiments2db, self).__init__(directory, configuration_files, configuration_values, *args, **kwargs) self.db_conf = DbConfiguration(configuration_files, configuration_values) self.config_js = os.path.join(directory, 'client', 'configuration.js') if os.path.exists(self.config_js): self.config = json.load(open(self.config_js)) else: self.config = {}
def on_dir(directory, configuration_files): db_conf = DbConfiguration(configuration_files) regular_url = db_conf.build_url() coord_url = db_conf.build_coord_url() upgrader = DbUpgrader(regular_url, coord_url) return upgrader.check_updated()
class ConfigurationExperiments2db(Upgrader): def __init__(self, directory, configuration_files, configuration_values, *args, **kwargs): super(ConfigurationExperiments2db, self).__init__(directory, configuration_files, configuration_values, *args, **kwargs) self.db_conf = DbConfiguration(configuration_files, configuration_values) self.config_js = os.path.join(directory, 'client', 'configuration.js') if os.path.exists(self.config_js): self.config = json.load(open(self.config_js)) else: self.config = {} @classmethod def get_priority(self): """ After database """ return 1 def check_updated(self): # In the future, this file will not exist. if not os.path.exists(self.config_js): return True # But if it exists and it has an 'experiments' section, it has # not been upgraded. if 'experiments' in self.config: print(" - configuration.js contains experiments. The experiments in the configuration.js are going to be migrated to the database.") return False return True def upgrade(self): connection_url = self.db_conf.build_url() engine = create_engine(connection_url, echo=False, convert_unicode=True) Session = sessionmaker(bind=engine) session = Session() try: for client_id, experiments in self.config['experiments'].iteritems(): for experiment in experiments: experiment_category = experiment['experiment.category'] experiment_name = experiment['experiment.name'] db_category = session.query(model.DbExperimentCategory).filter_by(name = experiment_category).first() if not db_category: # Experiment not in database continue db_experiment = session.query(model.DbExperiment).filter_by(category = db_category, name = experiment_name).first() if not db_experiment: # Experiment not in database continue db_experiment.client = client_id for key in experiment: if key in ('experiment.category','experiment.name'): # Already processed continue existing_parameter = session.query(model.DbExperimentClientParameter).filter_by(parameter_name = key, experiment = db_experiment).first() if existing_parameter: # Already processed in the past continue value = experiment[key] if isinstance(value, basestring): param_type = 'string' elif isinstance(value, bool): param_type = 'bool' elif isinstance(value, float): param_type = 'floating' elif isinstance(value, int): param_type = 'integer' else: print("Error: invalid parameter type %s for key %s of experiment %s" % (type(value), key, experiment_name)) continue param = model.DbExperimentClientParameter(db_experiment, key, param_type, unicode(experiment[key])) session.add(param) session.commit() finally: session.close() self.config.pop('experiments') json.dump(self.config, open(self.config_js, 'w'), indent = 4)
def __init__(self, directory, configuration_files, configuration_values, *args, **kwargs): super(DatabaseUpgrader, self).__init__(directory, configuration_files, configuration_values, *args, **kwargs) db_conf = DbConfiguration(configuration_files, configuration_values) regular_url = db_conf.build_url() coord_url = db_conf.build_coord_url() self.upgrader = DbUpgrader(regular_url, coord_url)