python-glowplug 0.3.2__tar.gz → 0.3.4__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,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.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
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
@@ -10,6 +10,8 @@ Chooses opinionated drivers with both async + sync support, as well as support f
10
10
  - `exists` - Check if database exists
11
11
  - `create` - Create a new database
12
12
  - `init` - Create all tables in the given database, optionally dropping first
13
+ - `list_tables` - List tables in the database
14
+ - `is_blank_slate` - Check if database is either missing or exists but is empty
13
15
  - `alembic` - Run any of the alembic commands on the given database
14
16
 
15
17
  ## Supported databases
@@ -1,7 +1,7 @@
1
1
  [tool.poetry]
2
2
  name = "python-glowplug"
3
- version = "0.3.2"
4
- description = ""
3
+ version = "0.3.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"
7
7
  license = "MIT"
@@ -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()
@@ -26,7 +26,9 @@ class SqliteDriver(DbDriver):
26
26
  return
27
27
  # Make sure directories exist. The file itself will be created
28
28
  # when the first connection is made.
29
- os.makedirs(os.path.dirname(self.path), exist_ok=True)
29
+ dirname = os.path.dirname(self.path)
30
+ if dirname:
31
+ os.makedirs(dirname, exist_ok=True)
30
32
 
31
33
  @property
32
34
  def async_uri(self) -> str:
File without changes