|
22 | 22 | from pg_partsmith.aio.hooks import BasePartitionLifecycleHooks |
23 | 23 | from pg_partsmith.aio.metadata import PostgresMetadataProvider |
24 | 24 | from pg_partsmith.aio.repositories import PostgresPartitionRepository |
| 25 | +from pg_partsmith.aio.repositories.creator import PartitionCreator |
25 | 26 | from pg_partsmith.entities import MaintenanceIssueStep, Period |
26 | 27 | from pg_partsmith.events import PartitionEvent |
27 | 28 | from pg_partsmith.exceptions import InvalidPartitionConfigError, PlanStaleError |
@@ -220,6 +221,65 @@ async def test__partition_data__attach_not_granted_its_lock__rows_stay_visible_a |
220 | 221 | assert await _count(db_engine, events) == 25 |
221 | 222 |
|
222 | 223 |
|
| 224 | +# sync-mirror: skip |
| 225 | +async def test__partition_data__an_insert_arriving_during_the_attach__lands_in_the_new_partition( |
| 226 | + db_engine: AsyncEngine, events: str |
| 227 | +) -> None: |
| 228 | + """The attach that ends a window keeps writers out at the parent, even with nothing left to move. |
| 229 | +
|
| 230 | + An insert that got past the parent while the attach was in flight would |
| 231 | + have chosen DEFAULT from the tree as it was, queued on DEFAULT's lock, and |
| 232 | + been rejected by the constraint the attach had just narrowed. |
| 233 | + """ |
| 234 | + # Arrange: March in DEFAULT, and the window's attach held open once it has run |
| 235 | + default = await _default_with_rows(db_engine, events, months=(3,), per_month=10) |
| 236 | + config = monthly_config(events, create_ahead=1) |
| 237 | + march = f"{events}__2026_03" |
| 238 | + attaching = asyncio.Event() |
| 239 | + release = asyncio.Event() |
| 240 | + original = PartitionCreator._clear_orphan_marker |
| 241 | + |
| 242 | + async def held_open(self: PartitionCreator, conn: object, partition_name: str) -> None: |
| 243 | + attaching.set() |
| 244 | + await release.wait() |
| 245 | + await original(self, conn, partition_name) # type: ignore[arg-type] |
| 246 | + |
| 247 | + async def insert_into_march() -> None: |
| 248 | + await attaching.wait() |
| 249 | + await exec_sql( |
| 250 | + db_engine, |
| 251 | + f'INSERT INTO "{events}" (created_at, payload) ' # noqa: S608 |
| 252 | + "VALUES (make_timestamptz(2026, 3, 15, 12, 0, 0, 'UTC'), 'live')", |
| 253 | + ) |
| 254 | + |
| 255 | + async def release_once_the_insert_waits() -> None: |
| 256 | + await attaching.wait() |
| 257 | + for _ in range(200): |
| 258 | + await asyncio.sleep(0.05) |
| 259 | + if await scalar( |
| 260 | + db_engine, |
| 261 | + "SELECT count(*) FROM pg_locks WHERE NOT granted " |
| 262 | + "AND relation IN (to_regclass(:parent), to_regclass(:default))", |
| 263 | + parent=events, |
| 264 | + default=default, |
| 265 | + ): |
| 266 | + break |
| 267 | + release.set() |
| 268 | + |
| 269 | + # Act |
| 270 | + with patch.object(PartitionCreator, "_clear_orphan_marker", held_open): |
| 271 | + result, _, _ = await asyncio.gather( |
| 272 | + make_service(db_engine).partition_data(config), |
| 273 | + insert_into_march(), |
| 274 | + release_once_the_insert_waits(), |
| 275 | + ) |
| 276 | + |
| 277 | + # Assert |
| 278 | + assert result.complete |
| 279 | + assert await _only_count(db_engine, default) == 0 |
| 280 | + assert await _count(db_engine, march) == 11 |
| 281 | + |
| 282 | + |
223 | 283 | async def test__partition_data__a_fill_the_process_died_after__is_reported_then_attached( |
224 | 284 | db_engine: AsyncEngine, events: str |
225 | 285 | ) -> None: |
|
0 commit comments