-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update CLI to use async connection to DB (#3450)
# Description This PR updates the CLI, so an async connection to the DB is used in the commands instead of a sync one. As the rest of the parts of the application are using the async connection, the code for the sync connection has been removed. The list of changes of this PR are: - Add `AsyncTyper` class which allows to register `async` command with its method `async_command`. It executes then executes the command using `asyncio.run`. - Add `run` function that allows to execute an `async` command. - Remove `database_async_url` property from `Settings` class. From now on, `database_url` should have an URL with an async driver (`sqlite+aiosqlite://` or `postgres+asyncpg://`). - Update Alembic connection to create an async engine to run the migrations. **Type of change** - [x] New feature (non-breaking change which adds functionality) - [x] Breaking change (fix or feature that would cause existing functionality to not work as expected) **How Has This Been Tested** - [x] All the unit tests regarding the CLI are working as expected - [x] All the commands have been executed locally and working as expected (using both `python -m argilla users create ...` and `python -m argilla.tasks.users.create ...`) - [x] Migrations have been applied without errors in a local environment to both SQLite and PostgreSQL instances. **Checklist** - [ ] I added relevant documentation - [x] follows the style guidelines of this project - [x] I did a self-review of my code - [ ] I made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [ ] I filled out [the contributor form](https://tally.so/r/n9XrxK) (see text above) - [x] I have added relevant notes to the CHANGELOG.md file (See https://keepachangelog.com/) --------- Co-authored-by: Francisco Aranda <[email protected]>
- Loading branch information
1 parent
af60012
commit ef1eb16
Showing
25 changed files
with
283 additions
and
218 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 was deleted.
Oops, something went wrong.
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,57 @@ | ||
# Copyright 2021-present, the Recognai S.L. team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import asyncio | ||
import sys | ||
from functools import wraps | ||
from typing import Any, Callable, Coroutine, TypeVar | ||
|
||
import typer | ||
|
||
if sys.version_info < (3, 10): | ||
from typing_extensions import ParamSpec | ||
else: | ||
from typing import ParamSpec | ||
|
||
|
||
P = ParamSpec("P") | ||
R = TypeVar("R") | ||
|
||
|
||
# https://github.com/tiangolo/typer/issues/88#issuecomment-1613013597 | ||
class AsyncTyper(typer.Typer): | ||
def command( | ||
self, *args: Any, **kwargs: Any | ||
) -> Callable[[Callable[P, Coroutine[Any, Any, R]]], Callable[P, Coroutine[Any, Any, R]]]: | ||
super_command = super().command(*args, **kwargs) | ||
|
||
def decorator(func: Callable[P, Coroutine[Any, Any, R]]) -> Callable[P, Coroutine[Any, Any, R]]: | ||
@wraps(func) | ||
def sync_func(*_args: P.args, **_kwargs: P.kwargs) -> R: | ||
return asyncio.run(func(*_args, **_kwargs)) | ||
|
||
if asyncio.iscoroutinefunction(func): | ||
super_command(sync_func) | ||
else: | ||
super_command(func) | ||
|
||
return func | ||
|
||
return decorator | ||
|
||
|
||
def run(function: Callable[..., Coroutine[Any, Any, Any]]) -> None: | ||
app = AsyncTyper(add_completion=False) | ||
app.command()(function) | ||
app() |
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
Oops, something went wrong.