Commit c32a72d
authored
Add
* Add list metadata repository to wp_mobile_cache
Introduces database infrastructure for tracking list metadata, enabling
efficient cache invalidation and stale detection for paginated lists.
Changes:
- Add migration for list metadata tables
- Add DbListMetadata types for database operations
- Add ListMetadata domain types
- Add ListMetadataRepository with full CRUD operations
- Update PostsRepository with stale detection helpers
Extracted from prototype/metadata-collection branch. Original commits:
- 14b01d80 (Implement stale detection by comparing modified_gmt timestamps)
- e47cec89 (Add database foundation for MetadataService)
- 2440a13f (Add list metadata repository concurrency helpers)
- cc0c8a58 (Reset stale fetching states on app launch)
- d64142fb (Split collection observers for data vs state updates)
- fe7435c9 (make fmt-rust)
- 2918b339 (Add parent and menu_order fields to list metadata items)
- 25f88a49 (Rename last_updated_at to last_fetched_at in list metadata)
- 1d709e70 (Simplify ListMetadataReader trait with combined ListInfo query)
Note: Since we use a rebase/squash merge strategy, these commits may show
"does not belong to any branch" on GitHub but remain accessible via URL.
* Store ListState as INTEGER instead of TEXT
Changes the database storage for `ListState` from TEXT strings to INTEGER
values for better performance and type safety.
Changes:
- Update migration to use `INTEGER NOT NULL DEFAULT 0` for state column
- Add `#[repr(i32)]` to `ListState` enum with explicit discriminant values
- Implement `ToSql` and `FromSql` traits for direct rusqlite integration
- Remove string-based `as_db_str()` and `From<&str>` implementations
- Update all callers to use the enum directly with rusqlite params
* Return error for invalid ListState values instead of silent fallback
The `FromSql` implementation now returns a proper error when encountering
an unknown integer value, rather than silently defaulting to `Idle`.
This makes data corruption issues visible instead of hiding them.
* Convert ListMetadataRepository methods to associated functions
The repository struct has no state, so methods are converted from
instance methods (&self) to associated functions. This removes the
need to instantiate the struct before calling methods.
Before: ListMetadataRepository.get_header(&conn, &site, key)
After: ListMetadataRepository::get_header(&conn, &site, key)
* Use batch insert for list metadata items
Replaces individual INSERT statements with a single batch INSERT for
better performance when inserting multiple items. Uses functional style
with try_for_each and flat_map.
Items are chunked to stay under SQLite's variable limit (999).
* Use JOIN query internally in get_state_by_key
Replaces two separate queries (get_header + get_state) with a single
JOIN query via get_header_with_state for better efficiency.
* Add ListKey newtype for type-safe list key handling
Replaces raw `&str` parameters with `&ListKey` throughout the repository
API. This prevents accidental misuse of arbitrary strings as list keys
and makes the API more self-documenting.
The ListKey type provides:
- Type safety at compile time
- Conversion from &str and String via From trait
- as_str() for SQL parameter usage
- Display impl for debug output
* Simplify reset_stale_fetching_states and return Result
- Condense doc comment, remove references to non-existent MetadataService
- Return Result<usize, SqliteDbError> instead of logging internally
- Ignore errors at call site with explanatory comment
* Add FK from list_metadata_items to list_metadata
Normalizes the schema by replacing (db_site_id, key) in items table
with a foreign key to list_metadata. This:
- Ensures referential integrity
- Enables cascade delete (simplifies delete_list)
- Reduces storage per item
* Add log crate for structured logging
Adds the `log` facade crate to enable proper logging. Debug logs added
to key list metadata operations:
- begin_refresh, begin_fetch_next_page
- complete_sync, complete_sync_with_error
- set_items, append_items, delete_list
Replaces eprintln! with log::warn! for unknown table warnings.
* Add ToSql/FromSql for ListKey and log stale state reset errors
Implement ToSql/FromSql traits for ListKey to simplify repository
code by using the type directly in SQL params instead of .as_str().
Also add logging for reset_stale_fetching_states failures instead
of silently ignoring errors.
Changes:
- Add ToSql/FromSql implementations for ListKey
- Replace key.as_str() with key in all SQL params
- Log warning on reset_stale_fetching_states failure
* Remove unused update hook helper functions
* Remove select_modified_gmt_by_ids from this PR
* Split get_items and get_item_count into by_list_metadata_id and by_list_key variants
Allows callers who already have the list_metadata_id to skip an extra
header lookup query.
* Remove redundant check_version function
* Add by_list_metadata_id variants for write operations
Split functions that take site+key to allow callers with an existing
list_metadata_id to skip the header lookup query.
Renamed functions for consistency:
- set_items → set_items_by_list_metadata_id / set_items_by_list_key
- append_items → append_items_by_list_metadata_id / append_items_by_list_key
- update_header → update_header_by_list_metadata_id / update_header_by_list_key
- increment_version → increment_version_by_list_metadata_id / increment_version_by_list_key
- get_state → get_state_by_list_metadata_id
- get_state_by_key → get_state_by_list_key
- update_state → update_state_by_list_metadata_id
- update_state_by_key → update_state_by_list_key
* Replace unwrap with expect for better panic messages
* Update migration count in Kotlin and Swift tests
* Handle race condition in get_or_create
Add `ConstraintViolation` variant to `SqliteDbError` to distinguish
UNIQUE constraint violations from other SQLite errors. Use this in
`ListMetadataRepository::get_or_create` to handle the rare case where
another thread creates the same header between our SELECT and INSERT.
Changes:
- Add `SqliteDbError::ConstraintViolation` variant
- Update `From<rusqlite::Error>` to detect constraint violations
- Update `get_or_create` to catch constraint violations and re-fetch
* Add optimized get_or_create_and_increment_version method
Add `get_or_create_and_increment_version` that uses a single
`INSERT ... ON CONFLICT DO UPDATE ... RETURNING` query to atomically
create or update a header while incrementing its version.
This reduces `begin_refresh` from 5-6 queries to 2 queries:
- Before: get_or_create (1-2) + increment_version (2) + update_state (1) + get_header (1)
- After: get_or_create_and_increment_version (1) + update_state (1)
Changes:
- Add `HeaderVersionInfo` struct for the return type
- Add `get_or_create_and_increment_version` method using RETURNING clause
- Update `begin_refresh` to use the new optimized method
- Add tests for the new method
* Remove standalone increment_version methods
Remove `increment_version_by_list_metadata_id` and `increment_version_by_list_key`
since version increment should only happen through `get_or_create_and_increment_version`
as part of a proper refresh flow, not called arbitrarily.
* Remove concurrency helpers from repository layer
Move concurrency orchestration logic out of the repository layer.
These helpers (`begin_refresh`, `begin_fetch_next_page`, `complete_sync`,
`complete_sync_with_error`) belong in a service layer since they:
- Orchestrate multiple repository operations
- Apply business rules (checking page counts, deciding state transitions)
- Return domain-specific result types
The repository now provides clean primitives that a service layer can compose:
- `get_or_create_and_increment_version` for atomic create/increment
- `update_state_by_list_metadata_id` for state changes
- `get_header`, `get_items`, etc. for reads
* Move reset_stale_fetching_states to ListMetadataRepository
Move `reset_stale_fetching_states_internal` from `WpApiCache` to
`ListMetadataRepository` where it belongs. The repository owns
the state table and should provide all operations on it.
`WpApiCache::perform_migrations` still calls this method after
migrations complete, but now delegates to the repository.
* Make per_page a required parameter with validation
The repository layer should not dictate per_page values - this
must be set by the service layer to match networking configuration.
Changes:
- Add PerPageMismatch error variant to SqliteDbError
- Make per_page a required parameter in get_or_create
- Make per_page required in get_or_create_and_increment_version
- Update set_items_by_list_key to require per_page
- Update append_items_by_list_key to require per_page
- Update update_state_by_list_key to require per_page
- Return error when existing header has different per_page
* Use PRAGMA-based column enum tests for list_metadata
Update schema validation tests to use `get_table_column_names` helper,
which verifies that column enum indices match actual database schema
positions via PRAGMA table_info.
This matches the pattern used by posts repository tests and provides
stronger guarantees that the column enums won't break if migrations
reorder columns.ListMetadataRepository for paginated list state tracking (#1069)1 parent 2389f4d commit c32a72d
File tree
12 files changed
+1684
-5
lines changed- native
- kotlin/api/kotlin/src/integrationTest/kotlin
- swift/Tests/wordpress-api-cache
- wp_mobile_cache
- migrations
- src
- db_types
- repository
12 files changed
+1684
-5
lines changedSome generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
48 | 49 | | |
49 | 50 | | |
50 | 51 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
14 | 14 | | |
15 | 15 | | |
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
Lines changed: 45 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
6 | 8 | | |
7 | 9 | | |
8 | 10 | | |
| 11 | + | |
9 | 12 | | |
10 | 13 | | |
11 | 14 | | |
| |||
15 | 18 | | |
16 | 19 | | |
17 | 20 | | |
| 21 | + | |
18 | 22 | | |
| 23 | + | |
19 | 24 | | |
20 | 25 | | |
21 | 26 | | |
22 | 27 | | |
23 | 28 | | |
24 | 29 | | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
25 | 33 | | |
26 | 34 | | |
27 | 35 | | |
| |||
30 | 38 | | |
31 | 39 | | |
32 | 40 | | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
33 | 48 | | |
34 | 49 | | |
35 | 50 | | |
36 | 51 | | |
37 | 52 | | |
38 | 53 | | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
39 | 59 | | |
40 | 60 | | |
41 | 61 | | |
| |||
64 | 84 | | |
65 | 85 | | |
66 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
67 | 93 | | |
68 | 94 | | |
69 | 95 | | |
| |||
79 | 105 | | |
80 | 106 | | |
81 | 107 | | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
82 | 111 | | |
83 | 112 | | |
84 | 113 | | |
| |||
107 | 136 | | |
108 | 137 | | |
109 | 138 | | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
110 | 142 | | |
111 | 143 | | |
112 | 144 | | |
| |||
249 | 281 | | |
250 | 282 | | |
251 | 283 | | |
252 | | - | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
253 | 295 | | |
254 | 296 | | |
255 | 297 | | |
| |||
271 | 313 | | |
272 | 314 | | |
273 | 315 | | |
274 | | - | |
| 316 | + | |
275 | 317 | | |
276 | 318 | | |
277 | 319 | | |
| |||
350 | 392 | | |
351 | 393 | | |
352 | 394 | | |
353 | | - | |
| 395 | + | |
354 | 396 | | |
355 | 397 | | |
356 | 398 | | |
357 | 399 | | |
358 | 400 | | |
359 | 401 | | |
| 402 | + | |
360 | 403 | | |
361 | 404 | | |
362 | 405 | | |
| |||
0 commit comments