Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Added support for asyncpg as an optional engine #125

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion create_aio_app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def main():
'no_input': True,
'extra_context': {
'project_name': args.get('name'),
'use_postgres': 'n' if args.get('without_postgres') else 'y',
'use_postgres': 'n' if args.get('without_postgres') else args['postgres_engine'], # noqa
'use_redis': 'y' if args.get('redis') else 'n',
},
}
Expand Down
2 changes: 1 addition & 1 deletion create_aio_app/template/cookiecutter.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"project_name": "app",
"use_postgres": "y",
"use_postgres": ["aiopg", "asyncpg", "n"],
"use_redis": "n"
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ test: lint
@docker-compose up test
@docker-compose stop test

{%- if cookiecutter.use_postgres == 'y' %}
{%- if cookiecutter.use_postgres != 'n' %}
# Database

psql:
Expand All @@ -61,11 +61,11 @@ migrate:
run_production:
@docker-compose -f docker-compose.yml -f docker-compose.production.yml up

adev: {%- if cookiecutter.use_postgres == 'y' or cookiecutter.use_redis == 'y' %} wait_resources {% endif %}
adev: {%- if cookiecutter.use_postgres != 'n' or cookiecutter.use_redis == 'y' %} wait_resources {% endif %}
adev runserver ./$(PROJECT_NAME)/__main__.py -p 8080


{%- if cookiecutter.use_postgres == 'y' or cookiecutter.use_redis == 'y' %}
{%- if cookiecutter.use_postgres != 'n' or cookiecutter.use_redis == 'y' %}
wait_resources:
python3 -m $(PROJECT_NAME).utils.wait_script
{%- endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ___
- docker-compose
- aio devtools
- aiohttp debug toolbar
{%- if cookiecutter.use_postgres == 'y' %}
{%- if cookiecutter.use_postgres != 'n' %}
- postgres
- alembic
- aiopg
Expand Down Expand Up @@ -85,7 +85,7 @@ make mypy
The all settings connected with a `mypy` you can customize in `mypy.ini`.
___

{%- if cookiecutter.use_postgres == 'y' %}
{%- if cookiecutter.use_postgres != 'n' %}

### Testing
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
app:
host: 0.0.0.0
port: 8000
{%- if cookiecutter.use_postgres == 'y' %}
{%- if cookiecutter.use_postgres != 'n' %}

# postgres
postgres:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
app:
host: 0.0.0.0
port: 8080
{%- if cookiecutter.use_postgres == 'y' %}
{%- if cookiecutter.use_postgres != 'n' %}

# postgres
postgres:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
app:
host: 0.0.0.0
port: 9000
{%- if cookiecutter.use_postgres == 'y' %}
{%- if cookiecutter.use_postgres != 'n' %}

# postgres
postgres:
Expand Down
28 changes: 18 additions & 10 deletions create_aio_app/template/{{cookiecutter.project_name}}/conftest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import pytest
{%- if cookiecutter.use_postgres == 'y' %}
{%- if cookiecutter.use_postgres == 'aiopg' %}
from sqlalchemy import create_engine
import aiopg.sa
{%- endif %}

from {{ cookiecutter.project_name }}.utils.common import PATH, get_config
from {{ cookiecutter.project_name }}.app import init_app
{%- if cookiecutter.use_postgres == 'y' %}
from {{ cookiecutter.project_name }}.migrations import metadata
from {{ cookiecutter.project_name }}.users.tables import users
from {{cookiecutter.project_name}}.utils.common import PATH, get_config
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the whole project uses {{ cookiecutter.project_name }} with indent, so don't change that, please.
In jinja2 documentation also use indents

{% for user in users %}
  <li><a href="{{ user.url }}">{{ user.username }}</a></li>
{% endfor %}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry autoformatting, my bad.

from {{cookiecutter.project_name}}.app import init_app
{%- if cookiecutter.use_postgres != 'n' %}
from {{cookiecutter.project_name}}.migrations import metadata
from {{cookiecutter.project_name}}.users.tables import users
{%- endif %}


Expand All @@ -20,10 +20,9 @@
test_config = get_config(['-c', TEST_CONFIG_PATH.as_posix()])


{%- if cookiecutter.use_postgres == 'y' %}
{%- if cookiecutter.use_postgres != 'n' %}
# helpers


def get_db_url(config: dict) -> str:
'''
Generate a url for db connection from the config.
Expand Down Expand Up @@ -58,7 +57,7 @@ def init_sample_data(engine) -> None:
'username': f'test#{idx}',
'email': f'test#{idx}',
'password': f'{idx}'} for idx in range(10)
])
])

conn.execute(query)

Expand Down Expand Up @@ -138,11 +137,20 @@ async def sa_engine(loop):
'''

return await aiopg.sa.create_engine(**test_config['postgres'])


@pytest.fixture
async def postgres_engine(loop):
{%- if cookiecutter.use_postgres == 'aiopg' %}
import asyncpg
return await asyncpg.connect(**test_config['postgres'])
{%- endif %}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that for compatibiliti with tests u should use asyncpgsa and just rewrite sa_engine some like that:

from sqlalchemy import create_engine

...

return await create_engine(dsn, echo=True, module=asyncpgsa)

what do u think?

{%- endif %}


@pytest.fixture
async def client(aiohttp_client{% if cookiecutter.use_postgres == 'y' %}, tables{% endif %}):
async def client(aiohttp_client {% if cookiecutter.use_postgres != 'n' %} , tables{% endif %}):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did u add indent here? this code generate

async def client(aiohttp_client  , tables): 
   ...

right?

'''
The fixture for the initialize client.
'''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ services:
restart: always
volumes:
- .:/app:delegated
{%- if cookiecutter.use_postgres == 'y' and cookiecutter.use_redis == 'y' %}
{%- if cookiecutter.use_postgres != 'n' and cookiecutter.use_redis == 'y' %}
depends_on:
- postgres
- redis
{%- elif cookiecutter.use_postgres == 'y' %}
{%- elif cookiecutter.use_postgres != 'n' %}
depends_on:
- postgres
{%- elif cookiecutter.use_redis == 'y' %}
Expand All @@ -32,7 +32,7 @@ services:
- 8080:8080
- 8081:8081
command: make adev
{%- if cookiecutter.use_postgres == 'y' %}
{%- if cookiecutter.use_postgres != 'n' %}

postgres:
image: postgres:10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ aiohttp==3.5.4
aiohttp_jinja2==1.1.0
trafaret_config==2.0.2
markdown2==2.3.7
{%- if cookiecutter.use_postgres == 'y' %}
{%- if cookiecutter.use_postgres == 'aiopg' %}
aiopg[sa]==0.16.0
psycopg2-binary==2.7.7
{%- elif cookiecutter.use_postgres == 'asyncpg' %}
asyncpg
{%- endif %}
{%- if cookiecutter.use_postgres != 'n' %}
alembic==1.0.7
{%- endif %}
{%- if cookiecutter.use_redis == 'y' %}
aioredis==1.2.0
{%- endif %}
{%- endif %}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[flake8]
max-line-length = 79
exclude = env/* {% if cookiecutter.use_postgres == 'y' %}migrations/*{% endif %}
exclude = env/* {% if cookiecutter.use_postgres != 'n' %}migrations/*{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
{%- endif %}

import aiohttp_jinja2
{%- if cookiecutter.use_postgres == 'y' %}
{%- if cookiecutter.use_postgres == 'asyncpg' %}
import asyncpg
{%- endif %}
{%- if cookiecutter.use_postgres == 'aiopg' %}
import aiopg.sa
{%- endif %}
from aiohttp import web
Expand All @@ -30,7 +33,7 @@ def init_jinja2(app: web.Application) -> None:
app,
loader=jinja2.FileSystemLoader(str(path / 'templates'))
)
{%- if cookiecutter.use_postgres == 'y' %}
{%- if cookiecutter.use_postgres != 'n' %}


async def database(app: web.Application) -> None:
Expand All @@ -40,7 +43,11 @@ async def database(app: web.Application) -> None:
'''
config = app['config']['postgres']

{%- if cookiecutter.use_postgres == 'aiopg' %}
engine = await aiopg.sa.create_engine(**config)
{%- elif cookiecutter.use_postgres == 'asyncpg' %}
engine = await asyncpg.connect(**config)
{%- endif %}
app['db'] = engine

yield
Expand Down Expand Up @@ -86,13 +93,13 @@ def init_app(config: Optional[List[str]] = None) -> web.Application:
init_jinja2(app)
init_config(app, config=config)
init_routes(app)
{%- if cookiecutter.use_postgres == 'y' and cookiecutter.use_redis == 'y' %}
{%- if cookiecutter.use_postgres != 'n' and cookiecutter.use_redis == 'y' %}

app.cleanup_ctx.extend([
redis,
database,
])
{%- elif cookiecutter.use_postgres == 'y' %}
{%- elif cookiecutter.use_postgres != 'n' %}

app.cleanup_ctx.extend([
database,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{%- if cookiecutter.use_postgres == 'y' %}
{%- if cookiecutter.use_postgres == 'aiopg' %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Types are important, so u need create types for asyncpg

from typing import List

from aiopg.sa.result import RowProxy


RowsProxy = List[RowProxy]
{% endif %}
{%- endif %}
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
{%- if cookiecutter.use_postgres == 'asyncpg' %}
import asyncpg
{%- endif %}
{%- if cookiecutter.use_postgres == 'aiopg' %}
from aiopg.sa import SAConnection
from aiopg.sa.result import RowProxy
{%- endif %}
from {{cookiecutter.project_name}}.users.tables import users{%- if cookiecutter.use_postgres == 'asyncpg' %}, dialect {%- endif %}

from {{ cookiecutter.project_name }}.users.tables import users
__all__ = ['select_user_by_id', 'select_user_by_id_query']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍



__all__ = ['select_user_by_id', ]


async def select_user_by_id(conn: SAConnection, key: int) -> RowProxy:
def select_user_by_id_query(key):
query = users\
.select()\
.where(users.c.id == key)\
.order_by(users.c.id)
cursor = await conn.execute(query)

return query


{%- if cookiecutter.use_postgres == 'asyncpg' %}
async def select_user_by_id(conn, key):
query = str(select_user_by_id_query(key).compile(dialect=dialect))
result = await conn.fetchrow(query)
return result
{%- endif %}
{%- if cookiecutter.use_postgres == 'aiopg' %}
async def select_user_by_id(conn: SAConnection, key: int) -> RowProxy:
cursor = await conn.execute(select_user_by_id_query(key))
return await cursor.fetchone()
{%- endif %}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from {{ cookiecutter.project_name }}.migrations import metadata
from {{ cookiecutter.project_name }}.users.enums import UserGender
from {{cookiecutter.project_name}}.migrations import metadata
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indents

from {{cookiecutter.project_name}}.users.enums import UserGender


__all__ = ['users', 'gender_enum', ]
__all__ = ['users', 'gender_enum', 'dialect', ]


gender_enum = postgresql.ENUM(UserGender)

dialect = postgresql.dialect()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this part need only for asyncpg, so use if statement


users = sa.Table(
'users', metadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'host': trafaret.String(),
'port': trafaret.Int(),
}),
{%- if cookiecutter.use_postgres == 'y' %}
{%- if cookiecutter.use_postgres != 'n' %}
trafaret.Key('postgres'):
trafaret.Dict({
'host': trafaret.String(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

if __name__ == '__main__':
config = get_config(['-c', DEFAULT_CONFIG_PATH.as_posix()])
{%- if cookiecutter.use_postgres == 'y' %}
{%- if cookiecutter.use_postgres != 'n' %}
postgres = config['postgres']
while True:
try:
Expand Down
6 changes: 6 additions & 0 deletions create_aio_app/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,11 @@ def parse_arguments() -> dict:
action='store_true',
help='generate project without postgres settings'
)
parser.add_argument(
'--postgres_engine',
default='aiopg',
choices=['aiopg', 'asyncpg'],
help='choose which postgres engine to use',
)

return parser.parse_args().__dict__