python-glowplug 0.3.2__py3-none-any.whl → 0.3.3__py3-none-any.whl

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.
glowplug/driver/base.py CHANGED
@@ -1,11 +1,11 @@
1
1
  from abc import ABC, abstractmethod
2
2
  from functools import cached_property
3
- from typing import Any
3
+ from typing import Any, List
4
4
 
5
5
  import alembic
6
6
  import alembic.command
7
7
  import alembic.config
8
- from sqlalchemy import Engine, create_engine
8
+ from sqlalchemy import Engine, create_engine, inspect
9
9
  from sqlalchemy.ext.asyncio import (
10
10
  AsyncEngine,
11
11
  AsyncSession,
@@ -58,6 +58,29 @@ class DbDriver(ABC):
58
58
  """The sync uri."""
59
59
  ...
60
60
 
61
+ def list_tables(self) -> List[str]:
62
+ """List tables in the database."""
63
+ # TODO: sqlalchemy doesn't have an async `inspect` API.
64
+ # Presumably this won't be run any context where that matters much,
65
+ # but ideally we'd have a way to do this async.
66
+ engine = self.get_sync_engine()
67
+ inspector = inspect(engine)
68
+ return inspector.get_table_names()
69
+
70
+ async def is_blank_slate(self) -> bool:
71
+ """Helper to check if the database is missing or has no tables.
72
+
73
+ Returns:
74
+ bool: True if the database is a blank slate, False otherwise.
75
+ """
76
+ if not await self.exists():
77
+ return True
78
+
79
+ if not self.list_tables():
80
+ return True
81
+
82
+ return False
83
+
61
84
  async def init(self, base: DeclarativeBase, drop_first: bool = False) -> None:
62
85
  """Initialize the database."""
63
86
  engine = self.get_async_engine()
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-glowplug
3
- Version: 0.3.2
4
- Summary:
3
+ Version: 0.3.3
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
7
7
  Author: Joe Nudell
@@ -31,6 +31,8 @@ Chooses opinionated drivers with both async + sync support, as well as support f
31
31
  - `exists` - Check if database exists
32
32
  - `create` - Create a new database
33
33
  - `init` - Create all tables in the given database, optionally dropping first
34
+ - `list_tables` - List tables in the database
35
+ - `is_blank_slate` - Check if database is either missing or exists but is empty
34
36
  - `alembic` - Run any of the alembic commands on the given database
35
37
 
36
38
  ## Supported databases
@@ -1,11 +1,11 @@
1
1
  glowplug/__init__.py,sha256=VKKI-9C6KSlXwvisxTQFhVUUtQdwiNeV_g8wB5TVjM4,378
2
2
  glowplug/config/__init__.py,sha256=0v2Tqk8mJ3v_wRuifXP1-V4x7--xupBxxtrSt2CFqXs,1628
3
3
  glowplug/driver/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- glowplug/driver/base.py,sha256=ZXVFaWcp8K6iPZuG7NkI_CPx_JZAXGXJvghGm7TE79k,2896
4
+ glowplug/driver/base.py,sha256=hMC8HKEY97O-_6VxCfBwdilPRQ3goDt8vaNcDl9KhkY,3673
5
5
  glowplug/driver/mssql.py,sha256=gbrlh2O-LViDwNmwwpme0sNzu2EbgJtQKbp4o_mNrvs,2635
6
6
  glowplug/driver/pg.py,sha256=4Hw2WG33UzgYyJtWLv8QWZzHhzzsM34TyDuObtxvNmI,2600
7
7
  glowplug/driver/sqlite.py,sha256=DEJ8lYP8ZPemeGezTovs8WISGaVyUfwTIqiwMz3FPdU,1020
8
- python_glowplug-0.3.2.dist-info/LICENSE,sha256=HvAzro26TfbigQ3ppmyagEGHIr_UtyQDfU8muTAqWFU,1060
9
- python_glowplug-0.3.2.dist-info/METADATA,sha256=Aak5w7-Bb8WEOR1S4HFRGGnXzHz4SGtVnCOMmwfet6U,1290
10
- python_glowplug-0.3.2.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
11
- python_glowplug-0.3.2.dist-info/RECORD,,
8
+ python_glowplug-0.3.3.dist-info/LICENSE,sha256=HvAzro26TfbigQ3ppmyagEGHIr_UtyQDfU8muTAqWFU,1060
9
+ python_glowplug-0.3.3.dist-info/METADATA,sha256=acjy0WeTq0-Ou_62GIxF00qwBAdl_9XYD32eJRpXIuM,1494
10
+ python_glowplug-0.3.3.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
11
+ python_glowplug-0.3.3.dist-info/RECORD,,