Skip to content

Commit

Permalink
Merge 5fa6230 into 02a52f4
Browse files Browse the repository at this point in the history
  • Loading branch information
long2ice committed Apr 13, 2022
2 parents 02a52f4 + 5fa6230 commit 68be6f5
Show file tree
Hide file tree
Showing 19 changed files with 527 additions and 27 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ up:
@poetry update

deps:
@poetry install -E asyncpg -E aiomysql -E asyncmy -E accel -E psycopg
@poetry install -E asyncpg -E aiomysql -E asyncmy -E accel -E psycopg -E asyncodbc

check: deps build
ifneq ($(shell which black),)
Expand Down Expand Up @@ -58,6 +58,9 @@ test_mysql_myisam:
test_mysql:
$(py_warn) TORTOISE_TEST_DB="mysql://root:$(TORTOISE_MYSQL_PASS)@127.0.0.1:3306/test_\{\}" pytest $(pytest_opts) --cov-append --cov-report=

test_mssql:
$(py_warn) TORTOISE_TEST_DB="mssql://sa:$(TORTOISE_MSSQL_PASS)@127.0.0.1:1433/test_\{\}?driver=${TORTOISE_ODBC_DRIVER}&TrustServerCertificate=YES&autocommit=1" pytest $(pytest_opts) --cov-append --cov-report=

_testall: test_sqlite test_postgres_asyncpg test_postgres_psycopg test_mysql_myisam test_mysql

testall: deps _testall
Expand Down
11 changes: 6 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ You can also install with your db driver (`aiosqlite` is builtin):
pip install tortoise-orm[asyncpg]
Or for MySQL:
For MySQL:

.. code-block:: bash
pip install tortoise-orm[aiomysql]
pip install tortoise-orm[asyncmy]
Or another asyncio MySQL driver `asyncmy <https://github.com/long2ice/asyncmy>`_:
For Microsoft SQL Server (**not fully tested**):

.. code-block:: bash
pip install tortoise-orm[asyncmy]
pip install tortoise-orm[asyncodbc]
Quick Tutorial
--------------
Expand Down Expand Up @@ -152,7 +152,8 @@ Tortoise ORM currently supports the following databases:

* SQLite (requires ``aiosqlite``)
* PostgreSQL (requires ``asyncpg``)
* MySQL (requires ``aiomysql``)
* MySQL (requires ``asyncmy``)
* Microsoft SQL Server (requires ``asyncodbc``)

``generate_schema`` generates the schema on an empty database. Tortoise generates schemas in safe mode by default which
includes the ``IF NOT EXISTS`` clause, so you may include it in your main code.
Expand Down
44 changes: 41 additions & 3 deletions docs/databases.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ Databases

Tortoise currently supports the following databases:

* SQLite
* SQLite (using ``aiosqlite``)
* PostgreSQL >= 9.4 (using ``asyncpg`` or ``psycopg``)
* MySQL/MariaDB (using ``aiomysql``)
* MySQL/MariaDB (using ``asyncmy``)
* Microsoft SQL Server (using ``asyncodbc``)

To use, please ensure that ``asyncpg``, ``psycopg`` and/or ``aiomysql`` is installed.
To use, please ensure that corresponding asyncio driver is installed.

.. _db_url:

Expand Down Expand Up @@ -46,6 +47,8 @@ The supported ``DB_TYPE``:
- ``asyncpg``: :samp:`asyncpg://postgres:pass@db.host:5432/somedb`
``mysql``:
Typically in the form of :samp:`mysql://myuser:mypass@db.host:3306/somedb`
``mssql``:
Typically in the form of :samp:`mssql://myuser:mypass@db.host:1433/somedb?driver=the odbc driver`

Capabilities
============
Expand Down Expand Up @@ -173,6 +176,41 @@ MySQL optional parameters are pass-though parameters to the driver, see `here <h

.. _db_ssl:

MSSQL
=====

DB URL is typically in the form of :samp:`mssql://myuser:mypass@db.host:1433/somedb?driver=the odbc driver`

Required Parameters
-------------------

``user``:
Username to connect with.
``password``:
Password for username.
``host``:
Network host that database is available at.
``port``:
Network port that database is available at. (defaults to ``1433``)
``database``:
Database to use.
``driver``:
The ODBC driver to use.

Optional parameters:
--------------------

MSSQL optional parameters are pass-though parameters to the driver, see `here <https://github.com/tortoise/asyncodbc>`__ for more details.

``minsize`` (defaults to ``1``):
Minimum connection pool size
``maxsize`` (defaults to ``10``):
Maximum connection pool size
``pool_recycle`` (defaults to ``-1``):
Pool recycle timeout in seconds.
``echo`` (defaults to ``False``):
Set to `True`` to echo SQL queries (debug only)

Passing in custom SSL Certificates
==================================

Expand Down
40 changes: 39 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ asyncpg = { version = "*", optional = true }
aiomysql = { version = "*", optional = true }
asyncmy = { version = "^0.2.5", optional = true }
psycopg = { extras = ["pool", "binary"], version = "*", optional = true }
asyncodbc = { version = "*", optional = true }

[tool.poetry.dev-dependencies]
# Linter tools
Expand Down Expand Up @@ -96,6 +97,7 @@ asyncpg = ["asyncpg"]
psycopg = ["psycopg"]
aiomysql = ["aiomysql"]
asyncmy = ["asyncmy"]
asyncodbc = ["asyncodbc"]

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down Expand Up @@ -169,7 +171,7 @@ docstring_style = "sphinx"

[tool.coverage.run]
branch = true
source = "tortoise"
source = ["tortoise"]

[tool.coverage.report]
show_missing = true
1 change: 0 additions & 1 deletion tests/schema/test_generate_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,6 @@ async def test_index_unsafe(self):
async def test_index_safe(self):
await self.init_for("tests.schema.models_postgres_index")
sql = get_schema_sql(connections.get("default"), safe=True)
print(sql)
self.assertEqual(
sql,
"""CREATE TABLE IF NOT EXISTS "index" (
Expand Down
2 changes: 2 additions & 0 deletions tests/test_early_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def test_early_init(self):
"constraints": {"readOnly": True},
"db_field_types": {
"": "TIMESTAMP",
"mssql": "DATETIME",
"mysql": "DATETIME(6)",
"postgres": "TIMESTAMPTZ",
},
Expand Down Expand Up @@ -257,6 +258,7 @@ def test_early_init(self):
"db_column": "created_at",
"db_field_types": {
"": "TIMESTAMP",
"mssql": "DATETIME",
"mysql": "DATETIME(6)",
"postgres": "TIMESTAMPTZ",
},
Expand Down
8 changes: 6 additions & 2 deletions tests/testmodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ class Employee(Model):
team_members: fields.ReverseRelation["Employee"]

talks_to: fields.ManyToManyRelation["Employee"] = fields.ManyToManyField(
"models.Employee", related_name="gets_talked_to"
"models.Employee", related_name="gets_talked_to", on_delete=fields.NO_ACTION
)
gets_talked_to: fields.ManyToManyRelation["Employee"]

Expand Down Expand Up @@ -575,7 +575,10 @@ class StraightFields(Model):
o2o_rev: fields.Field

rel_to: fields.ManyToManyRelation["StraightFields"] = fields.ManyToManyField(
"models.StraightFields", related_name="rel_from", description="M2M to myself"
"models.StraightFields",
related_name="rel_from",
description="M2M to myself",
on_delete=fields.NO_ACTION,
)
rel_from: fields.ManyToManyRelation["StraightFields"]

Expand Down Expand Up @@ -623,6 +626,7 @@ class SourceFields(Model):
forward_key="sts_forward",
backward_key="backward_sts",
description="M2M to myself",
on_delete=fields.NO_ACTION,
)
rel_from: fields.ManyToManyRelation["SourceFields"]

Expand Down
Loading

0 comments on commit 68be6f5

Please sign in to comment.