-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.py
More file actions
284 lines (244 loc) · 9.06 KB
/
Copy pathadmin.py
File metadata and controls
284 lines (244 loc) · 9.06 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"""
DbAdmin - Database administration utilities for test environments.
Provides functionality for creating, dropping, and managing test databases.
"""
from __future__ import annotations
import logging
from typing import Any
import psycopg2
from psycopg2 import sql
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
from pgsql_test.types import PgConfig
logger = logging.getLogger(__name__)
class DbAdmin:
"""
Database administration utilities for test environments.
Provides methods for:
- Creating and dropping databases
- Installing extensions
- Creating databases from templates
- Managing roles and permissions
"""
def __init__(self, config: PgConfig, verbose: bool = False) -> None:
"""
Initialize the database admin.
Args:
config: PostgreSQL connection configuration
verbose: Whether to log verbose output
"""
self._config = config
self._verbose = verbose
self._conn: psycopg2.extensions.connection | None = None
@property
def config(self) -> PgConfig:
"""Return the connection configuration."""
return self._config
def _get_admin_connection(self) -> psycopg2.extensions.connection:
"""Get a connection for admin operations (autocommit mode)."""
if self._conn is None or self._conn.closed:
self._conn = psycopg2.connect(
host=self._config.get("host", "localhost"),
port=self._config.get("port", 5432),
database=self._config.get("database", "postgres"),
user=self._config.get("user", "postgres"),
password=self._config.get("password", ""),
)
self._conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
return self._conn
def close(self) -> None:
"""Close the admin connection."""
if self._conn is not None:
try:
self._conn.close()
except Exception as e:
logger.warning(f"Error closing admin connection: {e}")
finally:
self._conn = None
def database_exists(self, database: str) -> bool:
"""Check if a database exists."""
conn = self._get_admin_connection()
with conn.cursor() as cur:
cur.execute(
"SELECT 1 FROM pg_database WHERE datname = %s",
(database,),
)
return cur.fetchone() is not None
def create(self, database: str) -> None:
"""
Create a new database.
Args:
database: Name of the database to create
"""
if self.database_exists(database):
if self._verbose:
logger.info(f"Database already exists: {database}")
return
conn = self._get_admin_connection()
with conn.cursor() as cur:
# Use sql.Identifier for safe database name handling
cur.execute(
sql.SQL("CREATE DATABASE {}").format(sql.Identifier(database))
)
logger.info(f"Created database: {database}")
def drop(self, database: str | None = None) -> None:
"""
Drop a database.
Args:
database: Name of the database to drop (defaults to config database)
"""
database = database or self._config.get("database")
if not database:
raise ValueError("No database specified to drop")
if not self.database_exists(database):
if self._verbose:
logger.info(f"Database does not exist: {database}")
return
conn = self._get_admin_connection()
with conn.cursor() as cur:
# Terminate existing connections
cur.execute(
"""
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = %s AND pid <> pg_backend_pid()
""",
(database,),
)
# Drop the database
cur.execute(
sql.SQL("DROP DATABASE IF EXISTS {}").format(sql.Identifier(database))
)
logger.info(f"Dropped database: {database}")
def create_from_template(self, template: str, database: str) -> None:
"""
Create a database from a template.
Args:
template: Name of the template database
database: Name of the new database
"""
if self.database_exists(database):
if self._verbose:
logger.info(f"Database already exists: {database}")
return
conn = self._get_admin_connection()
with conn.cursor() as cur:
cur.execute(
sql.SQL("CREATE DATABASE {} TEMPLATE {}").format(
sql.Identifier(database),
sql.Identifier(template),
)
)
logger.info(f"Created database {database} from template {template}")
def install_extensions(
self, extensions: list[str], database: str | None = None
) -> None:
"""
Install PostgreSQL extensions in a database.
Args:
extensions: List of extension names to install
database: Target database (defaults to config database)
"""
database = database or self._config.get("database")
if not database:
raise ValueError("No database specified")
# Connect to the target database
conn = psycopg2.connect(
host=self._config.get("host", "localhost"),
port=self._config.get("port", 5432),
database=database,
user=self._config.get("user", "postgres"),
password=self._config.get("password", ""),
)
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
try:
with conn.cursor() as cur:
for ext in extensions:
cur.execute(
sql.SQL("CREATE EXTENSION IF NOT EXISTS {}").format(
sql.Identifier(ext)
)
)
logger.debug(f"Installed extension: {ext}")
finally:
conn.close()
logger.info(f"Installed extensions in {database}: {extensions}")
def create_role(
self,
role_name: str,
password: str | None = None,
login: bool = True,
superuser: bool = False,
) -> None:
"""
Create a PostgreSQL role.
Args:
role_name: Name of the role to create
password: Optional password for the role
login: Whether the role can login
superuser: Whether the role is a superuser
"""
conn = self._get_admin_connection()
with conn.cursor() as cur:
# Check if role exists
cur.execute(
"SELECT 1 FROM pg_roles WHERE rolname = %s",
(role_name,),
)
if cur.fetchone() is not None:
if self._verbose:
logger.info(f"Role already exists: {role_name}")
return
# Build CREATE ROLE statement
options = []
if login:
options.append("LOGIN")
if superuser:
options.append("SUPERUSER")
if password:
options.append(f"PASSWORD '{password}'")
options_str = " ".join(options)
cur.execute(
sql.SQL("CREATE ROLE {} {}").format(
sql.Identifier(role_name),
sql.SQL(options_str),
)
)
logger.info(f"Created role: {role_name}")
def grant_connect(self, role_name: str, database: str) -> None:
"""
Grant CONNECT privilege on a database to a role.
Args:
role_name: Name of the role
database: Name of the database
"""
conn = self._get_admin_connection()
with conn.cursor() as cur:
cur.execute(
sql.SQL("GRANT CONNECT ON DATABASE {} TO {}").format(
sql.Identifier(database),
sql.Identifier(role_name),
)
)
logger.debug(f"Granted CONNECT on {database} to {role_name}")
def grant_all(self, role_name: str, database: str) -> None:
"""
Grant ALL privileges on a database to a role.
Args:
role_name: Name of the role
database: Name of the database
"""
conn = self._get_admin_connection()
with conn.cursor() as cur:
cur.execute(
sql.SQL("GRANT ALL PRIVILEGES ON DATABASE {} TO {}").format(
sql.Identifier(database),
sql.Identifier(role_name),
)
)
logger.debug(f"Granted ALL on {database} to {role_name}")
def __enter__(self) -> DbAdmin:
"""Context manager entry."""
return self
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
"""Context manager exit."""
self.close()