-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.py
More file actions
71 lines (51 loc) · 1.67 KB
/
Copy pathsql.py
File metadata and controls
71 lines (51 loc) · 1.67 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
"""
SQL file seeding adapter.
Provides functionality to seed databases from SQL files.
"""
from __future__ import annotations
import logging
from pathlib import Path
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from pgsql_test.types import SeedContext
logger = logging.getLogger(__name__)
class SqlFileSeedAdapter:
"""
Seed adapter that executes SQL files.
Usage:
adapter = SqlFileSeedAdapter(['schema.sql', 'fixtures.sql'])
await adapter.seed(ctx)
"""
def __init__(self, files: list[str | Path]) -> None:
"""
Initialize the SQL file seed adapter.
Args:
files: List of SQL file paths to execute
"""
self._files = [Path(f) for f in files]
def seed(self, ctx: SeedContext) -> None:
"""
Execute the SQL files in order.
Args:
ctx: Seed context containing pg client and config
"""
pg = ctx["pg"]
for file_path in self._files:
if not file_path.exists():
raise FileNotFoundError(f"SQL file not found: {file_path}")
logger.debug(f"Loading SQL file: {file_path}")
sql_content = file_path.read_text(encoding="utf-8")
# Execute the SQL
pg.query(sql_content)
logger.info(f"Executed SQL file: {file_path}")
def sqlfile(files: list[str | Path]) -> SqlFileSeedAdapter:
"""
Create a SQL file seed adapter.
Args:
files: List of SQL file paths to execute
Returns:
A SqlFileSeedAdapter instance
Example:
seed_adapters = [seed.sqlfile(['schema.sql', 'fixtures.sql'])]
"""
return SqlFileSeedAdapter(files)