GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(stats): make unique users migration idempotent · wxk/opencode@6f0e934

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • .github/workflows
    • deploy.yml
  • packages/stats/core
    • package.json
    • src
      • athena.ts
      • database.ts
      • ensure-unique-users.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎.github/workflows/deploy.yml‎

Copy file name to clipboardExpand all lines: .github/workflows/deploy.yml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
VITE_SENTRY_DSN: ${{ vars.WEB_SENTRY_DSN }}
4848
VITE_SENTRY_RELEASE: web@${{ github.sha }}
4949

50-
- run: bun sst shell --stage=${{ github.ref_name }} -- bun run --cwd packages/stats/core db:migrate
50+
- run: bun sst shell --stage=${{ github.ref_name }} -- bun run --cwd packages/stats/core db:ensure-unique-users
5151
env:
5252
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
5353
PLANETSCALE_SERVICE_TOKEN_NAME: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_NAME }}
Collapse file

‎packages/stats/core/package.json‎

Copy file name to clipboardExpand all lines: packages/stats/core/package.json
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"scripts": {
1919
"db:generate": "drizzle-kit generate --config=drizzle.config.ts",
20+
"db:ensure-unique-users": "bun src/ensure-unique-users.ts",
2021
"db:migrate": "bun src/migrate.ts",
2122
"db:push": "drizzle-kit push --config=drizzle.config.ts",
2223
"db:studio": "drizzle-kit studio --config=drizzle.config.ts",
Collapse file

‎packages/stats/core/src/athena.ts‎

Copy file name to clipboardExpand all lines: packages/stats/core/src/athena.ts
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export type AthenaData = Record<string, string>
1717
export class AthenaQueryError extends Schema.TaggedErrorClass<AthenaQueryError>()("AthenaQueryError", {
1818
message: Schema.String,
1919
queryExecutionId: Schema.optional(Schema.String),
20-
cause: Schema.optional(Schema.Defect()),
20+
cause: Schema.optional(Schema.Defect),
2121
}) {}
2222

2323
export class AthenaQueryTimeoutError extends Schema.TaggedErrorClass<AthenaQueryTimeoutError>()(
Collapse file

‎packages/stats/core/src/database.ts‎

Copy file name to clipboardExpand all lines: packages/stats/core/src/database.ts
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ export class DrizzleClient extends Context.Service<DrizzleClient, Drizzle>()("@o
4545
}
4646

4747
export class DatabaseError extends Schema.TaggedErrorClass<DatabaseError>()("DatabaseError", {
48-
cause: Schema.Defect(),
48+
cause: Schema.Defect,
4949
}) {}
5050

5151
export const catchDbError = Effect.mapError((cause) => DatabaseError.make({ cause }))
5252

5353
export class MigrationError extends Schema.TaggedErrorClass<MigrationError>()("MigrationError", {
5454
message: Schema.String,
55-
cause: Schema.optional(Schema.Defect()),
55+
cause: Schema.optional(Schema.Defect),
5656
}) {}
5757

5858
export const migrate = Effect.fn("Database.migrate")(function* () {
Collapse file

‎packages/stats/core/src/ensure-unique-users.ts‎

Copy file name to clipboard
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Client } from "@planetscale/database"
2+
import { Resource } from "sst/resource"
3+
4+
const tables = ["geo_stat", "model_stat", "provider_stat"] as const
5+
6+
const client = new Client({ url: Resource.StatsDatabase.url })
7+
8+
await tables.reduce(
9+
(promise, table) => promise.then(() => ensureUniqueUsersColumn(table)),
10+
Promise.resolve(),
11+
)
12+
13+
async function ensureUniqueUsersColumn(table: (typeof tables)[number]) {
14+
const result = await client.execute<{ column_name: string }>(
15+
"SELECT column_name FROM information_schema.columns WHERE table_schema = database() AND table_name = ? AND column_name = 'unique_users'",
16+
[table],
17+
)
18+
19+
if (result.rows.length > 0) {
20+
console.log(`unique_users column already exists on ${table}`)
21+
return
22+
}
23+
24+
await client.execute(`ALTER TABLE \`${table}\` ADD \`unique_users\` bigint NOT NULL DEFAULT 0`)
25+
console.log(`added unique_users column to ${table}`)
26+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.