def generate_database_configs(unshard: Optional[Dict] = None, shard: Optional[Dict] = None) -> Dict: configuration = {} # Make Unshard Configuration if unshard: for key, config in unshard.items(): configuration.update( make_replication_configuration( key=key, master=config['master'], slaves=config.get('slaves', []), conn_max_age=config.get('conn_max_age', DEFAULT_CONN_MAX_AGE))) # Make Shard Configuration if shard: for shard_group, config in shard.items(): configuration.update( make_shard_configuration( shard_group=shard_group, database_name=config['database_name'], logical_count=config['logical_count'], conn_max_age=config.get('conn_max_age', DEFAULT_CONN_MAX_AGE), shards=config['shards'], options=config.get('options', {}))) return configuration
def test_shard_configuration_with_slave(self): configs = make_shard_configuration(shard_group='GROUP', shard_options={ 'database_name': 'prepare_product', 'logical_count': 4, }, shards=[{ 'master': 'mysql://*****:*****@host/', 'slaves': [ 'mysql://*****:*****@host/', 'mysql://*****:*****@host/', ] }, { 'master': 'mysql://*****:*****@host/', 'slaves': [ 'mysql://*****:*****@host/', 'mysql://*****:*****@host/', ] }]) primary_config = [ key for key in configs if configs[key].get(DATABASE_CONFIG_MASTER, None) is None ] slave_config = {} for key in configs: if key in primary_config: continue primary = configs[key]['MASTER'] if primary not in slave_config: slave_config[primary] = [] slave_config[primary].append(key) self.assertEqual(len(primary_config), 4) for key in primary_config: self.assertEqual(len(slave_config[key]), 2) self.assertEqual(configs[key][DATABASE_CONFIG_SHARD_GROUP], 'GROUP') self.assertTrue(DATABASE_CONFIG_SHARD_NUMBER in configs[key]) for slave_key in slave_config[key]: self.assertEqual(configs[slave_key][DATABASE_CONFIG_MASTER], key)
def test_shard_configuration_only_master(self): configs = make_shard_configuration(shard_group='GROUP', shard_options={ 'database_name': 'prepare_product', 'logical_count': 4, }, shards=[{ 'master': 'mysql://*****:*****@host/' }]) self.assertEqual(len(configs), 4) for value in configs.values(): self.assertEqual(value[DATABASE_CONFIG_SHARD_GROUP], 'GROUP') self.assertTrue(DATABASE_CONFIG_SHARD_NUMBER in value) self.assertFalse(DATABASE_CONFIG_MASTER in value)