Skip to content

Commit

Permalink
fix: migrate KV for #245 (#248)
Browse files Browse the repository at this point in the history
* fix: migrate KV for #245

* fix
  • Loading branch information
iuioiua authored May 31, 2023
1 parent 9ca2661 commit 6e75b10
Showing 1 changed file with 2 additions and 12 deletions.
14 changes: 2 additions & 12 deletions tools/migrate_kv.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
// Copyright 2023 the Deno authors. All rights reserved. MIT license.
import { type Item, kv } from "@/utils/db.ts";

function incrementMonth(oldDate: string) {
const oldDateParts = oldDate.split("-").map((part) => parseInt(part));
oldDateParts[1] = oldDateParts[1] + 1;
const newDate = new Date(oldDateParts.join("-"));
return newDate.toISOString().split("T")[0];
}

export async function migrateKv() {
const iter = kv.list<Item>({ prefix: ["visits"] });
const iter = kv.list<Item>({ prefix: ["items_by_time"] });
const promises = [];
for await (const res of iter) {
promises.push(kv.delete(res.key));
const oldDate = res.key.at(-1);
const newDate = incrementMonth(oldDate as string);
promises.push(kv.set([res.key[0], newDate], res.value));
promises.push(kv.set(["items", res.value.id], res.value));
}
await Promise.all(promises);
}
Expand Down

0 comments on commit 6e75b10

Please sign in to comment.