feat: add support for --comment CLI param, to set versioning comment

only relevant if Wutta-Continuum is enabled
This commit is contained in:
Lance Edgar 2025-12-29 12:49:39 -06:00
parent 6ee008e169
commit e397890098
7 changed files with 49 additions and 38 deletions

View file

@ -129,9 +129,11 @@ class ImportCommandHandler(GenericHandler):
# all params from caller will be passed along
kw = dict(ctx.params)
# runas user also, but it comes from root/parent command
# runas user and comment also, but they come from root command
if username := ctx.parent.params.get("runas_username"):
kw["runas_username"] = username
if comment := ctx.parent.params.get("comment"):
kw["transaction_comment"] = comment
# sort out which models to process
models = kw.pop("models")

View file

@ -37,14 +37,7 @@ from .base import import_command, ImportCommandHandler
@wutta_typer.command()
@import_command
def import_versions( # pylint: disable=unused-argument
ctx: typer.Context,
comment: Annotated[
str,
typer.Option("--comment", "-m", help="Comment to set on the transaction."),
] = "import catch-up versions",
**kwargs,
):
def import_versions(ctx: typer.Context, **kwargs): # pylint: disable=unused-argument
"""
Import latest data to version tables, for Wutta DB
"""

View file

@ -172,6 +172,12 @@ class ImportHandler(GenericHandler): # pylint: disable=too-many-public-methods
mostly used for Continuum versioning.
"""
transaction_comment = None
"""
Optional comment to apply to the transaction, where applicable.
This is mostly used for Continuum versioning.
"""
importers = None
"""
This should be a dict of all importer/exporter classes available
@ -425,6 +431,9 @@ class ImportHandler(GenericHandler): # pylint: disable=too-many-public-methods
if "runas_username" in kwargs:
self.runas_username = kwargs.pop("runas_username")
if "transaction_comment" in kwargs:
self.transaction_comment = kwargs.pop("transaction_comment")
return kwargs
def begin_transaction(self):
@ -959,18 +968,26 @@ class ToWuttaHandler(ToSqlalchemyHandler):
calling
:meth:`~wuttjamaican:wuttjamaican.app.AppHandler.make_session()`.
It then may "customize" the session slightly. These
customizations only are relevant if Wutta-Continuum versioning
is enabled:
If :attr:`~ImportHandler.runas_username` is set, the
responsible user (``continuum_user_id``) will be set for the
new session as well. This info is only used if the
Wutta-Continuum versioning feature is enabled.
new session as well.
Similarly, if :attr:`~ImportHandler.transaction_comment` is
set, it (``continuum_comment``) will also be set for the new
session.
:returns: :class:`~wuttjamaican:wuttjamaican.db.sess.Session`
instance.
"""
model = self.app.model
session = self.app.make_session()
# set runas user in case continuum versioning is enabled
if self.runas_username:
model = self.app.model
if user := (
session.query(model.User)
.filter_by(username=self.runas_username)
@ -980,4 +997,8 @@ class ToWuttaHandler(ToSqlalchemyHandler):
else:
log.warning("runas username not found: %s", self.runas_username)
# set comment in case continuum versioning is enabled
if self.transaction_comment:
session.info["continuum_comment"] = self.transaction_comment
return session

View file

@ -92,13 +92,6 @@ class FromWuttaToVersions(FromWuttaHandler, ToWuttaHandler):
See also :attr:`continuum_uow`.
"""
continuum_comment = None
def consume_kwargs(self, kwargs):
kwargs = super().consume_kwargs(kwargs)
self.continuum_comment = kwargs.pop("comment", None)
return kwargs
def begin_target_transaction(self):
# pylint: disable=line-too-long
"""
@ -128,8 +121,8 @@ class FromWuttaToVersions(FromWuttaHandler, ToWuttaHandler):
self.continuum_txn = self.continuum_uow.create_transaction(self.target_session)
if self.continuum_comment:
self.continuum_txn.meta = {"comment": self.continuum_comment}
if self.transaction_comment:
self.continuum_txn.meta = {"comment": self.transaction_comment}
def get_importer_kwargs(self, key, **kwargs):
"""

View file

@ -53,11 +53,19 @@ class TestImportCommandHandler(DataTestCase):
self.__dict__.update(kw)
with patch.object(handler, "import_handler") as import_handler:
parent = Mock(params={"runas_username": "fred"})
parent = Mock(
params={
"runas_username": "fred",
"comment": "hello world",
}
)
# TODO: why can't we just use Mock here? the parent attr is problematic
ctx = Object(params={"models": []}, parent=parent)
handler.run(ctx)
import_handler.process_data.assert_called_once_with(runas_username="fred")
import_handler.process_data.assert_called_once_with(
runas_username="fred",
transaction_comment="hello world",
)
def test_list_models(self):
handler = self.make_handler(

View file

@ -198,6 +198,14 @@ class TestImportHandler(DataTestCase):
self.assertNotIn("runas_username", kw)
self.assertEqual(handler.runas_username, "fred")
# transaction_comment (consumed)
self.assertIsNone(handler.transaction_comment)
kw["transaction_comment"] = "hello world"
result = handler.consume_kwargs(kw)
self.assertIs(result, kw)
self.assertNotIn("transaction_comment", kw)
self.assertEqual(handler.transaction_comment, "hello world")
def test_define_importers(self):
handler = self.make_handler()
importers = handler.define_importers()

View file

@ -14,20 +14,6 @@ class TestFromWuttaToVersions(VersionTestCase):
def make_handler(self, **kwargs):
return mod.FromWuttaToVersions(self.config, **kwargs)
def test_consume_kwargs(self):
# no comment by default
handler = self.make_handler()
kw = handler.consume_kwargs({})
self.assertEqual(kw, {})
self.assertIsNone(handler.continuum_comment)
# but can provide one
handler = self.make_handler()
kw = handler.consume_kwargs({"comment": "yeehaw"})
self.assertEqual(kw, {})
self.assertEqual(handler.continuum_comment, "yeehaw")
def test_begin_target_transaction(self):
model = self.app.model
txncls = continuum.transaction_class(model.User)
@ -44,7 +30,7 @@ class TestFromWuttaToVersions(VersionTestCase):
# with comment
handler = self.make_handler()
handler.continuum_comment = "yeehaw"
handler.transaction_comment = "yeehaw"
handler.begin_target_transaction()
self.assertIn("comment", handler.continuum_txn.meta)
self.assertEqual(handler.continuum_txn.meta["comment"], "yeehaw")