|
| 1 | +"""Lifecycle hooks (middleware) and protocol for partition operations. |
| 2 | +
|
| 3 | +Hooks let you inject custom logic at each step of the partition lifecycle - |
| 4 | +for example, exporting data before a partition is dropped, publishing events |
| 5 | +to Kafka after a partition is created, or archiving rows before detachment. |
| 6 | +
|
| 7 | +Usage example:: |
| 8 | +
|
| 9 | + class KafkaExportHooks(BasePartitionLifecycleHooks): |
| 10 | + def __init__(self, producer: KafkaProducer) -> None: |
| 11 | + self._producer = producer |
| 12 | +
|
| 13 | + def before_drop(self, table_name: str, partition_name: str) -> None: |
| 14 | + self._producer.send("partition.before_drop", { |
| 15 | + "table": table_name, |
| 16 | + "partition": partition_name, |
| 17 | + }) |
| 18 | +
|
| 19 | + service = PartitionLifecycleService( |
| 20 | + repo=repo, |
| 21 | + metadata=metadata, |
| 22 | + locks=locks, |
| 23 | + period_calculator=calculator, |
| 24 | + hooks=[KafkaExportHooks(producer)], |
| 25 | + ) |
| 26 | +""" |
| 27 | + |
| 28 | +from typing import Protocol, runtime_checkable |
| 29 | + |
| 30 | +from pg_partsmith.entities import PartitionInfo, TablePartitionConfig |
| 31 | + |
| 32 | + |
| 33 | +@runtime_checkable |
| 34 | +class PartitionLifecycleHooks(Protocol): |
| 35 | + """Protocol for partition lifecycle hooks. |
| 36 | +
|
| 37 | + Implement this protocol to inject custom logic at each step of the partition |
| 38 | + lifecycle without inheriting from a specific base class. |
| 39 | + """ |
| 40 | + |
| 41 | + def before_create( |
| 42 | + self, |
| 43 | + config: TablePartitionConfig, |
| 44 | + partition_name: str, |
| 45 | + from_value: str, |
| 46 | + to_value: str, |
| 47 | + ) -> None: ... |
| 48 | + |
| 49 | + def after_create( |
| 50 | + self, |
| 51 | + config: TablePartitionConfig, |
| 52 | + partition: PartitionInfo, |
| 53 | + ) -> None: ... |
| 54 | + |
| 55 | + def before_detach( |
| 56 | + self, |
| 57 | + table_name: str, |
| 58 | + partition: PartitionInfo, |
| 59 | + ) -> None: ... |
| 60 | + |
| 61 | + def after_detach( |
| 62 | + self, |
| 63 | + table_name: str, |
| 64 | + partition_name: str, |
| 65 | + ) -> None: ... |
| 66 | + |
| 67 | + def before_drop( |
| 68 | + self, |
| 69 | + table_name: str, |
| 70 | + partition_name: str, |
| 71 | + ) -> None: ... |
| 72 | + |
| 73 | + def after_drop( |
| 74 | + self, |
| 75 | + table_name: str, |
| 76 | + partition_name: str, |
| 77 | + ) -> None: ... |
| 78 | + |
| 79 | + |
| 80 | +class BasePartitionLifecycleHooks: |
| 81 | + """No-op base implementation of partition lifecycle hooks. |
| 82 | +
|
| 83 | + Subclass and override only the methods you need. |
| 84 | + All methods are no-ops by default so you can selectively add behaviour |
| 85 | + without implementing every step. |
| 86 | + """ |
| 87 | + |
| 88 | + def before_create( |
| 89 | + self, |
| 90 | + config: TablePartitionConfig, |
| 91 | + partition_name: str, |
| 92 | + from_value: str, |
| 93 | + to_value: str, |
| 94 | + ) -> None: |
| 95 | + """Called before a partition is created. |
| 96 | +
|
| 97 | + Args: |
| 98 | + config: Table partition configuration. |
| 99 | + partition_name: Name the new partition will be given. |
| 100 | + from_value: Start boundary value. |
| 101 | + to_value: End boundary value. |
| 102 | + """ |
| 103 | + |
| 104 | + def after_create( |
| 105 | + self, |
| 106 | + config: TablePartitionConfig, |
| 107 | + partition: PartitionInfo, |
| 108 | + ) -> None: |
| 109 | + """Called after a partition has been created (and optionally attached). |
| 110 | +
|
| 111 | + Args: |
| 112 | + config: Table partition configuration. |
| 113 | + partition: Info about the newly created partition. |
| 114 | + """ |
| 115 | + |
| 116 | + def before_detach( |
| 117 | + self, |
| 118 | + table_name: str, |
| 119 | + partition: PartitionInfo, |
| 120 | + ) -> None: |
| 121 | + """Called before a partition is detached from its parent table. |
| 122 | +
|
| 123 | + This is a good place to export or archive data while the partition |
| 124 | + is still accessible via the parent table's indexes and constraints. |
| 125 | +
|
| 126 | + Args: |
| 127 | + table_name: Parent table name. |
| 128 | + partition: Info about the partition being detached. |
| 129 | + """ |
| 130 | + |
| 131 | + def after_detach( |
| 132 | + self, |
| 133 | + table_name: str, |
| 134 | + partition_name: str, |
| 135 | + ) -> None: |
| 136 | + """Called after a partition has been detached. |
| 137 | +
|
| 138 | + Args: |
| 139 | + table_name: Parent table name. |
| 140 | + partition_name: Name of the detached partition. |
| 141 | + """ |
| 142 | + |
| 143 | + def before_drop( |
| 144 | + self, |
| 145 | + table_name: str, |
| 146 | + partition_name: str, |
| 147 | + ) -> None: |
| 148 | + """Called before a partition table is dropped. |
| 149 | +
|
| 150 | + This is the last chance to read or export data from the partition |
| 151 | + before it is permanently destroyed. |
| 152 | +
|
| 153 | + Args: |
| 154 | + table_name: Parent table name. |
| 155 | + partition_name: Name of the partition about to be dropped. |
| 156 | + """ |
| 157 | + |
| 158 | + def after_drop( |
| 159 | + self, |
| 160 | + table_name: str, |
| 161 | + partition_name: str, |
| 162 | + ) -> None: |
| 163 | + """Called after a partition table has been dropped. |
| 164 | +
|
| 165 | + Args: |
| 166 | + table_name: Parent table name. |
| 167 | + partition_name: Name of the dropped partition. |
| 168 | + """ |
0 commit comments