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

Add naming convention #720

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
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ docs/_build/
# PyBuilder
target/

# mypy
.mypy_cache/
.dmypy.json
dmypy.json


# pyenv python configuration file
.python-version

Expand All @@ -73,3 +79,6 @@ target/

# extension stub files
src/gino/ext/*.pyi

# virtual env
venv/
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'python-gino'
html_theme = "python-gino"

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
Expand Down
14 changes: 13 additions & 1 deletion src/gino/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,15 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
await self._args[0].pop_bind().close()


NAMING_CONVENTION = {
"ix": "ix_%(column_0_label)s",
"uq": "uq_%(table_name)s_%(column_0_name)s",
"ck": "ck_%(table_name)s_%(constraint_name)s",
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
"pk": "pk_%(table_name)s",
}


class Gino(sa.MetaData):
"""
All-in-one API class of GINO, providing several shortcuts.
Expand Down Expand Up @@ -355,7 +364,10 @@ def __init__(
:class:`~sqlalchemy.schema.MetaData`.

"""
super().__init__(bind=bind, **kwargs)
preset_kwargs = dict(naming_convention=NAMING_CONVENTION, bind=bind)
preset_kwargs.update(kwargs)
super().__init__(**preset_kwargs)

if model_classes is None:
model_classes = self.model_base_classes
self._model = declarative_base(self, model_classes)
Expand Down
17 changes: 12 additions & 5 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
import enum
import random
import string
import uuid
from datetime import datetime

import pytest

from gino import Gino
from gino.dialects.asyncpg import JSONB
from sqlalchemy.dialects.postgresql import UUID


DB_ARGS = dict(
host=os.getenv("DB_HOST", "localhost"),
Expand Down Expand Up @@ -46,9 +49,9 @@ class User(db.Model):
balance = db.IntegerProperty(default=0)
birthday = db.DateTimeProperty(default=lambda i: datetime.utcfromtimestamp(0))
team_id = db.Column(db.ForeignKey("gino_teams.id"))
weight = db.IntegerProperty(prop_name='parameter')
height = db.IntegerProperty(default=170, prop_name='parameter')
bio = db.StringProperty(prop_name='parameter')
weight = db.IntegerProperty(prop_name="parameter")
height = db.IntegerProperty(default=170, prop_name="parameter")
bio = db.StringProperty(prop_name="parameter")

@balance.after_get
def balance(self, val):
Expand Down Expand Up @@ -134,19 +137,23 @@ def add_team(self, team):
class UserSetting(db.Model):
__tablename__ = "gino_user_settings"

# No constraints defined on columns
# No constraints defined on these columns
id = db.Column(db.BigInteger())
user_id = db.Column(db.BigInteger())
setting = db.Column(db.Text())
value = db.Column(db.Text())
col1 = db.Column(db.Integer, default=1)
col2 = db.Column(db.Integer, default=2)

# Some constraints defined on these columns
col3 = db.Column(UUID, default=uuid.uuid4, unique=True)
col4 = db.Column(db.Integer, default=4, nullable=False)

# Define indexes and constraints inline
id_pkey = db.PrimaryKeyConstraint("id")
user_id_fk = db.ForeignKeyConstraint(["user_id"], ["gino_users.id"])
user_id_setting_unique = db.UniqueConstraint("user_id", "setting")
col1_check = db.CheckConstraint("col1 >= 1 AND col1 <= 5")
col1_check = db.CheckConstraint("col1 >= 1 AND col1 <= 5", "check_col1")
col2_idx = db.Index("col2_idx", "col2")


Expand Down
24 changes: 12 additions & 12 deletions tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,21 +411,21 @@ async def test_repr():
from gino.dialects.asyncpg import NullPool

e = await create_engine(PG_URL, pool_class=NullPool)
assert 'cur=0' in repr(e)
assert "cur=0" in repr(e)
async with e.acquire():
assert 'cur=1' in repr(e)
assert "cur=1" in repr(e)
async with e.acquire():
assert 'cur=2' in repr(e)
assert 'cur=1' in repr(e)
assert 'cur=0' in repr(e)
assert 'NullPool' in e.repr(color=True)
assert "cur=2" in repr(e)
assert "cur=1" in repr(e)
assert "cur=0" in repr(e)
assert "NullPool" in e.repr(color=True)

e = await create_engine(PG_URL)
assert 'cur=10 use=0' in repr(e)
assert "cur=10 use=0" in repr(e)
async with e.acquire():
assert 'cur=10 use=1' in repr(e)
assert "cur=10 use=1" in repr(e)
async with e.acquire():
assert 'cur=10 use=2' in repr(e)
assert 'cur=10 use=1' in repr(e)
assert 'cur=10 use=0' in repr(e)
assert 'asyncpg.pool.Pool' in e.repr(color=True)
assert "cur=10 use=2" in repr(e)
assert "cur=10 use=1" in repr(e)
assert "cur=10 use=0" in repr(e)
assert "asyncpg.pool.Pool" in e.repr(color=True)