+
Skip to content

add UUID field #105

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

Merged
merged 5 commits into from
Sep 8, 2021
Merged
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
19 changes: 17 additions & 2 deletions .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,20 @@ jobs:

strategy:
matrix:
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10.0-rc.1"]
python-version: ["3.6", "3.7", "3.8", "3.9"]

services:
mysql:
image: mysql:5.7
env:
MYSQL_USER: username
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: testsuite
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

postgres:
image: postgres:10.8
env:
Expand All @@ -38,9 +49,13 @@ jobs:
run: "scripts/check"
- name: "Build package & docs"
run: "scripts/build"
- name: "Run tests"
- name: "Run tests with PostgreSQL"
env:
TEST_DATABASE_URL: "postgresql://username:password@localhost:5432/testsuite"
run: "scripts/test"
- name: "Run tests with MySQL"
env:
TEST_DATABASE_URL: "mysql://username:password@localhost:3306/testsuite"
run: "scripts/test"
- name: "Enforce coverage"
run: "scripts/coverage"
1 change: 1 addition & 0 deletions docs/declaring_models.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ See `TypeSystem` for [type-specific validation keyword arguments][typesystem-fie
* `orm.String(max_length)`
* `orm.Text()`
* `orm.Time()`
* `orm.UUID()`
* `orm.JSON()`

[psycopg2]: https://www.psycopg.org/
Expand Down
4 changes: 3 additions & 1 deletion orm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from orm.exceptions import MultipleMatches, NoMatch
from orm.fields import (
JSON,
UUID,
BigInteger,
Boolean,
Date,
Expand Down Expand Up @@ -28,10 +29,11 @@
"Enum",
"Float",
"Integer",
"JSON",
"String",
"Text",
"Time",
"JSON",
"UUID",
"ForeignKey",
"Model",
]
7 changes: 7 additions & 0 deletions orm/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import sqlalchemy
import typesystem

from orm.sqlalchemy_fields import GUID


class ModelField:
def __init__(
Expand Down Expand Up @@ -138,3 +140,8 @@ def __init__(self, max_digits: int, decimal_places: int, **kwargs):

def get_column_type(self):
return sqlalchemy.Numeric(precision=self.max_digits, scale=self.decimal_places)


class UUID(ModelField, typesystem.UUID):
def get_column_type(self):
return GUID()
36 changes: 36 additions & 0 deletions orm/sqlalchemy_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import uuid

import sqlalchemy


class GUID(sqlalchemy.TypeDecorator):
"""Platform-independent GUID type.

Uses PostgreSQL's UUID type, otherwise uses
CHAR(32), storing as stringified hex values.
"""

impl = sqlalchemy.CHAR
cache_ok = True

def load_dialect_impl(self, dialect):
if dialect.name == "postgresql":
return dialect.type_descriptor(sqlalchemy.dialects.postgresql.UUID())
else:
return dialect.type_descriptor(sqlalchemy.CHAR(32))

def process_bind_param(self, value, dialect):
if value is None:
return value
elif dialect.name == "postgresql":
return str(value)
else:
return "%.32x" % value.int

def process_result_value(self, value, dialect):
if value is None:
return value
else:
if not isinstance(value, uuid.UUID):
value = uuid.UUID(value)
return value
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
databases[postgresql]
psycopg2
databases[postgresql, mysql]
psycopg2-binary
pymysql
typesystem

# Packaging
Expand Down
2 changes: 1 addition & 1 deletion scripts/test
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if [ -z $GITHUB_ACTIONS ]; then
scripts/check
fi

${PREFIX}coverage run -m pytest $@
${PREFIX}coverage run -a -m pytest $@

if [ -z $GITHUB_ACTIONS ]; then
scripts/coverage
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ xfail_strict=True
filterwarnings=
# Turn warnings that aren't filtered into exceptions
error
ignore::DeprecationWarning

[coverage:run]
source_pkgs = orm, tests
14 changes: 13 additions & 1 deletion tests/test_columns.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import decimal
import uuid
from enum import Enum

import databases
Expand Down Expand Up @@ -30,6 +31,7 @@ class Example(orm.Model):
__database__ = database

id = orm.Integer(primary_key=True)
uuid = orm.UUID(allow_null=True)
huge_number = orm.BigInteger(default=9223372036854775807)
created = orm.DateTime(default=datetime.datetime.now)
created_day = orm.Date(default=datetime.date.today)
Expand All @@ -43,7 +45,13 @@ class Example(orm.Model):

@pytest.fixture(autouse=True, scope="module")
def create_test_database():
engine = sqlalchemy.create_engine(DATABASE_URL)
database_url = databases.DatabaseURL(DATABASE_URL)
if database_url.scheme == "mysql":
url = str(database_url.replace(driver="pymysql"))
else:
url = str(database_url)

engine = sqlalchemy.create_engine(url)
metadata.create_all(engine)
yield
metadata.drop_all(engine)
Expand All @@ -62,15 +70,19 @@ async def test_model_crud():
assert example.price is None
assert example.data == {}
assert example.status == StatusEnum.DRAFT
assert example.uuid is None

await example.update(
data={"foo": 123},
value=123.456,
status=StatusEnum.RELEASED,
price=decimal.Decimal("999.99"),
uuid=uuid.UUID("01175cde-c18f-4a13-a492-21bd9e1cb01b"),
)

example = await Example.objects.get()
assert example.value == 123.456
assert example.data == {"foo": 123}
assert example.status == StatusEnum.RELEASED
assert example.price == decimal.Decimal("999.99")
assert example.uuid == uuid.UUID("01175cde-c18f-4a13-a492-21bd9e1cb01b")
8 changes: 7 additions & 1 deletion tests/test_foreignkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ class Member(orm.Model):

@pytest.fixture(autouse=True, scope="module")
def create_test_database():
engine = sqlalchemy.create_engine(DATABASE_URL)
database_url = databases.DatabaseURL(DATABASE_URL)
if database_url.scheme == "mysql":
url = str(database_url.replace(driver="pymysql"))
else:
url = str(database_url)

engine = sqlalchemy.create_engine(url)
metadata.create_all(engine)
yield
metadata.drop_all(engine)
Expand Down
8 changes: 7 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ class Product(orm.Model):

@pytest.fixture(autouse=True, scope="module")
def create_test_database():
engine = sqlalchemy.create_engine(DATABASE_URL)
database_url = databases.DatabaseURL(DATABASE_URL)
if database_url.scheme == "mysql":
url = str(database_url.replace(driver="pymysql"))
else:
url = str(database_url)

engine = sqlalchemy.create_engine(url)
metadata.create_all(engine)
yield
metadata.drop_all(engine)
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载