Skip to content
This repository was archived by the owner on May 6, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions google/cloud/ndb/_datastore_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,10 @@ def _datastore_lookup(keys, read_options, retries=None, timeout=None, metadata=(
client = context_module.get_context().client
request = datastore_pb2.LookupRequest(
project_id=client.project,
database_id=client.database,
keys=[key for key in keys],
read_options=read_options,
)
helpers.set_database_id_to_request(request, client.database)
metadata = _add_routing_info(metadata, request)

return make_call(
Expand Down Expand Up @@ -881,11 +881,11 @@ def _datastore_commit(mutations, transaction, retries=None, timeout=None, metada
client = context_module.get_context().client
request = datastore_pb2.CommitRequest(
project_id=client.project,
database_id=client.database,
mode=mode,
mutations=mutations,
transaction=transaction,
)
helpers.set_database_id_to_request(request, client.database)
metadata = _add_routing_info(metadata, request)

return make_call(
Expand Down Expand Up @@ -1007,9 +1007,8 @@ def _datastore_allocate_ids(keys, retries=None, timeout=None, metadata=()):
:class:`google.cloud.datastore_v1.datastore_pb2.AllocateIdsResponse`
"""
client = context_module.get_context().client
request = datastore_pb2.AllocateIdsRequest(
project_id=client.project, database_id=client.database, keys=keys
)
request = datastore_pb2.AllocateIdsRequest(project_id=client.project, keys=keys)
helpers.set_database_id_to_request(request, client.database)
metadata = _add_routing_info(metadata, request)

return make_call(
Expand Down Expand Up @@ -1070,9 +1069,9 @@ def _datastore_begin_transaction(read_only, retries=None, timeout=None, metadata

request = datastore_pb2.BeginTransactionRequest(
project_id=client.project,
database_id=client.database,
transaction_options=options,
)
helpers.set_database_id_to_request(request, client.database)
metadata = _add_routing_info(metadata, request)

return make_call(
Expand Down Expand Up @@ -1121,8 +1120,10 @@ def _datastore_rollback(transaction, retries=None, timeout=None, metadata=()):
"""
client = context_module.get_context().client
request = datastore_pb2.RollbackRequest(
project_id=client.project, database_id=client.database, transaction=transaction
project_id=client.project,
transaction=transaction,
)
helpers.set_database_id_to_request(request, client.database)
metadata = _add_routing_info(metadata, request)

return make_call(
Expand Down
4 changes: 2 additions & 2 deletions google/cloud/ndb/_datastore_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,17 +1011,17 @@ def _datastore_run_query(query):
query_pb = _query_to_protobuf(query)
partition_id = entity_pb2.PartitionId(
project_id=query.project,
namespace_id=query.namespace,
database_id=query.database,
namespace_id=query.namespace,
)
read_options = _datastore_api.get_read_options(query)
request = datastore_pb2.RunQueryRequest(
project_id=query.project,
database_id=query.database,
partition_id=partition_id,
query=query_pb,
read_options=read_options,
)
helpers.set_database_id_to_request(request, query.database)
metadata = _datastore_api._add_routing_info((), request)

response = yield _datastore_api.make_call(
Expand Down
15 changes: 1 addition & 14 deletions google/cloud/ndb/_gql.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,28 +93,17 @@ class GQL(object):
_limit = -1
_hint = ""

def __init__(
self,
query_string,
_app=None,
_auth_domain=None,
namespace=None,
*,
database: str = None
):
def __init__(self, query_string, _app=None, _auth_domain=None, namespace=None):
"""Parses the input query into the class as a pre-compiled query.

Args:
query_string (str): properly formatted GQL query string.
namespace (str): The namespace to use for this query. Defaults to the client's value.
database (str): The database to use for this query. Defaults to the client's value.
Raises:
exceptions.BadQueryError: if the query is not parsable.
"""
self._app = _app

self._database = database

self._namespace = namespace

self._auth_domain = _auth_domain
Expand Down Expand Up @@ -722,7 +711,6 @@ def get_query(self):
keys_only = None
projection = self.projection()
project = self._app
database = self._database
namespace = self._namespace
if self.is_distinct():
distinct_on = projection
Expand All @@ -740,7 +728,6 @@ def get_query(self):
filters=filters,
order_by=order_by,
project=project,
database=database,
namespace=namespace,
default_options=default_options,
projection=projection,
Expand Down
3 changes: 1 addition & 2 deletions google/cloud/ndb/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ def __init__(
namespace=None,
credentials=None,
client_options=None,
*,
database: str = ""
database=None,
):
self.namespace = namespace
self.host = os.environ.get(environment_vars.GCD_HOST, DATASTORE_API_HOST)
Expand Down
15 changes: 7 additions & 8 deletions google/cloud/ndb/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class Key(object):
from google.cloud.ndb import context as context_module
client = mock.Mock(
project="testing",
database="",
database=None,
namespace=None,
stub=mock.Mock(spec=()),
spec=("project", "database", "namespace", "stub"),
Expand Down Expand Up @@ -378,7 +378,7 @@ def __hash__(self):

def _tuple(self):
"""Helper to return an orderable tuple."""
return (self.app(), self.namespace(), self.database(), self.pairs())
return (self.app(), self.namespace(), self.database() or "", self.pairs())

def __eq__(self, other):
"""Equality comparison operation."""
Expand Down Expand Up @@ -464,7 +464,7 @@ def __setstate__(self, state):
_clean_flat_path(flat)
project = _project_from_app(kwargs["app"])

database = ""
database = None
if "database" in kwargs:
database = kwargs["database"]

Expand Down Expand Up @@ -599,8 +599,7 @@ def database(self):
>>> key.database()
'mydb'
"""
db = self._key.database or ""
return db
return self._key.database

def id(self):
"""The string or integer ID in the last ``(kind, id)`` pair, if any.
Expand Down Expand Up @@ -1449,12 +1448,12 @@ def _parse_from_args(
# Offload verification of parent to ``google.cloud.datastore.Key()``.
parent_ds_key = parent._key

if database == "":
database = None

if namespace == "":
namespace = None

if database is None:
database = ""

return google.cloud.datastore.Key(
*flat,
parent=parent_ds_key,
Expand Down
12 changes: 5 additions & 7 deletions google/cloud/ndb/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

client = mock.Mock(
project="testing",
database="",
database=None,
namespace=None,
stub=mock.Mock(spec=()),
spec=("project", "namespace", "database", "stub"),
Expand Down Expand Up @@ -4864,8 +4864,8 @@ def __init__(_self, **kwargs):
id_ = self._get_arg(kwargs, "id")
project = self._get_arg(kwargs, "project")
app = self._get_arg(kwargs, "app")
namespace = self._get_arg(kwargs, "namespace", key_module.UNDEFINED)
database = self._get_arg(kwargs, "database", key_module.UNDEFINED)
namespace = self._get_arg(kwargs, "namespace", key_module.UNDEFINED)
parent = self._get_arg(kwargs, "parent")
projection = self._get_arg(kwargs, "projection")

Expand Down Expand Up @@ -5488,7 +5488,6 @@ def _prepare_for_put(self):
distinct_on=None,
group_by=None,
default_options=None,
database=None,
)
def _query(cls, *filters, **kwargs):
"""Generate a query for this class.
Expand All @@ -5515,8 +5514,6 @@ def _query(cls, *filters, **kwargs):
results.
group_by (list[str]): Deprecated. Synonym for distinct_on.
default_options (QueryOptions): QueryOptions object.
database (str): The database to perform the query against.
If not passed, uses the client's value.
"""
# Validating distinct
if kwargs["distinct"]:
Expand Down Expand Up @@ -5546,7 +5543,6 @@ def _query(cls, *filters, **kwargs):
distinct_on=kwargs["distinct_on"],
group_by=kwargs["group_by"],
default_options=kwargs["default_options"],
database=kwargs["database"],
)
query = query.filter(*cls._default_filters())
query = query.filter(*filters)
Expand Down Expand Up @@ -5724,7 +5720,7 @@ def _get_by_id(
max_memcache_items=None,
force_writes=None,
_options=None,
database: str = None,
database=None,
):
"""Get an instance of Model class by ID.

Expand Down Expand Up @@ -5768,6 +5764,8 @@ def _get_by_id(
``global_cache_timeout``.
max_memcache_items (int): No longer supported.
force_writes (bool): No longer supported.
database (Optional[str]): Database for the entity to load. If not
passed, uses the client's value.

Returns:
Optional[Model]: The retrieved entity, if one is found.
Expand Down
21 changes: 10 additions & 11 deletions google/cloud/ndb/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def ranked(cls, rank):
import logging
import six

from google.cloud.ndb import context as context_module
from google.cloud.ndb import exceptions
from google.cloud.ndb import _options
from google.cloud.ndb import tasklets
Expand Down Expand Up @@ -1267,8 +1268,8 @@ def __init__(self, config=None, context=None, **kwargs):
if not self.project:
self.project = context.client.project

if self.database is None:
self.database = context.client.database
# We always use the client's database, for consistency with python-datastore
self.database = context.client.database

if self.namespace is None:
if self.ancestor is None:
Expand Down Expand Up @@ -1304,7 +1305,6 @@ class Query(object):
results.
group_by (list[str]): Deprecated. Synonym for distinct_on.
default_options (QueryOptions): QueryOptions object.
database (str): The database to access. If not passed, uses the client's value.

Raises:
TypeError: If any of the arguments are invalid.
Expand All @@ -1327,8 +1327,6 @@ def __init__(
offset=None,
keys_only=None,
default_options=None,
*,
database: str = None,
):
# Avoid circular import in Python 2.7
from google.cloud.ndb import model
Expand Down Expand Up @@ -1374,7 +1372,6 @@ def __init__(
orders = self._option("orders", orders)
project = self._option("project", project)
app = self._option("app", app)
database = self._option("database", database)
namespace = self._option("namespace", namespace)
projection = self._option("projection", projection)
distinct_on = self._option("distinct_on", distinct_on)
Expand All @@ -1383,6 +1380,9 @@ def __init__(
offset = self._option("offset", offset)
keys_only = self._option("keys_only", keys_only)

# Except in the case of ancestor queries, we always use the client's database
database = context_module.get_context().client.database or None

if ancestor is not None:
if isinstance(ancestor, ParameterizedThing):
if isinstance(ancestor, ParameterizedFunction):
Expand All @@ -1402,6 +1402,9 @@ def __init__(
raise TypeError("ancestor/project id mismatch")
else:
project = ancestor.app()

database = ancestor.database()

if namespace is not None:
# if namespace is the empty string, that means default
# namespace, but after a put, if the ancestor is using
Expand All @@ -1413,6 +1416,7 @@ def __init__(
raise TypeError("ancestor/namespace mismatch")
else:
namespace = ancestor.namespace()

if filters is not None:
if not isinstance(filters, Node):
raise TypeError(
Expand Down Expand Up @@ -1483,8 +1487,6 @@ def __repr__(self):
args = []
if self.project is not None:
args.append("project=%r" % self.project)
if self.database is not None:
args.append("database=%r" % self.database)
if self.namespace is not None:
args.append("namespace=%r" % self.namespace)
if self.kind is not None:
Expand Down Expand Up @@ -1555,7 +1557,6 @@ def filter(self, *filters):
filters=new_filters,
order_by=self.order_by,
project=self.project,
database=self.database,
namespace=self.namespace,
default_options=self.default_options,
projection=self.projection,
Expand Down Expand Up @@ -1589,7 +1590,6 @@ def order(self, *props):
filters=self.filters,
order_by=order_by,
project=self.project,
database=self.database,
namespace=self.namespace,
default_options=self.default_options,
projection=self.projection,
Expand Down Expand Up @@ -1671,7 +1671,6 @@ def bind(self, *positional, **keyword):
filters=filters,
order_by=self.order_by,
project=self.project,
database=self.database,
namespace=self.namespace,
default_options=self.default_options,
projection=self.projection,
Expand Down
2 changes: 2 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ def system(session, use_named_db):
if use_named_db and os.environ.get("RUN_NAMED_DB_TESTS", "false") == "false":
session.skip("RUN_NAMED_DB_TESTS is set to false, skipping")

os.environ["IS_NAMED_DB_TEST"] = str(use_named_db)

system_test_exists = os.path.exists(system_test_path)
system_test_folder_exists = os.path.exists(system_test_folder_path)
# Sanity check: only run tests if found.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def main():
readme = readme_file.read()
dependencies = [
"google-api-core[grpc] >= 1.34.0, <3.0.0dev,!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,!=2.10.*",
"google-cloud-datastore @ git+https://github.com/googleapis/python-datastore@multi-db",
"google-cloud-datastore >= 2.16.0, < 3.0.0dev",
"protobuf >= 3.19.5, <5.0.0dev,!=3.20.0,!=3.20.1,!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5",
"pymemcache >= 2.1.0, < 5.0.0dev",
"redis >= 3.0.0, < 5.0.0dev",
Expand Down
2 changes: 1 addition & 1 deletion testing/constraints-3.7.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#
# e.g., if setup.py has "foo >= 1.14.0, < 2.0.0dev",
# Then this file should have foo==1.14.0
google-cloud-datastore @ git+https://github.com/googleapis/python-datastore@multi-db
google-cloud-datastore==2.16.0
google-api-core==1.34.0
protobuf==3.19.5
pymemcache==2.1.0
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def context_factory():
def context(**kwargs):
client = mock.Mock(
project="testing",
database="",
database=None,
namespace=None,
spec=("project", "database", "namespace"),
stub=mock.Mock(spec=()),
Expand Down
Loading