-
-
Notifications
You must be signed in to change notification settings - Fork 8
docs: 173 usage configuration #234
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
Changes from 22 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
cbd7023
docs: externalise migration CLI and add psycopg literalinclude
euri10 31cbf31
put import in tests and correct the lines accordingly
euri10 5dcc426
lint
euri10 da7a9ad
more correct lines
euri10 0ff5c15
delete
euri10 2d28f08
Revert "delete"
euri10 2ca1def
add leftovers
euri10 a6256a6
fix tests, one not passing when using provide_session by bind_key
euri10 b85c152
linter issues
euri10 642816a
12
euri10 5dd81fa
one test per block
euri10 27364bb
Merge branch 'main' into 173_usage_configuration
euri10 9c7c689
wrong psygopg note, replaced by correct config
euri10 e0b1082
Merge branch '173_usage_configuration' of github.com:euri10/sqlspec i…
euri10 cdca6b3
Merge branch 'main' into 173_usage_configuration
cofin a65c6cb
Merge branch 'main' into 173_usage_configuration
cofin f6d748f
Update docs/examples/usage/test_configuration_12.py
euri10 fffec01
Update docs/examples/usage/test_configuration_13.py
euri10 91c6d0e
Update docs/examples/usage/test_configuration_19.py
euri10 6aec1d2
Merge branch 'main' into 173_usage_configuration
cofin 5c5d60d
Merge branch 'main' into 173_usage_configuration
cofin 8d2c24c
Refactor example configurations and tests
cofin 4629000
ruff
euri10 05a1b8f
Revert "ruff"
euri10 c254d32
ruff
euri10 93154b9
make fix
euri10 2fcd5c1
feat: oracle update
cofin 93250d4
chore: update package versions for pydantic-settings, shibuya, and sp…
cofin 8d7f4e8
Merge branch 'main' into 173_usage_configuration
euri10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Generator | ||
|
|
||
| import pytest | ||
| from pytest_databases.docker.postgres import PostgresService | ||
|
|
||
| pytest_plugins = ["pytest_databases.docker.postgres"] | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session", autouse=True) | ||
| def usage_postgres_env(postgres_service: PostgresService) -> Generator[None, None, None]: | ||
| """Expose Postgres connection settings via env vars for docs examples.""" | ||
|
|
||
| patcher = pytest.MonkeyPatch() | ||
| dsn = ( | ||
| f"postgresql://{postgres_service.user}:{postgres_service.password}" | ||
| f"@{postgres_service.host}:{postgres_service.port}/{postgres_service.database}" | ||
| ) | ||
| patcher.setenv("SQLSPEC_USAGE_PG_DSN", dsn) | ||
| patcher.setenv("SQLSPEC_USAGE_PG_HOST", postgres_service.host) | ||
| patcher.setenv("SQLSPEC_USAGE_PG_PORT", str(postgres_service.port)) | ||
| patcher.setenv("SQLSPEC_USAGE_PG_USER", postgres_service.user) | ||
| patcher.setenv("SQLSPEC_USAGE_PG_PASSWORD", postgres_service.password) | ||
| patcher.setenv("SQLSPEC_USAGE_PG_DATABASE", postgres_service.database) | ||
| yield | ||
| patcher.undo() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| def test_sqlite_memory_db() -> None: | ||
| from sqlspec import SQLSpec | ||
| from sqlspec.adapters.sqlite import SqliteConfig | ||
|
|
||
| # Create SQLSpec instance | ||
| db_manager = SQLSpec() | ||
|
|
||
| # Add database configuration | ||
| db = db_manager.add_config(SqliteConfig(pool_config={"database": ":memory:"})) | ||
|
|
||
| # Use the database | ||
| with db_manager.provide_session(db) as session: | ||
| result = session.execute("SELECT 1") | ||
| assert result[0] == {"1": 1} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| POOL_INSTANCE = 20 | ||
|
|
||
|
|
||
| def test_manual_pool() -> None: | ||
| import os | ||
|
|
||
| from sqlspec.adapters.asyncpg import AsyncpgConfig | ||
|
|
||
| dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", "postgresql://localhost/db") | ||
| pool = {"dsn": dsn, "min_size": 10, "max_size": POOL_INSTANCE} | ||
| db = AsyncpgConfig(pool_instance=pool) | ||
| assert db.pool_instance["max_size"] == POOL_INSTANCE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| def test_thread_local_connections() -> None: | ||
| from sqlspec.adapters.sqlite import SqliteConfig | ||
|
|
||
| config = SqliteConfig(pool_config={"database": "test.db"}) | ||
| assert config.pool_config["database"] == "test.db" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| def test_basic_statement_config() -> None: | ||
| import os | ||
|
|
||
| from sqlspec import StatementConfig | ||
| from sqlspec.adapters.asyncpg import AsyncpgConfig | ||
|
|
||
| statement_config = StatementConfig( | ||
| dialect="postgres", # SQLGlot dialect | ||
| enable_parsing=True, # Parse SQL into AST | ||
| enable_validation=True, # Run security/performance validators | ||
| enable_transformations=True, # Apply AST transformations | ||
| enable_caching=True, # Enable multi-tier caching | ||
| ) | ||
|
|
||
| # Apply to adapter | ||
| dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", "postgresql://localhost/db") | ||
| config = AsyncpgConfig(pool_config={"dsn": dsn}, statement_config=statement_config) | ||
| assert config.statement_config.dialect == "postgres" | ||
| assert config.statement_config.enable_parsing is True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| def test_parameter_style_config() -> None: | ||
| from sqlspec import ParameterStyle, ParameterStyleConfig, StatementConfig | ||
|
|
||
| param_config = ParameterStyleConfig( | ||
| default_parameter_style=ParameterStyle.NUMERIC, # $1, $2, ... | ||
| supported_parameter_styles={ | ||
| ParameterStyle.NUMERIC, | ||
| ParameterStyle.NAMED_COLON, # :name | ||
| }, | ||
| has_native_list_expansion=False, | ||
| needs_static_script_compilation=False, | ||
| ) | ||
|
|
||
| statement_config = StatementConfig(dialect="postgres", parameter_config=param_config) | ||
| assert statement_config.parameter_config.default_parameter_style == ParameterStyle.NUMERIC |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| def test_parameter_styles() -> None: | ||
| from sqlspec import ParameterStyle | ||
|
|
||
| # Question mark (SQLite, DuckDB) | ||
| qmark = ParameterStyle.QMARK # WHERE id = ? | ||
|
|
||
| # Numeric (PostgreSQL, asyncpg) | ||
| numeric = ParameterStyle.NUMERIC # WHERE id = $1 | ||
|
|
||
| # Named colon (Oracle, SQLite) | ||
| named_colon = ParameterStyle.NAMED_COLON # WHERE id = :id | ||
|
|
||
| # Named at (BigQuery) | ||
|
|
||
| # Format/pyformat (psycopg, MySQL) | ||
|
|
||
| assert qmark == ParameterStyle.QMARK | ||
| assert numeric == ParameterStyle.NUMERIC | ||
| assert named_colon == ParameterStyle.NAMED_COLON |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| """Test configuration example: Global cache configuration.""" | ||
|
|
||
| SQL_CACHE_SIZE = 1000 | ||
|
|
||
|
|
||
| def test_global_cache_config() -> None: | ||
| """Test global cache configuration.""" | ||
| from sqlspec.core.cache import CacheConfig, update_cache_config | ||
|
|
||
| cache_config = CacheConfig( | ||
| compiled_cache_enabled=True, # Cache compiled SQL | ||
| sql_cache_enabled=True, # Cache SQL strings | ||
| fragment_cache_enabled=True, # Cache SQL fragments | ||
| optimized_cache_enabled=True, # Cache optimized AST | ||
| sql_cache_size=SQL_CACHE_SIZE, # Maximum cached SQL items | ||
| ) | ||
|
|
||
| # Update global cache configuration | ||
| update_cache_config(cache_config) | ||
|
|
||
| # Verify config applied | ||
| assert cache_config.sql_cache_enabled is True | ||
| assert cache_config.sql_cache_size == SQL_CACHE_SIZE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| """Test configuration example: Per-instance cache configuration.""" | ||
|
|
||
|
|
||
| def test_per_instance_cache_config() -> None: | ||
| """Test per-instance cache configuration.""" | ||
| import tempfile | ||
|
|
||
| from sqlspec import SQLSpec | ||
| from sqlspec.adapters.sqlite import SqliteConfig | ||
| from sqlspec.core.cache import CacheConfig | ||
|
|
||
| with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: | ||
| # Configure cache for specific SQLSpec instance | ||
| db_manager = SQLSpec() | ||
| db_manager.update_cache_config(CacheConfig(sql_cache_enabled=True, sql_cache_size=500)) | ||
|
|
||
| # Add database config | ||
| db = db_manager.add_config(SqliteConfig(pool_config={"database": tmp.name})) | ||
|
|
||
| # Use the configured spec | ||
| with db_manager.provide_session(db) as session: | ||
| result = session.execute("SELECT 1") | ||
| assert result is not None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| """Test configuration example: Cache statistics tracking.""" | ||
|
|
||
|
|
||
| def test_cache_statistics() -> None: | ||
| """Test cache statistics tracking.""" | ||
| import tempfile | ||
|
|
||
| from sqlspec import SQLSpec | ||
| from sqlspec.adapters.sqlite import SqliteConfig | ||
| from sqlspec.core.cache import get_cache_statistics, log_cache_stats | ||
|
|
||
| with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: | ||
| db_manager = SQLSpec() | ||
| db = db_manager.add_config(SqliteConfig(pool_config={"database": tmp.name})) | ||
|
|
||
| # Execute some queries to generate cache activity | ||
| with db_manager.provide_session(db) as session: | ||
| session.execute("SELECT 1") | ||
| session.execute("SELECT 1") # Should hit cache | ||
|
|
||
| # Get statistics | ||
| stats = get_cache_statistics() | ||
| assert isinstance(stats, dict) | ||
| assert "multi_level" in stats | ||
|
|
||
| # Log statistics (logs to configured logger) | ||
| log_cache_stats() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| """Test configuration example: Cache clearing operations.""" | ||
|
|
||
|
|
||
| def test_clear_cache() -> None: | ||
| """Test cache clearing operations.""" | ||
| from sqlspec.core.cache import clear_all_caches, get_cache_statistics | ||
|
|
||
| # Get initial statistics | ||
| stats_before = get_cache_statistics() | ||
| assert isinstance(stats_before, dict) | ||
|
|
||
| # Clear all caches and reset statistics | ||
| clear_all_caches() | ||
|
|
||
| # Verify caches were cleared | ||
| stats_after = get_cache_statistics() | ||
| assert isinstance(stats_after, dict) | ||
| assert "multi_level" in stats_after |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| """Test configuration example: Binding multiple database configurations.""" | ||
|
|
||
|
|
||
| def test_binding_multiple_configs() -> None: | ||
| """Test binding multiple database configurations.""" | ||
| import os | ||
| import tempfile | ||
|
|
||
| from sqlspec import SQLSpec | ||
| from sqlspec.adapters.asyncpg import AsyncpgConfig | ||
| from sqlspec.adapters.sqlite import SqliteConfig | ||
|
|
||
| with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: | ||
| db_manager = SQLSpec() | ||
|
|
||
| # Add multiple configurations | ||
| sqlite_key = db_manager.add_config(SqliteConfig(pool_config={"database": tmp.name})) | ||
| dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", "postgresql://localhost/db") | ||
| asyncpg_key = db_manager.add_config(AsyncpgConfig(pool_config={"dsn": dsn})) | ||
|
|
||
| # Use specific configuration | ||
| with db_manager.provide_session(sqlite_key) as session: | ||
| session.execute("SELECT 1") | ||
|
|
||
| sqlite_config = db_manager.get_config(sqlite_key) | ||
| pg_config = db_manager.get_config(asyncpg_key) | ||
|
|
||
| assert sqlite_config.pool_config["database"] == tmp.name | ||
| assert pg_config.pool_config["dsn"] == os.getenv("SQLSPEC_USAGE_PG_DSN", "postgresql://localhost/db") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| def test_sqlite_config_setup() -> None: | ||
| from sqlspec.adapters.sqlite import SqliteConfig | ||
|
|
||
| config = SqliteConfig( | ||
| pool_config={ | ||
| "database": "myapp.db", # Database file path | ||
| "timeout": 5.0, # Lock timeout in seconds | ||
| "check_same_thread": False, # Allow multi-thread access | ||
| "cached_statements": 100, # Statement cache size | ||
| "uri": False, # Enable URI mode | ||
| } | ||
| ) | ||
| assert config.pool_config["database"] == "myapp.db" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| """Test configuration example: Named database bindings.""" | ||
|
|
||
|
|
||
| def test_named_bindings() -> None: | ||
| """Test named database bindings.""" | ||
| import os | ||
| import tempfile | ||
|
|
||
| from sqlspec import SQLSpec | ||
| from sqlspec.adapters.asyncpg import AsyncpgConfig | ||
| from sqlspec.adapters.sqlite import SqliteConfig | ||
|
|
||
| with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: | ||
| db_manager = SQLSpec() | ||
|
|
||
| # Add with bind keys | ||
| cache_key = db_manager.add_config( | ||
| SqliteConfig(pool_config={"database": tmp.name}, bind_key="cache_db") | ||
| ) | ||
| dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", "postgresql://localhost/db") | ||
| main_key = db_manager.add_config(AsyncpgConfig(pool_config={"dsn": dsn}, bind_key="main_db")) | ||
|
|
||
| # Access by bind key | ||
| with db_manager.provide_session(cache_key) as session: | ||
| session.execute("SELECT 1") | ||
|
|
||
| cache_config = db_manager.get_config(cache_key) | ||
| main_config = db_manager.get_config(main_key) | ||
|
|
||
| assert cache_config.bind_key == "cache_db" | ||
| assert main_config.bind_key == "main_db" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| """Test configuration example: Basic migration configuration.""" | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| @pytest.mark.skipif( | ||
| not pytest.importorskip("asyncpg", reason="AsyncPG not installed"), reason="AsyncPG integration tests disabled" | ||
| ) | ||
| def test_basic_migration_config() -> None: | ||
| """Test basic migration configuration.""" | ||
| import os | ||
|
|
||
| from sqlspec.adapters.asyncpg import AsyncpgConfig | ||
|
|
||
| dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", "postgresql://localhost/db") | ||
| config = AsyncpgConfig( | ||
| pool_config={"dsn": dsn}, | ||
| extension_config={ | ||
| "litestar": {"session_table": "custom_sessions"} # Extension settings | ||
| }, | ||
| migration_config={ | ||
| "script_location": "migrations", # Migration directory | ||
| "version_table": "alembic_version", # Version tracking table | ||
| "include_extensions": ["litestar"], # Simple string list only | ||
| }, | ||
| ) | ||
|
|
||
| assert config.migration_config["script_location"] == "migrations" | ||
| assert "litestar" in config.migration_config["include_extensions"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| def test_basic_migration_config() -> None: | ||
| import os | ||
|
|
||
| from sqlspec.adapters.asyncpg import AsyncpgConfig | ||
|
|
||
| dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", "postgresql://localhost/db") | ||
| config = AsyncpgConfig( | ||
| pool_config={"dsn": dsn}, | ||
| extension_config={ | ||
| "litestar": {"session_table": "custom_sessions"} # Extension settings | ||
| }, | ||
| migration_config={ | ||
| "script_location": "migrations", # Migration directory | ||
| "version_table": "alembic_version", # Version tracking table | ||
| "include_extensions": ["litestar"], # Simple string list only | ||
| }, | ||
| ) | ||
| assert config.migration_config["script_location"] == "migrations" | ||
| assert "litestar" in config.migration_config["include_extensions"] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.