python-glowplug 0.3.9__tar.gz → 0.3.11__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-glowplug
3
- Version: 0.3.9
3
+ Version: 0.3.11
4
4
  Summary: A collection of utilities for common DB maintenance operations in SQLAlchemy
5
5
  Home-page: https://github.com/jnu/python-glowplug
6
6
  License: MIT
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "python-glowplug"
3
- version = "0.3.9"
3
+ version = "0.3.11"
4
4
  description = "A collection of utilities for common DB maintenance operations in SQLAlchemy"
5
5
  authors = ["Joe Nudell <jnudell@hks.harvard.edu>"]
6
6
  readme = "README.md"
@@ -19,6 +19,9 @@ class SqliteSettings(BaseModel):
19
19
  return SqliteDriver(self.path)
20
20
 
21
21
 
22
+ PgSslMode = Literal["disable", "allow", "prefer", "require", "verify-ca", "verify-full"]
23
+
24
+
22
25
  class PostgresSettings(BaseModel):
23
26
  """Settings for connecting to Postgres."""
24
27
 
@@ -29,10 +32,15 @@ class PostgresSettings(BaseModel):
29
32
  password: str
30
33
  database: str
31
34
  maintenance_db: str | None = None
35
+ sslmode: PgSslMode | None = None
32
36
 
33
37
  @cached_property
34
38
  def driver(self) -> PostgresDriver:
35
39
  url = f"{self.user}:{self.password}@{self.host}:{self.port}/{self.database}"
40
+
41
+ if self.sslmode:
42
+ url += f"?sslmode={self.sslmode}"
43
+
36
44
  return PostgresDriver(url, maintenance_db=self.maintenance_db)
37
45
 
38
46
 
@@ -64,8 +64,11 @@ class DbDriver(ABC):
64
64
  # Presumably this won't be run any context where that matters much,
65
65
  # but ideally we'd have a way to do this async.
66
66
  engine = self.get_sync_engine()
67
- inspector = inspect(engine)
68
- return inspector.get_table_names()
67
+ try:
68
+ inspector = inspect(engine)
69
+ return inspector.get_table_names()
70
+ finally:
71
+ engine.dispose()
69
72
 
70
73
  async def is_blank_slate(self) -> bool:
71
74
  """Helper to check if the database is missing or has no tables.
@@ -27,14 +27,17 @@ class MsSqlDriver(DbDriver):
27
27
  path, database = self._split_path()
28
28
  # Connect to the database-less path
29
29
  engine = create_async_engine(f"mssql+aioodbc://{path}")
30
- async with engine.connect() as conn:
31
- # Check if the database exists
32
- result = await conn.execute(
33
- text("SELECT name FROM sys.databases WHERE name = :db"),
34
- {"db": database},
35
- )
36
- row = result.fetchone()
37
- return row is not None
30
+ try:
31
+ async with engine.connect() as conn:
32
+ # Check if the database exists
33
+ result = await conn.execute(
34
+ text("SELECT name FROM sys.databases WHERE name = :db"),
35
+ {"db": database},
36
+ )
37
+ row = result.fetchone()
38
+ return row is not None
39
+ finally:
40
+ await engine.dispose()
38
41
 
39
42
  async def create(self) -> None:
40
43
  """Create the database."""
@@ -44,10 +47,13 @@ class MsSqlDriver(DbDriver):
44
47
  engine = create_async_engine(f"mssql+aioodbc://{path}")
45
48
  # Need to run this with autocommit=True to avoid a transaction error
46
49
  # See: https://stackoverflow.com/a/42008664
47
- async with engine.connect() as conn:
48
- rc = await conn.get_raw_connection()
49
- rc.driver_connection.autocommit = True
50
- await conn.execute(text(f"CREATE DATABASE {database}"))
50
+ try:
51
+ async with engine.connect() as conn:
52
+ rc = await conn.get_raw_connection()
53
+ rc.driver_connection.autocommit = True
54
+ await conn.execute(text(f"CREATE DATABASE {database}"))
55
+ finally:
56
+ await engine.dispose()
51
57
 
52
58
  @property
53
59
  def async_uri(self) -> str:
@@ -26,10 +26,10 @@ class PostgresDriver(DbDriver):
26
26
  async def exists(self) -> bool:
27
27
  """Check if the database exists."""
28
28
  # Split the url on the last / to get base url & db name
29
- base_url, db_name = self._split_url()
29
+ base_url, db_name, query = self._split_url()
30
30
  # If a maintenance db is provided, use that to check if the db exists
31
31
  if self.maintenance_db:
32
- base_url = f"{base_url}/{self.maintenance_db}"
32
+ base_url = f"{base_url}/{self.maintenance_db}?{query}"
33
33
  engine = create_async_engine(
34
34
  f"postgresql+asyncpg://{base_url}", echo=self.debug
35
35
  )
@@ -45,10 +45,10 @@ class PostgresDriver(DbDriver):
45
45
 
46
46
  async def create(self) -> None:
47
47
  """Create the database."""
48
- base_url, db_name = self._split_url()
48
+ base_url, db_name, query = self._split_url()
49
49
  # If a maintenance db is provided, use that to create the db
50
50
  if self.maintenance_db:
51
- base_url = f"{base_url}/{self.maintenance_db}"
51
+ base_url = f"{base_url}/{self.maintenance_db}?{query}"
52
52
  engine = create_async_engine(
53
53
  f"postgresql+asyncpg://{base_url}",
54
54
  echo=self.debug,
@@ -65,10 +65,11 @@ class PostgresDriver(DbDriver):
65
65
 
66
66
  @property
67
67
  def sync_uri(self) -> str:
68
- """Sync connection stringe."""
68
+ """Sync connection string."""
69
69
  return f"postgresql://{self.url}"
70
70
 
71
- def _split_url(self) -> tuple[str, str]:
71
+ def _split_url(self) -> tuple[str, str, str]:
72
72
  """Split the url on the last / to get base url & db name."""
73
- url, db = self.url.rsplit("/", 1)
74
- return url, db
73
+ url, db_and_query = self.url.rsplit("/", 1)
74
+ db, _, query = db_and_query.partition("?")
75
+ return url, db, query