-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Support for TTL policies and specifying a `__ttl_field__` in the model classes. - Update dependencies. - Update pre-commit hooks. - Remove support for Python 3.7, which is EOL, now requires 3.8.1 or newer. - Run tests on Python 3.11 in addition to older versions. - Update authors (company was renamed to IOXIO Ltd).
- Loading branch information
1 parent
ccb5404
commit 2bb8159
Showing
20 changed files
with
964 additions
and
552 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from logging import getLogger | ||
from typing import Iterable, List, Optional, Type | ||
|
||
from google.api_core.operation_async import AsyncOperation | ||
from google.cloud.firestore_admin_v1 import Field | ||
from google.cloud.firestore_admin_v1.services.firestore_admin import ( | ||
FirestoreAdminAsyncClient, | ||
) | ||
|
||
from firedantic._async.model import AsyncBareModel | ||
from firedantic.utils import remove_prefix | ||
|
||
logger = getLogger("firedantic") | ||
|
||
|
||
async def set_up_ttl_policies( | ||
gcloud_project: str, | ||
models: Iterable[Type[AsyncBareModel]], | ||
database: str = "(default)", | ||
client: Optional[FirestoreAdminAsyncClient] = None, | ||
) -> List[AsyncOperation]: | ||
""" | ||
Set up TTL policies for models. | ||
:param gcloud_project: The technical name of the project in Google Cloud. | ||
:param models: Models for which to set up the TTL policy. | ||
:param database: The Firestore database instance (it now supports multiple). | ||
:param client: The Firestore admin client. | ||
:return: List of operations that were launched to enable the policies. | ||
""" | ||
if not client: | ||
client = FirestoreAdminAsyncClient() | ||
|
||
operations = [] | ||
for model in models: | ||
if not model.__ttl_field__: | ||
continue | ||
|
||
# Get current details of the field | ||
path = client.field_path( | ||
project=gcloud_project, | ||
database=database, | ||
collection=model.get_collection_name(), | ||
field=model.__ttl_field__, | ||
) | ||
field_obj = await client.get_field({"name": path}) | ||
|
||
# Variables for logging | ||
readable_state = remove_prefix(str(field_obj.ttl_config.state), "State.") | ||
log_str = '"%s", collection: "%s", field: "%s", state: "%s"' | ||
log_params = [ | ||
model.__class__.__name__, | ||
model.get_collection_name(), | ||
model.__ttl_field__, | ||
readable_state, | ||
] | ||
|
||
if field_obj.ttl_config.state == Field.TtlConfig.State.STATE_UNSPECIFIED: | ||
logger.info("Setting up new TTL config: " + log_str, *log_params) | ||
field_obj.ttl_config = Field.TtlConfig( | ||
{"state": Field.TtlConfig.State.CREATING} | ||
) | ||
operation = await client.update_field({"field": field_obj}) | ||
operations.append(operation) | ||
elif field_obj.ttl_config.state == Field.TtlConfig.State.CREATING: | ||
logger.info("TTL config is still being created: " + log_str, *log_params) | ||
elif field_obj.ttl_config.state == Field.TtlConfig.State.NEEDS_REPAIR: | ||
logger.error("TTL config needs repair: " + log_str, *log_params) | ||
elif field_obj.ttl_config.state == Field.TtlConfig.State.ACTIVE: | ||
logger.debug("TTL config is active: " + log_str, *log_params) | ||
|
||
return operations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.