( trx: DbTransaction, tableId: string, minDeletedPos?: number )
| 145 | * the cheaper {@link shiftRowsDownAfter}. |
| 146 | */ |
| 147 | export async function compactPositions( |
| 148 | trx: DbTransaction, |
| 149 | tableId: string, |
| 150 | minDeletedPos?: number |
| 151 | ) { |
| 152 | if (minDeletedPos === undefined) { |
| 153 | await trx.execute(sql` |
| 154 | UPDATE user_table_rows t |
| 155 | SET position = r.new_pos |
| 156 | FROM ( |
| 157 | SELECT id, ROW_NUMBER() OVER (ORDER BY position) - 1 AS new_pos |
| 158 | FROM user_table_rows |
| 159 | WHERE table_id = ${tableId} |
| 160 | ) r |
| 161 | WHERE t.id = r.id AND t.table_id = ${tableId} AND t.position != r.new_pos |
| 162 | `) |
| 163 | return |
| 164 | } |
| 165 | await trx.execute(sql` |
| 166 | UPDATE user_table_rows t |
| 167 | SET position = r.new_pos |
| 168 | FROM ( |
| 169 | SELECT id, ${minDeletedPos}::int + ROW_NUMBER() OVER (ORDER BY position) - 1 AS new_pos |
| 170 | FROM user_table_rows |
| 171 | WHERE table_id = ${tableId} AND position >= ${minDeletedPos} |
| 172 | ) r |
| 173 | WHERE t.id = r.id AND t.table_id = ${tableId} AND t.position != r.new_pos |
| 174 | `) |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Computes the fractional `order_key` for a row inserted at the integer |
no test coverage detected