-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapters.py
More file actions
124 lines (94 loc) · 3.05 KB
/
Copy pathadapters.py
File metadata and controls
124 lines (94 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""
Generic seed adapters for pgsql-test.
Provides composable seeding utilities:
- fn: Run custom Python functions
- compose: Combine multiple adapters
"""
from __future__ import annotations
import logging
from collections.abc import Callable
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from pgsql_test.types import SeedContext
logger = logging.getLogger(__name__)
class FnSeedAdapter:
"""
Seed adapter that runs a custom function.
Usage:
adapter = FnSeedAdapter(lambda ctx: ctx['pg'].query('INSERT INTO ...'))
adapter.seed(ctx)
"""
def __init__(self, func: Callable[[SeedContext], None]) -> None:
"""
Initialize the function seed adapter.
Args:
func: Function to execute during seeding
"""
self._func = func
def seed(self, ctx: SeedContext) -> None:
"""
Execute the seed function.
Args:
ctx: Seed context containing pg client and config
"""
logger.debug("Executing custom seed function")
self._func(ctx)
logger.debug("Custom seed function completed")
class ComposeSeedAdapter:
"""
Seed adapter that composes multiple adapters.
Executes adapters in order.
Usage:
adapter = ComposeSeedAdapter([
seed.sqlfile(['schema.sql']),
seed.fn(lambda ctx: ctx['pg'].query('INSERT INTO ...'))
])
adapter.seed(ctx)
"""
def __init__(self, adapters: list[FnSeedAdapter | ComposeSeedAdapter | object]) -> None:
"""
Initialize the compose adapter.
Args:
adapters: List of seed adapters to compose
"""
self._adapters = adapters
def seed(self, ctx: SeedContext) -> None:
"""
Execute all adapters in order.
Args:
ctx: Seed context containing pg client and config
"""
for i, adapter in enumerate(self._adapters):
logger.debug(f"Executing seed adapter {i + 1}/{len(self._adapters)}")
adapter.seed(ctx) # type: ignore
logger.debug(f"Completed {len(self._adapters)} seed adapters")
def fn(func: Callable[[SeedContext], None]) -> FnSeedAdapter:
"""
Create a function seed adapter.
Args:
func: Function to execute during seeding.
Receives SeedContext with 'pg', 'admin', and 'config'.
Returns:
A FnSeedAdapter instance
Example:
seed_adapters = [
seed.fn(lambda ctx: ctx['pg'].query('INSERT INTO users (name) VALUES (%s)', ('Alice',)))
]
"""
return FnSeedAdapter(func)
def compose(adapters: list[object]) -> ComposeSeedAdapter:
"""
Compose multiple seed adapters into one.
Args:
adapters: List of seed adapters to compose
Returns:
A ComposeSeedAdapter instance
Example:
seed_adapters = [
seed.compose([
seed.sqlfile(['schema.sql']),
seed.fn(lambda ctx: ctx['pg'].query('INSERT INTO ...'))
])
]
"""
return ComposeSeedAdapter(adapters)