MCPcopy Create free account
hub / github.com/blacklanternsecurity/bbot / create_database

Method create_database

bbot/modules/output/postgres.py:28–53  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

26 protocol = "postgresql+asyncpg"
27
28 async def create_database(self):
29 import asyncpg
30 from sqlalchemy import text
31 from sqlalchemy.ext.asyncio import create_async_engine
32
33 # Create the engine for the initial connection to the server
34 initial_engine = create_async_engine(self.connection_string().rsplit("/", 1)[0])
35
36 async with initial_engine.connect() as conn:
37 # Check if the database exists
38 result = await conn.execute(text(f"SELECT 1 FROM pg_database WHERE datname = '{self.database}'"))
39 database_exists = result.scalar() is not None
40
41 # Create the database if it does not exist
42 if not database_exists:
43 # Use asyncpg directly to create the database
44 raw_conn = await asyncpg.connect(
45 user=self.username,
46 password=self.password,
47 host=self.host,
48 port=self.port,
49 )
50 try:
51 await raw_conn.execute(f"CREATE DATABASE {self.database}")
52 finally:
53 await raw_conn.close()

Callers

nothing calls this directly

Calls 3

textFunction · 0.85
connection_stringMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected