An extension for GINO to support Starlette server.

Overview

gino-starlette

Codacy Badge

Introduction

An extension for GINO to support starlette server.

Usage

The common usage looks like this:

from starlette.applications import Starlette
from gino.ext.starlette import Gino

app = Starlette()
db = Gino(app, **kwargs)

Configuration

GINO adds a middleware to the Starlette app to setup and cleanup database according to the configurations that passed in the kwargs parameter.

The config includes:

Name Description Default
driver the database driver asyncpg
host database server host localhost
port database server port 5432
user database server user postgres
password database server password empty
database database name postgres
dsn a SQLAlchemy database URL to create the engine, its existence will replace all previous connect arguments. N/A
retry_times the retry times when database failed to connect 20
retry_interval the interval in seconds between each time of retry 5
pool_min_size the initial number of connections of the db pool. N/A
pool_max_size the maximum number of connections in the db pool. N/A
echo enable SQLAlchemy echo mode. N/A
ssl SSL context passed to asyncpg.connect None
use_connection_for_request flag to set up lazy connection for requests. N/A
retry_limit the number of retries to connect to the database on start up. 1
retry_interval seconds to wait between retries. 1
kwargs other parameters passed to the specified dialects, like asyncpg. Unrecognized parameters will cause exceptions. N/A

Lazy Connection

If use_connection_for_request is set to be True, then a lazy connection is available at request['connection']. By default, a database connection is borrowed on the first query, shared in the same execution context, and returned to the pool on response. If you need to release the connection early in the middle to do some long-running tasks, you can simply do this:

await request['connection'].release(permanent=False)
Comments
  • gino.exceptions.UninitializedError: Gino engine is not initialized.

    gino.exceptions.UninitializedError: Gino engine is not initialized.

    Steps to Reproduce the Problem

    1. gino.exceptions.UninitializedError: Gino engine is not initialized. image

    2. view image 3 settings image

    3. model image

    Specifications

    • Python version: 3.6.5
    • GINO version:0.8.5
    • Starlette version:0.13.1
    opened by begyy 11
  • Prod FastAPI Demo Dockerfile doesn't build image

    Prod FastAPI Demo Dockerfile doesn't build image

    I try the prod fastAPI demo example on my own machine.

    Expected Behavior

    Demo run with docker compose

    Actual Behavior

    Image doesn't build correctly and docker compose cannot up.

    Steps to Reproduce the Problem

    1. Cloning the repo
    2. Go to prod_fastapi_demo folder
    3. run docker compose command
    git clone https://github.com/python-gino/gino-starlette
    cd gino-starlette/examples/prod_fastapi_demo
    docker-compose up
    

    Specifications

    • Python version: 3.8
    • GINO version: na
    • Starlette version: na
    • WSL 2 (Ubuntu 20.04 LTS)
    • Docker : 20.10.2, build 2291f61
    • Docker Compose: 1.29.2, build 5becea4c

    2021-07-30 08_21_11- _fastapi_demo

    opened by atzisb-kozak 9
  • Upgrade Starlette version

    Upgrade Starlette version

    Expected Behavior

    No dependency conflict

    Actual Behavior

    New Fastapi version causes dependency versions conflict

    Steps to Reproduce the Problem

    1. Try to freeze dependencies with Fastapi >= 0.76.0 and gino-starlette
    2. Get an error (pip-tools for example) image

    Btw, there are newer versions of starlette: 0.20.* https://github.com/tiangolo/fastapi/pull/4820 https://github.com/tiangolo/fastapi/pull/4936 So, if you just change ^0.17 to ^0.19, it wont work for long

    Specifications

    • Python version: 3.10
    • GINO version: 1.0.1
    • Starlette version: 0.19.1
    opened by zoola969 4
  • Support for Starlette 0.14

    Support for Starlette 0.14

    This PR addresses #20. gino-starlette currently does not support starlette version 0.14.x This PR simply updates the dependencies in pyproject.toml

    I uploaded a build to test.pypi.org for further testing. Works for me as is. https://test.pypi.org/project/gino-starlette/

    opened by thomas-maschler 3
  • Use mypy to type check a project

    Use mypy to type check a project

    Expected Behavior

    Mypy should know the types from gino-starlette to check our creations against it

    Actual Behavior

    Mypy warns about it. Cannot infer the types because they are not declared

    Steps to Reproduce the Problem

    1. Clone sample project: https://github.com/python-gino/gino-starlette/tree/master/examples/prod_fastapi_demo
    2. Install mypy
    3. Execute mypy
    mypy src/
    src/gino_fastapi_demo/models/__init__.py:1: error: Skipping analyzing 'gino_starlette': found module but no type hints or library stubs
    src/gino_fastapi_demo/models/__init__.py:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
    Found 1 error in 1 file (checked 8 source files)
    

    Specifications

    • Python version: 3.9.1
    • GINO version: 1.0.1
    • Starlette version: 0.13.6
    • gino-starlette: 0.1.1

    Solution

    Maybe we can do a package with the needed stubs like the one for sqlalchemy on pypi

    I've generated a sample with the instructions provided at mypy documentation

    from typing import Any, Optional
    
    from fastapi.applications import FastAPI
    from gino.api import Gino as _Gino
    from gino.api import GinoExecutor as _Executor
    from gino.engine import GinoConnection as _Connection
    from gino.engine import GinoEngine as _Engine
    from gino.strategies import GinoStrategy
    from starlette.types import Receive as Receive
    from starlette.types import Scope as Scope
    from starlette.types import Send as Send
    
    logger: Any
    
    class StarletteModelMixin:
        @classmethod
        async def get_or_404(cls, *args: Any, **kwargs: Any): ...
    
    class GinoExecutor(_Executor):
        async def first_or_404(self, *args: Any, **kwargs: Any): ...
    
    class GinoConnection(_Connection):
        async def first_or_404(self, *args: Any, **kwargs: Any): ...
    
    class GinoEngine(_Engine):
        connection_cls: Any = ...
        async def first_or_404(self, *args: Any, **kwargs: Any): ...
    
    class StarletteStrategy(GinoStrategy):
        name: str = ...
        engine_cls: Any = ...
    
    class _Middleware:
        app: Any = ...
        db: Any = ...
        def __init__(self, app: Any, db: Any) -> None: ...
        async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: ...
    
    class Gino(_Gino):
        model_base_classes: Any = ...
        query_executor: Any = ...
        config: Any = ...
        def __init__(
            self, app: Optional[FastAPI] = ..., *args: Any, **kwargs: Any
        ) -> None: ...
        def init_app(self, app: FastAPI) -> None: ...
        async def first_or_404(self, *args: Any, **kwargs: Any): ...
        async def set_bind(self, bind: Any, loop: Optional[Any] = ..., **kwargs: Any): ...
    
    opened by paxet 2
  • pip install gino-starlette does not work

    pip install gino-starlette does not work

    Expected Behavior

    Describe what you were trying to get done and what you expected to happen.

    Ran pip install gino-starlette and expected it to install gino-starlette

    Actual Behavior

    pip can not find the package

    $ pip install gino-starlette Collecting gino-starlette Could not find a version that satisfies the requirement gino-starlette (from versions: ) No matching distribution found for gino-starlette

    Steps to Reproduce the Problem

    Collecting gino-starlette
      Could not find a version that satisfies the requirement gino-starlette (from versions: )
    No matching distribution found for gino-starlette
    

    Can confirm that it can connect to pypi:

    $ pip install fastapi Collecting fastapi Downloading https://files.pythonhosted.org/packages/f9/eb/f899ccd1f052bb306d3938696458a403ee61a116fd784bc9fb266f6ce211/fastapi-0.53.0-py3-none-any.whl (47kB) 100% |████████████████████████████████| 51kB 916kB/s

    Specifications

    • Python version: 3.8.1
    • GINO version: n/a
    • Starlette version: n/a
    opened by ltieman 2
  • Please Bump Starlette Version

    Please Bump Starlette Version

    Expected Behavior

    No dependency conflicts with FastAPI

    Actual Behavior

    Newer FastAPI versions have incompatible Starlette version (72.0+)

    Steps to Reproduce the Problem

    1. Install FastAPI 72.0 or newer
    2. See dependencies version error

    image image

    Specifications

    • Python version: ^3.9
    • GINO version: n/a
    • Starlette version: ^0.17.1
    • FastAPI version: ^72.0
    opened by hamie96 1
  • Please, bump Starlette version

    Please, bump Starlette version

    Expected Behavior

    No dependency conflicts with FastAPI

    Actual Behavior

    Newer FastAPI versions have incompatible Starlette version

    Steps to Reproduce the Problem

    1. Install FastAPI 0.69+ via pipenv
    2. See dependencies version error
    $ pipenv install
    [pipenv.exceptions.ResolutionFailure]: Warning: Your dependencies could not be resolved. You likely have a mismatch in your sub-dependencies.
      First try clearing your dependency cache with $ pipenv lock --clear, then try the original command again.
     Alternatively, you can use $ pipenv install --skip-lock to bypass this mechanism, then run $ pipenv graph to inspect the situation.
      Hint: try $ pipenv lock --pre if it is a pre-release dependency.
    ERROR: Could not find a version that matches starlette<0.15.0,==0.16.0,>=0.13.0 (from -r /var/folders/7y/z1m2c48104b74g3qwbsjr5sc0000gq/T/pipenvwmto7eg4requirements/pipenv-mdqpmq80-constraints.txt (line 7))
    Tried: 0.1.0, 0.1.1, 0.1.2, 0.1.3, 0.1.4, 0.1.5, 0.1.6, 0.1.7, 0.1.8, 0.1.9, 0.1.10, 0.1.11, 0.1.12, 0.1.13, 0.1.14, 0.1.15, 0.1.16, 0.1.17, 0.2.0, 0.2.1, 0.2.2, 0.2.3, 0.3.0, 0.3.1, 0.3.2, 0.3.3, 0.3.4, 0.3.5, 0.3.6, 0.3.7, 0.4.0, 0.4.1, 0.4.2, 0.5.0, 0.5.1, 0.5.2, 0.5.3, 0.5.4, 0.5.5, 0.6.0, 0.6.1, 0.6.2, 0.6.3, 0.7.0, 0.7.1, 0.7.2, 0.7.3, 0.7.4, 0.8.0, 0.8.1, 0.8.2, 0.8.3, 0.8.4, 0.8.5, 0.8.6, 0.8.7, 0.8.8, 0.9.0, 0.9.1, 0.9.2, 0.9.3, 0.9.4, 0.9.5, 0.9.6, 0.9.7, 0.9.8, 0.9.9, 0.9.10, 0.9.11, 0.10.0, 0.10.1, 0.10.2, 0.10.3, 0.10.4, 0.10.5, 0.10.6, 0.10.7, 0.11.0, 0.11.1, 0.11.2, 0.11.3, 0.11.4, 0.12.0, 0.12.1, 0.12.2, 0.12.3, 0.12.4, 0.12.5, 0.12.6, 0.12.7, 0.12.8, 0.12.9, 0.12.10, 0.12.11, 0.12.12, 0.12.13, 0.13.0, 0.13.1, 0.13.1, 0.13.2, 0.13.2, 0.13.3, 0.13.3, 0.13.4, 0.13.4, 0.13.5, 0.13.5, 0.13.6, 0.13.6, 0.13.7, 0.13.7, 0.13.8, 0.13.8, 0.14.0, 0.14.0, 0.14.1, 0.14.1, 0.14.2, 0.14.2, 0.15.0, 0.15.0, 0.16.0, 0.16.0, 0.17.0, 0.17.0
    Skipped pre-versions: 0.12.0b1, 0.12.0b2, 0.12.0b3
    There are incompatible versions in the resolved dependencies:
      starlette (from -r /var/folders/7y/z1m2c48104b74g3qwbsjr5sc0000gq/T/pipenvwmto7eg4requirements/pipenv-mdqpmq80-constraints.txt (line 7))
      starlette<0.15.0,>=0.13.0 (from gino-starlette==0.1.2->-r /var/folders/7y/z1m2c48104b74g3qwbsjr5sc0000gq/T/pipenvwmto7eg4requirements/pipenv-mdqpmq80-constraints.txt (line 4))
      starlette==0.16.0 (from fastapi==0.70.0->-r /var/folders/7y/z1m2c48104b74g3qwbsjr5sc0000gq/T/pipenvwmto7eg4requirements/pipenv-mdqpmq80-constraints.txt (line 3))
    

    Specifications

    • Python version: 3.9.7
    • GINO version: na
    • Starlette version: na
    opened by devalv 1
  • [Security] Bump py from 1.8.1 to 1.10.0

    [Security] Bump py from 1.8.1 to 1.10.0

    Bumps py from 1.8.1 to 1.10.0. This update includes a security fix.

    Vulnerabilities fixed

    Sourced from The GitHub Security Advisory Database.

    Regular expression deinal of service in py A denial of service via regular expression in the py.path.svnwc component of py (aka python-py) through 1.9.0 could be used by attackers to cause a compute-time denial of service attack by supplying malicious input to the blame functionality.

    Affected versions: < 1.10.0

    Changelog

    Sourced from py's changelog.

    1.10.0 (2020-12-12)

    • Fix a regular expression DoS vulnerability in the py.path.svnwc SVN blame functionality (CVE-2020-29651)
    • Update vendored apipkg: 1.4 => 1.5
    • Update vendored iniconfig: 1.0.0 => 1.1.1

    1.9.0 (2020-06-24)

    • Add type annotation stubs for the following modules:

      • py.error
      • py.iniconfig
      • py.path (not including SVN paths)
      • py.io
      • py.xml

      There are no plans to type other modules at this time.

      The type annotations are provided in external .pyi files, not inline in the code, and may therefore contain small errors or omissions. If you use py in conjunction with a type checker, and encounter any type errors you believe should be accepted, please report it in an issue.

    1.8.2 (2020-06-15)

    • On Windows, py.path.locals which differ only in case now have the same Python hash value. Previously, such paths were considered equal but had different hashes, which is not allowed and breaks the assumptions made by dicts, sets and other users of hashes.
    Commits
    • e5ff378 Update CHANGELOG for 1.10.0
    • 94cf44f Update vendored libs
    • 5e8ded5 testing: comment out an assert which fails on Python 3.9 for now
    • afdffcc Rename HOWTORELEASE.rst to RELEASING.rst
    • 2de53a6 Merge pull request #266 from nicoddemus/gh-actions
    • fa1b32e Merge pull request #264 from hugovk/patch-2
    • 887d6b8 Skip test_samefile_symlink on pypy3 on Windows
    • e94e670 Fix test_comments() in test_source
    • fef9a32 Adapt test
    • 4a694b0 Add GitHub Actions badge to README
    • Additional commits viewable in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language
    • @dependabot badge me will comment on this PR with code to add a "Dependabot enabled" badge to your readme

    Additionally, you can set the following in your Dependabot dashboard:

    • Update frequency (including time of day and day of week)
    • Pull request limits (per update run and/or open at any time)
    • Out-of-range updates (receive only lockfile updates, if desired)
    • Security updates (receive only security updates, if desired)
    dependencies security 
    opened by dependabot-preview[bot] 1
  • [Security] Bump asyncpg from 0.20.1 to 0.21.0

    [Security] Bump asyncpg from 0.20.1 to 0.21.0

    Bumps asyncpg from 0.20.1 to 0.21.0. This update includes a security fix.

    Vulnerabilities fixed

    Sourced from The GitHub Security Advisory Database.

    Access of Uninitialized Pointer asyncpg before 0.21.0 allows a malicious PostgreSQL server to trigger a crash or execute arbitrary code (on a database client) via a crafted server response, because of access to an uninitialized pointer in the array data decoder.

    Affected versions: < 0.21.0

    Release notes

    Sourced from asyncpg's releases.

    asyncpg v0.21.0

    Improvements

    • Add support for password functions (useful for RDS IAM auth) (#554) (by Harvey Frye in 1d9457f0 for #554)

    • Add support for connection termination listeners (#525) (by @iomintz in 8141b93c for #525)

    • Update CI matrix, aarch64 builds (#595) (by @Gelbpunkt in ac6a2fcf for #595)

    Fixes

    • Fix possible uninitalized pointer access on unexpected array message data (CVE-2020-17446, by @elprans in 69bcdf5b, reported by @risicle)

    • Fix Connection class _copy_in private method (by @ABCDeath in 7f5c2a24 for #555)

    • Bump pgproto to fix compilation issues (by @elprans in aa67d61b for #565)

    • Improve pool documentation examples (#491) (by @nyurik in 745f8f81 for #491)

    • Update usage.rst (#572) (by @xuedong09 in f5b425ae for #572)

    • Fix links in connection documentation (#584) (by @samuelcolvin in b0813204 for #584)

    • Fix usage documentation for hstore (#515) (by @aaliddell in 39040b3c for #515)

    • Fix compiler warnings (by @elprans in 6cb5ba19)

    Commits

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language
    • @dependabot badge me will comment on this PR with code to add a "Dependabot enabled" badge to your readme

    Additionally, you can set the following in your Dependabot dashboard:

    • Update frequency (including time of day and day of week)
    • Pull request limits (per update run and/or open at any time)
    • Out-of-range updates (receive only lockfile updates, if desired)
    • Security updates (receive only security updates, if desired)
    dependencies security 
    opened by dependabot-preview[bot] 1
  • Bump starlette from 0.13.3 to 0.13.8

    Bump starlette from 0.13.3 to 0.13.8

    Bumps starlette from 0.13.3 to 0.13.8.

    Release notes

    Sourced from starlette's releases.

    Version 0.13.8

    • Revert Queue(maxsize=1) fix for BaseHTTPMiddleware middleware classes and streaming responses.

    • The StaticFiles constructor now allows pathlib.Path in addition to strings for its directory argument.

    Version 0.13.7

    • Fix high memory usage when using BaseHTTPMiddleware middleware classes and streaming responses.

    Version 0.13.6

    • Fix 404 errors with StaticFiles.

    Version 0.13.5

    0.13.5

    • Add support for Starlette(lifespan=...) functions.
    • More robust path-traversal check in StaticFiles app.
    • Fix WSGI PATH_INFO encoding.
    • RedirectResponse now accepts optional background parameter
    • Allow path routes to contain regex meta characters
    • Treat ASGI HTTP 'body' as an optional key.
    • Don't use thread pooling for writing to in-memory upload files.

    Version 0.13.4

    • Add UUID convertor. #903
    • More lenient cookie parsing. #900
    Changelog

    Sourced from starlette's changelog.

    0.13.8

    • Revert Queue(maxsize=1) fix for BaseHTTPMiddleware middleware classes and streaming responses.

    • The StaticFiles constructor now allows pathlib.Path in addition to strings for its directory argument.

    0.13.7

    • Fix high memory usage when using BaseHTTPMiddleware middleware classes and streaming responses.

    0.13.6

    • Fix 404 errors with StaticFiles.

    0.13.5

    • Add support for Starlette(lifespan=...) functions.
    • More robust path-traversal check in StaticFiles app.
    • Fix WSGI PATH_INFO encoding.
    • RedirectResponse now accepts optional background parameter
    • Allow path routes to contain regex meta characters
    • Treat ASGI HTTP 'body' as an optional key.
    • Don't use thread pooling for writing to in-memory upload files.

    0.13.0

    • Switch to promoting application configuration on init style everywhere. This means dropping the decorator style in favour of declarative routing tables and middleware definitions.

    0.12.12

    • Fix request.url_for() for the Mount-within-a-Mount case.

    0.12.11

    • Fix request.url_for() when an ASGI root_path is being used.

    0.12.1

    • Add URL.include_query_params(**kwargs)
    • Add URL.replace_query_params(**kwargs)
    • Add URL.remove_query_params(param_names)
    • request.state properly persisting across middleware.
    • Added request.scope interface.

    0.12.0

    • Switch to ASGI 3.0.
    • Fixes to CORS middleware.
    Commits

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Note: This repo was added to Dependabot recently, so you'll receive a maximum of 5 PRs for your first few update runs. Once an update run creates fewer than 5 PRs we'll remove that limit.

    You can always request more updates by clicking Bump now in your Dependabot dashboard.

    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language
    • @dependabot badge me will comment on this PR with code to add a "Dependabot enabled" badge to your readme

    Additionally, you can set the following in your Dependabot dashboard:

    • Update frequency (including time of day and day of week)
    • Pull request limits (per update run and/or open at any time)
    • Out-of-range updates (receive only lockfile updates, if desired)
    • Security updates (receive only security updates, if desired)
    dependencies 
    opened by dependabot-preview[bot] 1
  • Can I use Gino[starlette] with SQLite?

    Can I use Gino[starlette] with SQLite?

    Tests using SQLite

    Hi all, I'm trying to make the Gino mock, but I keep seeing the error gino.exceptions.UninitializedError: Gino engine is not initialized.

    Code

    My code is formed like this:

    # __init__.py
    @functools.lru_cache
    def get_db_service():
        db = Gino(dsn=settings.get_settings().postgresql_conn_url)
    
        return db
    
    # model 
    
    _db = get_db_service()
    
    class EdaTableInstance(_db.Model):
        __tablename__ = "eda_table_instance"
    #...
    
       @classmethod
        async def get_all(cls) -> List['EdaTableInstance']:
            async with _db.acquire():
                  return await EdaTableInstance.query.gino.all()
    

    Now let's see how I'm writing the tests (various attempts)

    # conftest.py
    @pytest.fixture(autouse=True)
    def mock_get_db_service(mocker):
        db = Gino(dsn="sqlite//:memory:")
        async_mock = AsyncMock(db)
        mocker.patch("gateway_api.services.get_db_service", return_value=async_mock)
        yield
    

    or

    # conftest.py
    
    @pytest.fixture
    async def db_initialize():
       await db.set_bind('sqlite:///:memory:')
       await db.gino.create_all()
       await EdaTableInstance.create_eda_table_instance(
           EdaTableInstanceInputOnCreate({"name":"table_server1", "host":"123.123.123.123"})
       )
       yield
    

    or

    # test_models.py
    @pytest.fixture
    def mock_gino_get_all(mocker):
        mocker.patch("gino.api.GinoExecutor.all", return_value=[])
    
    @pytest.mark.asyncio
    @pytest.mark.parametrize("id, expected", [(None, None)])
    async def test_01_table_instance_get_all(id, expected):
        mock_cursor = MagicMock()
        mock_cursor.configure_mock(
            **{
                "get_one.return_value":[id]
            }
        )
        res = await EdaTableInstance().get_one(mock_cursor)
        assert res == expected
    
    

    I would like to use SqLite in memory, so I don't have to connect from a database, if you know better methods to mock the database, it would help me so much. Thank you.

    Specifications

    • Python version: 3.9.5
    • GINO version: 1.0.1
    • Gino Starlette: 0.1.5
    • Starlette version: 0.16.0
    opened by Plaoo 0
  • Cannot release connection after query is canceled.

    Cannot release connection after query is canceled.

    Expected Behavior

    When canceling query, connection should be released back into pool.

    Actual Behavior

    After canceling query (and catching errors in code) gino_startlette stills throws error asyncpg.exceptions.QueryCanceledError when trying to release connection.

    Steps to Reproduce the Problem

    from asyncpg import QueryCanceledError
    from fastapi import FastAPI
    from gino_starlette import Gino
    
    app = FastAPI()
    
    db = Gino(
        app,
        kwargs=dict(server_settings=dict(statement_timeout="1")), # cancel query after 1 ms
    )
    
    
    @app.get("/")
    async def root():
        message = "success"
        try:
            await db.scalar("SELECT 1")
        except QueryCanceledError:
            message = "timeout"
    
        return {"message": message}
    

    When called, Endpoint will correctly return {"message": "timeout"} but app will still throw QueryCanceledError

    Traceback (most recent call last):
      File "****/site-packages/uvicorn/protocols/http/httptools_impl.py", line 385, in run_asgi
        result = await app(self.scope, self.receive, self.send)
      File "****/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
        return await self.app(scope, receive, send)
      File "****/site-packages/fastapi/applications.py", line 181, in __call__
        await super().__call__(scope, receive, send)
      File "****/site-packages/starlette/applications.py", line 102, in __call__
        await self.middleware_stack(scope, receive, send)
      File "****/site-packages/starlette/middleware/errors.py", line 181, in __call__
        raise exc from None
      File "****/site-packages/starlette/middleware/errors.py", line 159, in __call__
        await self.app(scope, receive, _send)
      File "****/site-packages/gino_starlette.py", line 83, in __call__
        await conn.release()
      File "****/site-packages/gino/engine.py", line 300, in release
        await dbapi_conn.release(True)
      File "****/site-packages/gino/engine.py", line 48, in release
        return await self._release()
      File "****/site-packages/gino/engine.py", line 84, in _release
        await self._pool.release(conn)
      File "****/site-packages/gino/dialects/asyncpg.py", line 280, in release
        await self._pool.release(conn)
      File "****/site-packages/asyncpg/pool.py", line 654, in release
        return await asyncio.shield(ch.release(timeout))
      File "****/site-packages/asyncpg/pool.py", line 216, in release
        raise ex
      File "****/site-packages/asyncpg/pool.py", line 206, in release
        await self._con.reset(timeout=budget)
      File "****/site-packages/asyncpg/connection.py", line 1137, in reset
        await self.execute(reset_query, timeout=timeout)
      File "****/site-packages/asyncpg/connection.py", line 295, in execute
        return await self._protocol.query(query, timeout)
      File "asyncpg/protocol/protocol.pyx", line 316, in query
    asyncpg.exceptions.QueryCanceledError: canceling statement due to statement timeout
    

    Specifications

    • Python version: 3.8
    • GINO version: 1.0.1
    • Starlette version: 0.1.1

    Workarounds

    I can suppress the error message when replacing this line https://github.com/python-gino/gino-starlette/blob/master/src/gino_starlette.py#L83 with this:

    try:
        await conn.release()
    except QueryCanceledError:
        pass
    

    But I am not quite sure if this is the best approach

    opened by thomas-maschler 0
  • Cannot connect to the database; max retries reached.

    Cannot connect to the database; max retries reached.

    Expected Behavior

    Describe what you were trying to get done and what you expected to happen. Cannot connect to the database; max retries reached. ERROR: Traceback (most recent call last): File "/home/dilshod/Desktop/dev_appointment/my_env/lib/python3.7/site-packages/starlette/routing.py", line 526, in lifespan async for item in self.lifespan_context(app): File "/home/dilshod/Desktop/dev_appointment/my_env/lib/python3.7/site-packages/starlette/routing.py", line 467, in default_lifespan await self.startup() File "/home/dilshod/Desktop/dev_appointment/my_env/lib/python3.7/site-packages/starlette/routing.py", line 502, in startup await handler() File "/home/dilshod/Desktop/dev_appointment/my_env/lib/python3.7/site-packages/gino_starlette.py", line 183, in startup **config["kwargs"], File "/home/dilshod/Desktop/dev_appointment/my_env/lib/python3.7/site-packages/gino_starlette.py", line 226, in set_bind return await super().set_bind(bind, loop=loop, **kwargs) File "/home/dilshod/Desktop/dev_appointment/my_env/lib/python3.7/site-packages/gino/api.py", line 417, in set_bind bind = await create_engine(bind, loop=loop, **kwargs) File "/home/dilshod/Desktop/dev_appointment/my_env/lib/python3.7/site-packages/gino/strategies.py", line 55, in create pool = await dialect.init_pool(u, loop, pool_class=pool_class) File "/home/dilshod/Desktop/dev_appointment/my_env/lib/python3.7/site-packages/gino/dialects/asyncpg.py", line 465, in init_pool return await pool_class(url, loop, init=self.on_connect(), **self._pool_kwargs) File "/home/dilshod/Desktop/dev_appointment/my_env/lib/python3.7/site-packages/gino/dialects/asyncpg.py", line 218, in _init self.pool = await asyncpg.create_pool(**args) File "/home/dilshod/Desktop/dev_appointment/my_env/lib/python3.7/site-packages/asyncpg/pool.py", line 398, in async__init await self._initialize() File "/home/dilshod/Desktop/dev_appointment/my_env/lib/python3.7/site-packages/asyncpg/pool.py", line 426, in _initialize await first_ch.connect() File "/home/dilshod/Desktop/dev_appointment/my_env/lib/python3.7/site-packages/asyncpg/pool.py", line 125, in connect self._con = await self._pool._get_new_connection() File "/home/dilshod/Desktop/dev_appointment/my_env/lib/python3.7/site-packages/asyncpg/pool.py", line 472, in _get_new_connection **self._connect_kwargs) File "/home/dilshod/Desktop/dev_appointment/my_env/lib/python3.7/site-packages/asyncpg/connection.py", line 1727, in connect max_cacheable_statement_size=max_cacheable_statement_size) File "/home/dilshod/Desktop/dev_appointment/my_env/lib/python3.7/site-packages/asyncpg/connect_utils.py", line 674, in _connect raise last_error File "/home/dilshod/Desktop/dev_appointment/my_env/lib/python3.7/site-packages/asyncpg/connect_utils.py", line 666, in _connect connection_class=connection_class) File "/home/dilshod/Desktop/dev_appointment/my_env/lib/python3.7/site-packages/asyncpg/connect_utils.py", line 633, in _connect_addr connector, timeout=timeout) File "/usr/local/lib/python3.7/asyncio/tasks.py", line 412, in wait_for return fut.result() File "uvloop/loop.pyx", line 1914, in create_connection socket.gaierror: [Errno -2] Name or service not known

    ERROR: Application startup failed. Exiting.

    I could not connect my database.

    opened by bakhtiyorovdilshod 1
  • DB connection for request is not released when there are more than one database

    DB connection for request is not released when there are more than one database

    With use_connection_for_request=True and two DBs:

    db1.init_app(app)
    db2.init_app(app)
    

    And in the fastapi router, query db1 and db2.

    1. Call the api
    2. Change file contents to make it reload
    3. Try exiting

    Then you'll see the server gets stuck.

    opened by kigawas 1
  • Failing to run with Alembic with FastAPI

    Failing to run with Alembic with FastAPI

    Expected Behavior

    I am trying to run the alembic make migrations and expected to succeed

    Actual Behavior

    exception is raised

    Steps to Reproduce the Problem

    1. clone https://github.com/ahmednafies/fastapi_gino.git
    2. run make makemigrations
    command
    `alembic revision --autogenerate`
    
    error
    AttributeError: type object 'AsyncpgDBAPI' has no attribute 'connect'
    

    Specifications

    • Python version: 3.8.3
    • GINO version: 1.0.1
    • Starlette version: 0.13.4
    opened by nf1s 4
Htmdf - html to pdf with support for variables using fastApi.

htmdf Converts html to pdf with support for variables using fastApi. Installation Clone this repository. git clone https://github.com/ShreehariVaasish

Shreehari 1 Jan 30, 2022
asgi-server-timing-middleware

ASGI Server-Timing middleware An ASGI middleware that wraps the excellent yappi profiler to let you measure the execution time of any function or coro

null 33 Dec 15, 2022
JSON-RPC server based on fastapi

Description JSON-RPC server based on fastapi: https://fastapi.tiangolo.com Motivation Autogenerated OpenAPI and Swagger (thanks to fastapi) for JSON-R

null 199 Dec 30, 2022
A Jupyter server based on FastAPI (Experimental)

jupyverse is experimental and should not be used in place of jupyter-server, which is the official Jupyter server.

Jupyter Server 122 Dec 27, 2022
OpenAPI generated FastAPI server

OpenAPI generated FastAPI server This Python package is automatically generated by the OpenAPI Generator project: API version: 1.0.0 Build package: or

microbo 1 Oct 31, 2021
Mnist API server w/ FastAPI

Mnist API server w/ FastAPI

Jinwoo Park (Curt) 8 Feb 8, 2022
FastAPI Server Session is a dependency-based extension for FastAPI that adds support for server-sided session management

FastAPI Server-sided Session FastAPI Server Session is a dependency-based extension for FastAPI that adds support for server-sided session management.

DevGuyAhnaf 5 Dec 23, 2022
GINO Is Not ORM - a Python asyncio ORM on SQLAlchemy core.

GINO - GINO Is Not ORM - is a lightweight asynchronous ORM built on top of SQLAlchemy core for Python asyncio. GINO 1.0 supports only PostgreSQL with

GINO Community 2.5k Dec 27, 2022
High-performance Async REST API, in Python. FastAPI + GINO + Arq + Uvicorn (w/ Redis and PostgreSQL).

fastapi-gino-arq-uvicorn High-performance Async REST API, in Python. FastAPI + GINO + Arq + Uvicorn (powered by Redis & PostgreSQL). Contents Get Star

Leo Sussan 351 Jan 4, 2023
Opentracing support for Starlette and FastApi

Starlette-OpenTracing OpenTracing support for Starlette and FastApi. Inspired by: Flask-OpenTracing OpenTracing implementations exist for major distri

Rene Dohmen 63 Dec 30, 2022
Opentracing support for Starlette and FastApi

Starlette-OpenTracing OpenTracing support for Starlette and FastApi. Inspired by: Flask-OpenTracing OpenTracing implementations exist for major distri

Rene Dohmen 26 Feb 11, 2021
This is a file deletion program that asks you for an extension of a file (.mp3, .pdf, .docx, etc.) to delete all of the files in a dir that have that extension.

FileBulk This is a file deletion program that asks you for an extension of a file (.mp3, .pdf, .docx, etc.) to delete all of the files in a dir that h

Enoc Mena 1 Jun 26, 2022
Middleware for Starlette that allows you to store and access the context data of a request. Can be used with logging so logs automatically use request headers such as x-request-id or x-correlation-id.

starlette context Middleware for Starlette that allows you to store and access the context data of a request. Can be used with logging so logs automat

Tomasz Wójcik 300 Dec 26, 2022
Prometheus integration for Starlette.

Starlette Prometheus Introduction Prometheus integration for Starlette. Requirements Python 3.6+ Starlette 0.9+ Installation $ pip install starlette-p

José Antonio Perdiguero 229 Dec 21, 2022
Prometheus exporter for Starlette and FastAPI

starlette_exporter Prometheus exporter for Starlette and FastAPI. The middleware collects basic metrics: Counter: starlette_requests_total Histogram:

Steve Hillier 225 Jan 5, 2023
A rate limiter for Starlette and FastAPI

SlowApi A rate limiting library for Starlette and FastAPI adapted from flask-limiter. Note: this is alpha quality code still, the API may change, and

Laurent Savaete 562 Jan 1, 2023
Starlette middleware for Prerender

Prerender Python Starlette Starlette middleware for Prerender Documentation: https://BeeMyDesk.github.io/prerender-python-starlette/ Source Code: http

BeeMyDesk 14 May 2, 2021
Middleware for Starlette that allows you to store and access the context data of a request. Can be used with logging so logs automatically use request headers such as x-request-id or x-correlation-id.

starlette context Middleware for Starlette that allows you to store and access the context data of a request. Can be used with logging so logs automat

Tomasz Wójcik 300 Dec 26, 2022
Prometheus integration for Starlette.

Starlette Prometheus Introduction Prometheus integration for Starlette. Requirements Python 3.6+ Starlette 0.9+ Installation $ pip install starlette-p

José Antonio Perdiguero 229 Dec 21, 2022
Starlette middleware for Prerender

Prerender Python Starlette Starlette middleware for Prerender Documentation: https://BeeMyDesk.github.io/prerender-python-starlette/ Source Code: http

BeeMyDesk 13 Jul 27, 2020