Skip to content

Commit

Permalink
Update README.asyncpg.md
Browse files Browse the repository at this point in the history
  • Loading branch information
gordthompson committed Dec 10, 2024
1 parent 63ccef9 commit 5004128
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
76 changes: 74 additions & 2 deletions README.asyncpg.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,81 @@ There is a customized version of the FastAPI SQL database tutorial for

https://github.com/gordthompson/fastapi-tutorial-cockroachdb-async

### Database support for Alembic
### Default transaction isolation level

CockroachDB version 23.1 or later is required to work with Alembic.
Applications using asyncpg that were developed prior to CockroachDB's inclusion of
READ COMMITTED transaction isolation may operate on the assumption that the default
isolation level will be SERIALIZABLE. For example,

```python
import asyncio

from sqlalchemy.ext.asyncio import create_async_engine


async def async_main():
engine = create_async_engine(
"cockroachdb+asyncpg://root@localhost:26257/defaultdb",
)
async with engine.begin() as conn:
result = await conn.exec_driver_sql("select version()")
print(result.scalar().split("(")[0]) # CockroachDB CCL v23.2.4

result = await conn.exec_driver_sql("show transaction isolation level")
print(result.scalar()) # serializable


asyncio.run(async_main())
```

With current versions of CockroachDB, the default transaction isolation level
**for asyncpg only** is now READ COMMITTED

```python
import asyncio

from sqlalchemy.ext.asyncio import create_async_engine


async def async_main():
engine = create_async_engine(
"cockroachdb+asyncpg://root@localhost:26257/defaultdb",
)
async with engine.begin() as conn:
result = await conn.exec_driver_sql("select version()")
print(result.scalar().split("(")[0]) # CockroachDB CCL v24.3.0

result = await conn.exec_driver_sql("show transaction isolation level")
print(result.scalar()) # read committed


asyncio.run(async_main())
```

Applications that rely on the previous behavior will have to add `isolation_level="SERIALIZABLE"`
to their `create_async_engine()` call

```python
import asyncio

from sqlalchemy.ext.asyncio import create_async_engine


async def async_main():
engine = create_async_engine(
"cockroachdb+asyncpg://root@localhost:26257/defaultdb",
isolation_level="SERIALIZABLE",
)
async with engine.begin() as conn:
result = await conn.exec_driver_sql("select version()")
print(result.scalar().split("(")[0]) # CockroachDB CCL v24.3.0

result = await conn.exec_driver_sql("show transaction isolation level")
print(result.scalar()) # serializable


asyncio.run(async_main())
```

### Testing

Expand Down
3 changes: 2 additions & 1 deletion sqlalchemy_cockroachdb/provision.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
def _cockroachdb_temp_table_keyword_args(cfg, eng):
return {"prefixes": ["TEMPORARY"]}


@update_db_opts.for_db("cockroachdb")
def _update_db_opts(db_url, db_opts, options):
"""Set database options (db_opts) for a test database that we created."""
db_opts['isolation_level'] = 'SERIALIZABLE'
db_opts["isolation_level"] = "SERIALIZABLE"

0 comments on commit 5004128

Please sign in to comment.