|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +from pytest_databases.docker.postgres import PostgresService |
| 4 | + |
| 5 | +from sqlspec import SQLFileLoader |
| 6 | +from sqlspec.adapters.asyncpg import AsyncpgConfig |
| 7 | +from sqlspec.adapters.sqlite import SqliteConfig |
| 8 | + |
| 9 | +__all__ = ("test_multi_database_setup_example",) |
| 10 | + |
| 11 | + |
| 12 | +async def test_multi_database_setup_example(tmp_path: Path, postgres_service: PostgresService) -> None: |
| 13 | + user_sql_path_pg = tmp_path / "sql" / "postgres" |
| 14 | + user_sql_path_pg.mkdir(parents=True, exist_ok=True) |
| 15 | + user_sql_file_pg = user_sql_path_pg / "users.sql" |
| 16 | + user_sql_file_pg.write_text( |
| 17 | + """-- name: upsert_user |
| 18 | + INSERT INTO users_sf1 (id, username, email) VALUES (:id, :username, :email) |
| 19 | + ON CONFLICT (id) DO UPDATE SET username = EXCLUDED.username, email = EXCLUDED.email; |
| 20 | + """ |
| 21 | + ) |
| 22 | + user_sql_path_sqlite = tmp_path / "sql" / "sqlite" |
| 23 | + user_sql_path_sqlite.mkdir(parents=True, exist_ok=True) |
| 24 | + user_sql_file_sqlite = user_sql_path_sqlite / "users.sql" |
| 25 | + user_sql_file_sqlite.write_text( |
| 26 | + """-- name: get_user |
| 27 | + SELECT id, username, email FROM users_sf1 WHERE id = :user_id; |
| 28 | + """ |
| 29 | + ) |
| 30 | + shared_sql_path = tmp_path / "sql" / "shared" |
| 31 | + shared_sql_path.mkdir(parents=True, exist_ok=True) |
| 32 | + shared_sql_file = shared_sql_path / "common.sql" |
| 33 | + shared_sql_file.write_text( |
| 34 | + """-- name: delete_user |
| 35 | + DELETE FROM users_sf1 WHERE id = :user_id; |
| 36 | + """ |
| 37 | + ) |
| 38 | + params = { "id": 1, "username": "john_doe", "email": "[email protected]"} |
| 39 | + |
| 40 | + # start-example |
| 41 | + # Different SQL files for different databases |
| 42 | + loader = SQLFileLoader() |
| 43 | + loader.load_sql(tmp_path / "sql/postgres/", tmp_path / "sql/sqlite/", tmp_path / "sql/shared/") |
| 44 | + |
| 45 | + # Queries automatically select correct dialect |
| 46 | + pg_query = loader.get_sql("upsert_user") # Uses Postgres ON CONFLICT |
| 47 | + sqlite_query = loader.get_sql("get_user") # Uses shared query |
| 48 | + |
| 49 | + from sqlspec import SQLSpec |
| 50 | + |
| 51 | + spec = SQLSpec() |
| 52 | + postgres_config = AsyncpgConfig( |
| 53 | + pool_config={ |
| 54 | + "user": postgres_service.user, |
| 55 | + "password": postgres_service.password, |
| 56 | + "host": postgres_service.host, |
| 57 | + "port": postgres_service.port, |
| 58 | + "database": postgres_service.database, |
| 59 | + } |
| 60 | + ) |
| 61 | + sqlite_config = SqliteConfig() |
| 62 | + # Execute on appropriate database |
| 63 | + async with spec.provide_session(postgres_config) as pg_session: |
| 64 | + await pg_session.execute("""CREATE TABLE users_sf1 ( id INTEGER PRIMARY KEY, username TEXT, email TEXT)""") |
| 65 | + await pg_session.execute( |
| 66 | + """ INSERT INTO users_sf1 (id, username, email) VALUES (1, 'old_name', '[email protected]');""" |
| 67 | + ) |
| 68 | + |
| 69 | + await pg_session.execute(pg_query, **params) |
| 70 | + |
| 71 | + with spec.provide_session(sqlite_config) as sqlite_session: |
| 72 | + sqlite_session.execute("""CREATE TABLE users_sf1 ( id INTEGER PRIMARY KEY, username TEXT, email TEXT)""") |
| 73 | + sqlite_session.execute( |
| 74 | + """ INSERT INTO users_sf1 (id, username, email) VALUES (1, 'john_doe', '[email protected]');""" |
| 75 | + ) |
| 76 | + sqlite_session.execute(sqlite_query, user_id=1) |
| 77 | + # end-example |
| 78 | + # Dummy asserts for doc example |
| 79 | + assert hasattr(loader, "load_sql") |
0 commit comments