Skip to content

Commit

Permalink
migrate over to new db schema
Browse files Browse the repository at this point in the history
  • Loading branch information
QPixel committed Apr 3, 2024
1 parent 36f25e6 commit 24eb0fd
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 7 deletions.
13 changes: 13 additions & 0 deletions migrations/0001-fix-timestamps.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- SQLITE

ALTER TABLE Womps ADD COLUMN last_updated_str TEXT;
UPDATE Womps SET last_updated_str = last_updated;

ALTER TABLE Womps ADD COLUMN last_updated_date integer;

UPDATE Womps SET last_updated_date = strftime('%s', last_updated_str);

ALTER TABLE Womps DROP COLUMN last_updated;
ALTER TABLE Womps ADD COLUMN last_updated integer;
UPDATE Womps SET last_updated = last_updated_date;
ALTER TABLE Womps DROP COLUMN last_updated_date;
1 change: 1 addition & 0 deletions src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { text, integer, sqliteTable } from "drizzle-orm/sqlite-core";

export const Womps = sqliteTable("Womps",{
last_updated: integer("last_updated", {mode: "timestamp"}).notNull().default(sql`CURRENT_TIMESTAMP`),
last_updated_str: text("last_updated_str").notNull().default(""),
id: integer("id").notNull().primaryKey(),
updated_by: integer("updated_by").notNull().default(0),
quarter_id: text("quarter_id").notNull().default("24-Q1"),
Expand Down
3 changes: 0 additions & 3 deletions src/pages/api/counter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { APIRoute } from "astro";
// import { Womps, db, eq, sql } from "astro:db";
import { compareAsc } from "date-fns";
import type { KVNamespace } from "@cloudflare/workers-types";
import { getDB } from "src/db/drizzle";
Expand All @@ -9,8 +8,6 @@ import { eq, sql } from "drizzle-orm";

export const prerendered = false;



async function resolveUsernameFromId(id: number, kv: KVNamespace) {
// resolve the async promise inline
const username = await kv.get<string>(`user:${id}`)
Expand Down
2 changes: 0 additions & 2 deletions src/pages/api/leaderboard/all.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { APIContext, APIRoute } from "astro";
// import { db, desc, eq, sql, Womps } from "astro:db";
import { getDB } from "src/db/drizzle";
import { Womps } from "src/db/schema";
import type { KVNamespace } from "@cloudflare/workers-types";
import type { ENV } from "src/env";
import { desc, sql } from "drizzle-orm";

Expand Down
2 changes: 0 additions & 2 deletions src/pages/api/leaderboard/quarter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { APIRoute } from "astro";
import type { KVNamespace } from "@cloudflare/workers-types";
// import { db, desc, eq, sql, Womps } from "astro:db";
import { getDB } from "src/db/drizzle";
import type { ENV } from "src/env";
import { Womps } from "src/db/schema";
Expand Down
41 changes: 41 additions & 0 deletions womp-womp.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS "Womps" ("last_updated" text NOT NULL DEFAULT '2024-04-03T00:27:19.299Z', "id" integer PRIMARY KEY, "updated_by" integer NOT NULL DEFAULT 0, "quarter_id" text NOT NULL DEFAULT '24-Q1');
INSERT INTO Womps VALUES('2024-03-17T07:00:00.000Z',0,0,'24-Q1');
INSERT INTO Womps VALUES('2024-03-17T22:55:00.000Z',1,0,'24-Q1');
INSERT INTO Womps VALUES('2024-03-17T22:55:00.000Z',2,42069,'24-Q1');
INSERT INTO Womps VALUES('2024-03-17T22:55:00.000Z',3,42069,'24-Q1');
INSERT INTO Womps VALUES('2024-03-17T22:55:00.000Z',4,42069,'24-Q1');
INSERT INTO Womps VALUES('2024-03-17T22:55:00.000Z',5,42069,'24-Q1');
INSERT INTO Womps VALUES('2024-03-17T22:55:00.000Z',6,42069,'24-Q1');
INSERT INTO Womps VALUES('2024-03-17T22:55:00.000Z',7,42069,'24-Q1');
INSERT INTO Womps VALUES('2024-03-18T18:35:00.000Z',8,1,'24-Q1');
INSERT INTO Womps VALUES('2024-03-19T18:45:52.792Z',9,2,'24-Q1');
INSERT INTO Womps VALUES('2024-03-19T18:45:53.906Z',10,2,'24-Q1');
INSERT INTO Womps VALUES('2024-03-19T22:00:18.874Z',11,2,'24-Q1');
INSERT INTO Womps VALUES('2024-03-20T02:10:00.000Z',12,3,'24-Q1');
INSERT INTO Womps VALUES('2024-03-20T16:22:30.990Z',13,2,'24-Q1');
INSERT INTO Womps VALUES('2024-03-20T18:28:00.000Z',14,3,'24-Q1');
INSERT INTO Womps VALUES('2024-03-22T05:49:33.092Z',15,3,'24-Q1');
INSERT INTO Womps VALUES('2024-03-23T02:16:20.973Z',16,2,'24-Q1');
INSERT INTO Womps VALUES('2024-03-24T22:28:16.454Z',17,2,'24-Q1');
INSERT INTO Womps VALUES('2024-03-24T22:28:18.382Z',18,2,'24-Q1');
INSERT INTO Womps VALUES('2024-03-24T22:28:20.473Z',19,2,'24-Q1');
INSERT INTO Womps VALUES('2024-03-24T22:28:21.822Z',20,2,'24-Q1');
INSERT INTO Womps VALUES('2024-03-25T00:04:41.366Z',21,2,'24-Q1');
INSERT INTO Womps VALUES('2024-03-26T02:47:09.804Z',22,2,'24-Q1');
INSERT INTO Womps VALUES('2024-03-26T02:47:15.774Z',23,2,'24-Q1');
INSERT INTO Womps VALUES('2024-03-27T02:19:24.630Z',24,2,'24-Q1');
INSERT INTO Womps VALUES('2024-03-27T17:27:47.296Z',25,2,'24-Q1');
INSERT INTO Womps VALUES('2024-03-27T22:47:31.414Z',26,3,'24-Q1');
INSERT INTO Womps VALUES('2024-03-28T20:02:17.662Z',27,3,'24-Q1');
INSERT INTO Womps VALUES('2024-03-29T03:59:55.278Z',28,3,'24-Q1');
INSERT INTO Womps VALUES('2024-03-29T04:37:13.238Z',29,3,'24-Q1');
INSERT INTO Womps VALUES('2024-03-30T00:49:30.364Z',30,3,'24-Q1');
INSERT INTO Womps VALUES('2024-03-30T05:43:09.993Z',31,3,'24-Q1');
INSERT INTO Womps VALUES('2024-03-31T16:43:20.373Z',32,0,'24-Q1');
INSERT INTO Womps VALUES('2024-04-01T00:08:00.000Z',33,1,'24-Q1');
INSERT INTO Womps VALUES('2024-04-01T15:28:55.398Z',34,0,'24-Q2');
INSERT INTO Womps VALUES('2024-04-01T21:59:31.313Z',35,2,'24-Q2');
INSERT INTO Womps VALUES('2024-04-02T03:52:33.760Z',36,1,'24-Q2');
COMMIT;
1 change: 1 addition & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ compatibility_flags = [ "nodejs_compat" ]
binding = "WOMP_DB"
database_name = "WOMP_DB"
database_id = "b32bf941-f600-4779-ac2c-04cdbc7afa43"
preview_database_id = "local_test_db"

# Bind a KV Namespace. Use KV as persistent storage for small key-value pairs.
# Docs: https://developers.cloudflare.com/pages/functions/bindings/#kv-namespaces
Expand Down

0 comments on commit 24eb0fd

Please sign in to comment.