Beispiel #1
0
def get_database_for_model_instance(instance):
    if instance._state.db:
        return instance._state.db

    model = instance._meta.model
    possible_databases = get_possible_databases_for_model(model=model)
    if len(possible_databases) == 1:
        return possible_databases[0]
    elif len(possible_databases) == 0:
        pass
    else:
        model_has_sharded_id_field = getattr(
            model, 'django_sharding__sharded_by_field', None) is not None

        if model_has_sharded_id_field:
            sharded_by_field_id = getattr(
                instance,
                getattr(model, 'django_sharding__sharded_by_field',
                        'django_sharding__none'), None)
            if sharded_by_field_id is not None:
                return model.get_shard_from_id(sharded_by_field_id)

        return instance.get_shard()

    raise DjangoShardingException(
        "Unable to deduce datbase for model instance")
Beispiel #2
0
def is_model_class_on_database(model, database):
    specific_database = getattr(model, 'django_sharding__database', None)
    is_sharded = getattr(model, 'django_sharding__is_sharded', False)

    if specific_database and is_sharded:
        raise DjangoShardingException('Model marked as both sharded and on a single database, unable to determine where to run migrations for {}.'.format(model.__class__.__name__))

    if specific_database:
        return getattr(model, 'django_sharding__database') == database

    if is_sharded:
        shard_group = getattr(model, 'django_sharding__shard_group', None)
        if shard_group:
            return settings.DATABASES[database]['SHARD_GROUP'] == shard_group
        raise DjangoShardingException("Unable to determine what database the model is on as the shard group of {} is unknown.".format(model.__class__.__name__))

    return database == "default"
Beispiel #3
0
    def db_for_read(self, model, **hints):
        possible_databases = get_possible_databases_for_model(model=model)
        if len(possible_databases) == 1:
            return possible_databases[0]

        shard = self._get_shard(model, **hints)
        if shard:
            shard_group = getattr(model, 'django_sharding__shard_group', None)
            if not shard_group:
                raise DjangoShardingException('Unable to identify the shard_group for the {} model'.format(model))
            routing_strategy = self.get_read_db_routing_strategy(shard_group)
            return routing_strategy.pick_read_db(shard)
        return None
Beispiel #4
0
def get_database_for_model_instance(instance):
    if instance._state.db:
        return instance._state.db

    model = instance._meta.model
    possible_databases = get_possible_databases_for_model(model=model)
    if len(possible_databases) == 1:
        return possible_databases[0]
    elif len(possible_databases) == 0:
        pass
    else:
        return instance.get_shard()

    raise DjangoShardingException(
        "Unable to deduce datbase for model instance")