|
| 1 | +""" |
| 2 | +pgpm seed adapter for pysql-test. |
| 3 | +
|
| 4 | +Provides integration with pgpm (PostgreSQL Package Manager) for running |
| 5 | +database migrations as part of test seeding. |
| 6 | +
|
| 7 | +Requires pgpm to be installed globally: npm install -g pgpm |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import logging |
| 13 | +import os |
| 14 | +import subprocess |
| 15 | +from typing import TYPE_CHECKING |
| 16 | + |
| 17 | +if TYPE_CHECKING: |
| 18 | + from pysql_test.types import SeedContext |
| 19 | + |
| 20 | +logger = logging.getLogger(__name__) |
| 21 | + |
| 22 | + |
| 23 | +class PgpmSeedAdapter: |
| 24 | + """ |
| 25 | + Seed adapter that runs pgpm deploy to apply migrations. |
| 26 | +
|
| 27 | + This adapter calls the pgpm CLI via subprocess, passing the database |
| 28 | + connection info via environment variables. |
| 29 | +
|
| 30 | + Usage: |
| 31 | + adapter = PgpmSeedAdapter(module_path="./my-module") |
| 32 | + adapter.seed(ctx) |
| 33 | + """ |
| 34 | + |
| 35 | + def __init__( |
| 36 | + self, |
| 37 | + module_path: str | None = None, |
| 38 | + package: str | None = None, |
| 39 | + deploy_args: list[str] | None = None, |
| 40 | + cache: bool = False, |
| 41 | + ) -> None: |
| 42 | + """ |
| 43 | + Initialize the pgpm seed adapter. |
| 44 | +
|
| 45 | + Args: |
| 46 | + module_path: Path to the pgpm module directory (defaults to cwd) |
| 47 | + package: Package name to deploy (avoids interactive prompt) |
| 48 | + deploy_args: Additional arguments to pass to pgpm deploy |
| 49 | + cache: Whether to enable caching (not yet implemented) |
| 50 | + """ |
| 51 | + self._module_path = module_path |
| 52 | + self._package = package |
| 53 | + self._deploy_args = deploy_args or [] |
| 54 | + self._cache = cache |
| 55 | + |
| 56 | + def seed(self, ctx: SeedContext) -> None: |
| 57 | + """ |
| 58 | + Run pgpm deploy to apply migrations. |
| 59 | +
|
| 60 | + Args: |
| 61 | + ctx: Seed context containing pg client and config |
| 62 | +
|
| 63 | + Raises: |
| 64 | + RuntimeError: If pgpm deploy fails |
| 65 | + """ |
| 66 | + config = ctx["config"] |
| 67 | + |
| 68 | + # Build environment with database connection info |
| 69 | + env = os.environ.copy() |
| 70 | + env["PGHOST"] = config.get("host", "localhost") |
| 71 | + env["PGPORT"] = str(config.get("port", 5432)) |
| 72 | + env["PGDATABASE"] = config["database"] |
| 73 | + env["PGUSER"] = config.get("user", "postgres") |
| 74 | + if "password" in config: |
| 75 | + env["PGPASSWORD"] = config["password"] |
| 76 | + |
| 77 | + # Determine working directory |
| 78 | + cwd = self._module_path or os.getcwd() |
| 79 | + |
| 80 | + # Build pgpm deploy command |
| 81 | + cmd = ["pgpm", "deploy", "--yes", "--verbose"] |
| 82 | + if self._package: |
| 83 | + cmd.extend(["--package", self._package]) |
| 84 | + cmd.extend(self._deploy_args) |
| 85 | + |
| 86 | + logger.info(f"Running pgpm deploy in {cwd}") |
| 87 | + logger.debug(f"Command: {' '.join(cmd)}") |
| 88 | + logger.debug(f"Database: {config['database']}") |
| 89 | + |
| 90 | + try: |
| 91 | + result = subprocess.run( |
| 92 | + cmd, |
| 93 | + cwd=cwd, |
| 94 | + env=env, |
| 95 | + capture_output=True, |
| 96 | + text=True, |
| 97 | + check=False, |
| 98 | + ) |
| 99 | + |
| 100 | + if result.returncode != 0: |
| 101 | + error_msg = result.stderr or result.stdout or "Unknown error" |
| 102 | + logger.error(f"pgpm deploy failed: {error_msg}") |
| 103 | + raise RuntimeError(f"pgpm deploy failed: {error_msg}") |
| 104 | + |
| 105 | + logger.info("pgpm deploy completed successfully") |
| 106 | + if result.stdout: |
| 107 | + logger.info(f"pgpm output: {result.stdout}") |
| 108 | + if result.stderr: |
| 109 | + logger.info(f"pgpm stderr: {result.stderr}") |
| 110 | + |
| 111 | + except FileNotFoundError as err: |
| 112 | + raise RuntimeError( |
| 113 | + "pgpm not found. Install it with: npm install -g pgpm" |
| 114 | + ) from err |
| 115 | + |
| 116 | + |
| 117 | +def pgpm( |
| 118 | + module_path: str | None = None, |
| 119 | + package: str | None = None, |
| 120 | + deploy_args: list[str] | None = None, |
| 121 | + cache: bool = False, |
| 122 | +) -> PgpmSeedAdapter: |
| 123 | + """ |
| 124 | + Create a pgpm seed adapter. |
| 125 | +
|
| 126 | + This adapter runs pgpm deploy to apply database migrations as part of |
| 127 | + test seeding. Requires pgpm to be installed globally. |
| 128 | +
|
| 129 | + Args: |
| 130 | + module_path: Path to the pgpm module directory (defaults to cwd) |
| 131 | + package: Package name to deploy (avoids interactive prompt) |
| 132 | + deploy_args: Additional arguments to pass to pgpm deploy |
| 133 | + cache: Whether to enable caching |
| 134 | +
|
| 135 | + Returns: |
| 136 | + A PgpmSeedAdapter instance |
| 137 | +
|
| 138 | + Example: |
| 139 | + # Deploy migrations from a specific module |
| 140 | + seed_adapters = [ |
| 141 | + seed.pgpm(module_path="./packages/my-module", package="my-module") |
| 142 | + ] |
| 143 | +
|
| 144 | + # Deploy with additional arguments |
| 145 | + seed_adapters = [ |
| 146 | + seed.pgpm(module_path="./my-module", package="my-module", deploy_args=["--verbose"]) |
| 147 | + ] |
| 148 | + """ |
| 149 | + return PgpmSeedAdapter(module_path=module_path, package=package, deploy_args=deploy_args, cache=cache) |
0 commit comments