hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits
| 1 | //! The `fee_plan` table: the create, the reads, and the guarded edit and |
| 2 | //! delete. Both guards are conditional single-record writes on the plan's own |
| 3 | //! refcount (`assignment_count`) — the WHERE clause *is* the assigned-plan |
| 4 | //! freeze rule — so they live here whole. The web-facing doors are |
| 5 | //! [`crate::service::fee_plan`]. |
| 6 | //! |
| 7 | //! Postgres replaces the old process lock with the row itself: the assign's |
| 8 | //! claim (`UPDATE fee_plan SET assignment_count = …` in |
| 9 | //! [`crate::db::fee_plan_assignment::create`]) and the guarded edit/delete |
| 10 | //! below all rewrite this one row, so they take its row lock and re-check |
| 11 | //! their condition against the committed winner — an assign racing an edit |
| 12 | //! either claims first (the edit is refused) or claims after (and bills the |
| 13 | //! edited plan, which it re-reads). No window, no mutex. |
| 14 | |
| 15 | use crateDatabase; |
| 16 | use cratePagedList; |
| 17 | use crate |
| 18 | |
| 19 | ; |
| 20 | use crateTimestamp; |
| 21 | use crateUserId; |
| 22 | use crateAppError; |
| 23 | |
| 24 | pub async |
| 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 | pub async |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | /// Every plan, newest first — a school runs a handful. |
| 64 | pub async |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | /// Write only the fields the PATCH carried — `None` means the request |
| 75 | /// omitted it, so the column is left alone (`COALESCE` keeps the stored |
| 76 | /// value) rather than re-stated from the snapshot this struct was read into. |
| 77 | /// Zero rows = refused, nothing was written: somebody is already on the plan. |
| 78 | /// |
| 79 | /// Editing a plan never moves money: charges are frozen copies of the |
| 80 | /// installments as they stood when the plan was assigned. That is exactly |
| 81 | /// why an assigned plan may not be edited at all — the plan and the charges |
| 82 | /// raised from it would tell different stories. The roster is the plan's own |
| 83 | /// `assignment_count` refcount, claimed in the same statement as the |
| 84 | /// assignment row ([`crate::db::fee_plan_assignment::create`]), so the check |
| 85 | /// and the write are one conditional `UPDATE` on one record: an assign |
| 86 | /// racing this either claims first (and the edit is refused) or claims |
| 87 | /// after (and bills the edited plan, which it re-reads). A |
| 88 | /// `SELECT`-then-write could be, and was, stepped over in the gap between |
| 89 | /// the two. |
| 90 | /// |
| 91 | /// A request that carried no field at all is answered before the guarded |
| 92 | /// statement: it writes nothing (and must not be refused as "assigned"), |
| 93 | /// so the row is simply read back. |
| 94 | pub async |
| 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 | /// Delete the plan, but only while nobody is on it. `false` = refused, |
| 140 | /// nothing was written; `Err(NotFound)` keeps the answer a concurrent |
| 141 | /// *delete* gives. Same one-record guard as [`update`], and the |
| 142 | /// same reason — the charges an assignment raised name this plan, and a |
| 143 | /// school's financial history keeps its references. |
| 144 | /// |
| 145 | /// A single guarded statement needs no retry loop: the guard rewrites the |
| 146 | /// plan row itself, so it contends with an assign's claim on that row's lock |
| 147 | /// and whichever version lands, the loser re-evaluated against the winner's |
| 148 | /// committed state. Nothing was written on a loss, so there is nothing to |
| 149 | /// unwind. |
| 150 | pub async |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 |