Skip to content

Commit 4a211c0

Browse files
authored
Merge branch 'main' into 168_usage_migrations
2 parents c33ab11 + bfbb4f7 commit 4a211c0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1429
-399
lines changed

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@
196196
"github_url": "https://github.com/litestar-org/sqlspec",
197197
"discord_url": "https://discord.gg/dSDXd4mKhp",
198198
"navigation_with_keys": True,
199-
"globaltoc_expand_depth": 2,
199+
"globaltoc_expand_depth": 0,
200200
"light_logo": "_static/logo-default.png",
201201
"dark_logo": "_static/logo-default.png",
202202
"discussion_url": "https://discord.gg/dSDXd4mKhp",
4.42 KB
Binary file not shown.

docs/examples/quickstart/quickstart_4.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ class User(BaseModel):
2525
async with db_manager.provide_session(db) as session:
2626
await session.execute(
2727
"""
28-
CREATE TABLE users (id INTEGER, name TEXT, email TEXT)
28+
CREATE TABLE if not exists users (id INTEGER, name TEXT, email TEXT)
2929
"""
3030
)
31-
await session.execute("INSERT INTO users VALUES (?, ?, ?)", 1, "Alice", "[email protected]")
32-
user = await session.select_one("SELECT * FROM users WHERE id = ?", 1, schema_type=User)
31+
await session.execute("INSERT INTO users VALUES (?, ?, ?)", 100, "Alice", "[email protected]")
32+
user = await session.select_one("SELECT * FROM users WHERE id = ?", 100, schema_type=User)
3333
print(f"User: {user.name}")
3434
# end-example
3535

36-
assert user == User(id=1, name="Alice", email="[email protected]")
36+
assert user == User(id=100, name="Alice", email="[email protected]")

docs/examples/quickstart/quickstart_6.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
__all__ = ("test_quickstart_6",)
22

33

4-
def test_quickstart_6() -> None:
4+
from pathlib import Path
5+
6+
7+
def test_quickstart_6(tmp_path: Path) -> None:
58
# start-example
69
from sqlspec import SQLSpec
710
from sqlspec.adapters.duckdb import DuckDBConfig
811
from sqlspec.adapters.sqlite import SqliteConfig
912

13+
app_db = tmp_path / "app.db"
14+
analytics_db = tmp_path / "analytics.duckdb"
15+
1016
db_manager = SQLSpec()
11-
sqlite_db = db_manager.add_config(SqliteConfig(pool_config={"database": "app.db"}))
12-
duckdb_db = db_manager.add_config(DuckDBConfig(pool_config={"database": "analytics.duckdb"}))
17+
sqlite_db = db_manager.add_config(SqliteConfig(pool_config={"database": app_db.name}))
18+
duckdb_db = db_manager.add_config(DuckDBConfig(pool_config={"database": analytics_db.name}))
1319

1420
with db_manager.provide_session(sqlite_db) as sqlite_session:
1521
users = sqlite_session.select("SELECT 1")

docs/examples/usage/conftest.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
from __future__ import annotations
22

3+
import os
34
from collections.abc import Generator
45

56
import pytest
67
from pytest_databases.docker.postgres import PostgresService
78

8-
pytest_plugins = ["pytest_databases.docker.postgres"]
9+
pytest_plugins = [
10+
"pytest_databases.docker.postgres",
11+
"pytest_databases.docker.mysql",
12+
"pytest_databases.docker.oracle",
13+
"pytest_databases.docker.bigquery",
14+
]
915

1016

1117
@pytest.fixture(scope="session", autouse=True)
1218
def usage_postgres_env(postgres_service: PostgresService) -> Generator[None, None, None]:
1319
"""Expose Postgres connection settings via env vars for docs examples."""
1420

21+
os.environ
1522
patcher = pytest.MonkeyPatch()
1623
dsn = (
1724
f"postgresql://{postgres_service.user}:{postgres_service.password}"
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
__all__ = ("test_manual_pool",)
22

33

4-
def test_manual_pool() -> None:
4+
async def test_manual_pool() -> None:
55

66
# start-example
77
import os
@@ -10,9 +10,10 @@ def test_manual_pool() -> None:
1010

1111
from sqlspec.adapters.asyncpg import AsyncpgConfig
1212

13-
pool = asyncpg.create_pool(
13+
pool = await asyncpg.create_pool(
1414
dsn=os.getenv("SQLSPEC_USAGE_PG_DSN", "postgresql://localhost/db"), min_size=10, max_size=20
1515
)
1616
db = AsyncpgConfig(pool_instance=pool)
1717
# end-example
1818
assert db.pool_instance is pool
19+
await pool.close()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Test module converted from docs example - code-block 1
2+
"""Minimal smoke test for drivers_and_querying example 1."""
3+
4+
import os
5+
6+
from pytest_databases.docker.postgres import PostgresService
7+
8+
__all__ = ("test_importable_1",)
9+
10+
11+
async def test_importable_1(postgres_service: PostgresService) -> None:
12+
# start-example
13+
from sqlspec import SQLSpec
14+
from sqlspec.adapters.asyncpg import AsyncpgConfig, AsyncpgPoolConfig
15+
16+
# Typical driver usage
17+
spec = SQLSpec()
18+
host = os.environ.get("SQLSPEC_USAGE_PG_HOST", "localhost")
19+
port = int(os.environ.get("SQLSPEC_USAGE_PG_PORT", "5432"))
20+
user = os.environ.get("SQLSPEC_USAGE_PG_USER", "postgres")
21+
password = os.environ.get("SQLSPEC_USAGE_PG_PASSWORD", "postgres")
22+
database = os.environ.get("SQLSPEC_USAGE_PG_DATABASE", "sqlspec")
23+
24+
db = spec.add_config(
25+
AsyncpgConfig(
26+
pool_config=AsyncpgPoolConfig(host=host, port=port, user=user, password=password, database=database)
27+
)
28+
) # Config layer, registers pool
29+
async with spec.provide_session(db) as session: # Session layer
30+
await session.execute("SELECT 1") # Driver layer
31+
# end-example
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Test module converted from docs example - code-block 10
2+
"""Minimal smoke test for drivers_and_querying example 10."""
3+
4+
from pathlib import Path
5+
6+
__all__ = ("test_example_10_duckdb_config",)
7+
8+
9+
def test_example_10_duckdb_config() -> None:
10+
# start-example
11+
from sqlspec import SQLSpec
12+
from sqlspec.adapters.duckdb import DuckDBConfig
13+
14+
spec = SQLSpec()
15+
# In-memory
16+
config = DuckDBConfig()
17+
18+
# Persistent
19+
config = DuckDBConfig(pool_config={"database": "analytics.duckdb"})
20+
21+
with spec.provide_session(config) as session:
22+
# Create table from Parquet
23+
session.execute(f"""
24+
CREATE TABLE if not exists users AS
25+
SELECT * FROM read_parquet('{Path(__file__).parent.parent / "queries/users.parquet"}')
26+
""")
27+
28+
# Analytical query
29+
session.execute("""
30+
SELECT date_trunc('day', created_at) as day,
31+
count(*) as user_count
32+
FROM users
33+
GROUP BY day
34+
ORDER BY day
35+
""")
36+
# end-example
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Test module converted from docs example - code-block 11
2+
"""Minimal smoke test for drivers_and_querying example 11."""
3+
4+
from pytest_databases.docker.oracle import OracleService
5+
6+
__all__ = ("test_example_11_oracledb_config",)
7+
8+
9+
def test_example_11_oracledb_config(oracle_service: OracleService) -> None:
10+
# start-example
11+
from sqlspec import SQLSpec
12+
from sqlspec.adapters.oracledb import OracleSyncConfig
13+
14+
spec = SQLSpec()
15+
config = OracleSyncConfig(
16+
pool_config={
17+
"user": oracle_service.user,
18+
"password": oracle_service.password,
19+
"host": oracle_service.host,
20+
"port": oracle_service.port,
21+
"service_name": oracle_service.service_name,
22+
}
23+
)
24+
25+
with spec.provide_session(config) as session:
26+
create_table_sql = """CREATE TABLE if not exists employees (
27+
employee_id NUMBER PRIMARY KEY,
28+
first_name VARCHAR2(50),
29+
last_name VARCHAR2(50)
30+
)"""
31+
session.execute(create_table_sql)
32+
session.execute("""
33+
INSERT INTO employees (employee_id, first_name, last_name) VALUES (100, 'John', 'Doe')
34+
""")
35+
36+
session.execute("SELECT * FROM employees WHERE employee_id = :id", id=100)
37+
# end-example
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Test module converted from docs example - code-block 12
2+
"""Minimal smoke test for drivers_and_querying example 12."""
3+
4+
import pytest
5+
6+
from sqlspec.adapters.bigquery.driver import BigQueryDriver
7+
8+
__all__ = ("test_example_12_bigquery_config",)
9+
10+
11+
@pytest.mark.skip(reason="Requires BigQuery emulator setup with more complex configuration.")
12+
def test_example_12_bigquery_config(bigquery_service: BigQueryDriver) -> None:
13+
# start-example
14+
import datetime
15+
16+
from google.api_core.client_options import ClientOptions
17+
from google.auth.credentials import AnonymousCredentials
18+
19+
from sqlspec import SQLSpec
20+
from sqlspec.adapters.bigquery.config import BigQueryConfig
21+
22+
config = BigQueryConfig(
23+
connection_config={
24+
"project": bigquery_service.project,
25+
"dataset_id": bigquery_service.dataset,
26+
"client_options": ClientOptions(api_endpoint=f"http://{bigquery_service.host}:{bigquery_service.port}"),
27+
"credentials": AnonymousCredentials(), # type: ignore[no-untyped-call]
28+
}
29+
)
30+
spec = SQLSpec()
31+
with spec.provide_session(config) as bigquery_session:
32+
bigquery_session.execute("SELECT 1 AS value")
33+
34+
# Create the test table
35+
36+
create_table_query = """
37+
CREATE or replace TABLE events (
38+
timestamp TIMESTAMP,
39+
event_type STRING
40+
)
41+
"""
42+
bigquery_session.execute_script(create_table_query)
43+
44+
print("Executing test query...")
45+
bigquery_session.execute(
46+
"""
47+
SELECT DATE(timestamp) as date,
48+
COUNT(*) as events
49+
FROM events
50+
WHERE timestamp >= @start_date
51+
GROUP BY date
52+
""",
53+
start_date=datetime.date(2025, 1, 1),
54+
)
55+
# end-example

0 commit comments

Comments
 (0)