({
tableSchema,
data,
}: {
tableSchema: DatabaseTableSchema;
data: OptimizeTableState;
})
| 13 | } |
| 14 | |
| 15 | function generateTableChangePlan({ |
| 16 | tableSchema, |
| 17 | data, |
| 18 | }: { |
| 19 | tableSchema: DatabaseTableSchema; |
| 20 | data: OptimizeTableState; |
| 21 | }): ExecutePlan[] { |
| 22 | const rowChangeList = data.getChangedRows(); |
| 23 | const plans: ExecutePlan[] = []; |
| 24 | |
| 25 | for (const row of rowChangeList) { |
| 26 | const rowChange = row.change; |
| 27 | if (rowChange) { |
| 28 | const pk = tableSchema.pk; |
| 29 | |
| 30 | const wherePrimaryKey = pk.reduce<Record<string, unknown>>( |
| 31 | (condition, pkColumnName) => { |
| 32 | condition[pkColumnName] = row.raw[pkColumnName]; |
| 33 | return condition; |
| 34 | }, |
| 35 | {} |
| 36 | ); |
| 37 | |
| 38 | if (row.isNewRow) { |
| 39 | plans.push({ |
| 40 | row, |
| 41 | plan: { |
| 42 | operation: "INSERT", |
| 43 | values: rowChange, |
| 44 | autoIncrementPkColumn: tableSchema.autoIncrement |
| 45 | ? tableSchema.pk[0] |
| 46 | : undefined, |
| 47 | pk: tableSchema.pk, |
| 48 | }, |
| 49 | }); |
| 50 | } else if (row.isRemoved) { |
| 51 | plans.push({ |
| 52 | row, |
| 53 | plan: { operation: "DELETE", where: wherePrimaryKey }, |
| 54 | }); |
| 55 | } else { |
| 56 | plans.push({ |
| 57 | row, |
| 58 | plan: { |
| 59 | operation: "UPDATE", |
| 60 | where: wherePrimaryKey, |
| 61 | values: rowChange, |
| 62 | }, |
| 63 | }); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return plans; |
| 69 | } |
| 70 | |
| 71 | export async function commitChange({ |
| 72 | driver, |
no test coverage detected