(self)
| 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() |
nothing calls this directly
no test coverage detected