Skip to content

feat: a command at every lifecycle moment, for teams that write no Python - #39

Closed
AlexeyShalaev wants to merge 1 commit into
feat/container-imagefrom
feat/command-hooks
Closed

AlexeyShalaev wants to merge 1 commit into
feat/container-imagefrom
feat/command-hooks

Conversation

@AlexeyShalaev

Copy link
Copy Markdown
Member

Part 3b of #32: hooks as commands.

The epic's stated blocker is already gone. It says #27 must be resolved before 3a/3b so we do not ship a config contract we immediately have to break. Option A shipped in 1.1: one PartitionEvent for all eight phases. So this needed no new context at all — the payload a command reads is event.model_dump_json(by_alias=True), the same object a Python hook is handed, and before_drop is told the window and the size that DETACH has already erased from the catalog.

hooks:
  timeout_seconds: 600
  before_drop: ["/usr/local/bin/archive-partition"]
pg-partsmith apply -c partitions.yaml --allow-destructive --allow-hooks

A non-zero exit is a refusal, exactly as a raised exception is; a command outliving its timeout is killed and counts as one, because a hook that hangs holds the table's maintenance lock. Argument vectors only — nothing goes through a shell, so a partition name can never be read as syntax.

On --allow-hooks. A document declaring hooks is refused rather than quietly stripped when the flag is absent: silently skipping a configured before_drop would let an operator read the file, believe their archiver ran, and be wrong. The docs make the epic's own argument — the document already authorises dropping tables, so a command in it is not a new privilege boundary as long as it comes from the same trust domain as the DSN, and that stops holding the moment it is assembled from somewhere less trusted. No sandbox is claimed.

Two things worth reviewing:

  1. CommandHooks must implement all eight phase methods — the executor calls getattr(hook, phase.value)(event) after on_event, so a hook with only on_event would abort the operation with an AttributeError. mypy caught it; it now inherits BasePartitionLifecycleHooks, with a test pinning it.
  2. asyncio.create_subprocess_exec raises NotImplementedError on a selector loop — which is what this repo's own conftest installs on Windows. Rather than patch around the platform, both mirrors run the command through one blocking implementation and the aio side hands it to a thread. A hook that runs in production but not on a developer's machine is a hook nobody trusts.

11 unit tests across both mirrors + 3 against a real PostgreSQL, including a hook firing around a real DROP and a refusing hook leaving the partition in place.

…thon

A Go or Ruby team already owns an archiver; what they need is for it to be
invoked at the right moment with the right metadata. CommandHooks runs a
configured command per phase, hands it the PartitionEvent as JSON on stdin,
and treats a non-zero exit exactly as a raised exception -- the operation is
abandoned and planned again next run.

This needed no new hook context. The event has been one model for every
phase since the start of this release, so the payload is a dump of the
object a Python hook already receives, and before_drop is told the window
and the size that DETACH has already erased from the catalog.

The commands run only when the run asks for them, and a document declaring
hooks is refused rather than quietly stripped when it does not: silently
skipping a configured before_drop would let an operator read the file,
believe their archiver ran, and be wrong. No sandbox is provided and none is
claimed -- the container boundary is the honest answer, and the docs say so.

Both mirrors run the command through one blocking implementation, with the
aio side handing it to a thread. asyncio.create_subprocess_exec needs a loop
that can spawn processes, and a selector loop -- what a Windows asyncpg
deployment installs -- raises NotImplementedError instead. A hook that runs
in production and not on a developer's machine is a hook nobody trusts.
@AlexeyShalaev

Copy link
Copy Markdown
Member Author

Folded into #43, one PR for the whole epic; this branch stays for history.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant