GeezerVison is showing github.com — fix(stats): make unique users migration idempotent · wxk/opencode@6f0e934
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .github/workflows/deploy.yml +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 47 | 47 | VITE_SENTRY_DSN: ${{ vars.WEB_SENTRY_DSN }} | 48 | 48 | VITE_SENTRY_RELEASE: web@${{ github.sha }} | 49 | 49 |
| 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 | 51 | 51 | env: | 52 | 52 | CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} | 53 | 53 | PLANETSCALE_SERVICE_TOKEN_NAME: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_NAME }} | |
Collapse file Copy file name to clipboardExpand all lines: packages/stats/core/package.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 17 | 17 | }, | 18 | 18 | "scripts": { | 19 | 19 | "db:generate": "drizzle-kit generate --config=drizzle.config.ts", | | 20 | + "db:ensure-unique-users": "bun src/ensure-unique-users.ts", | 20 | 21 | "db:migrate": "bun src/migrate.ts", | 21 | 22 | "db:push": "drizzle-kit push --config=drizzle.config.ts", | 22 | 23 | "db:studio": "drizzle-kit studio --config=drizzle.config.ts", | |
Collapse file Copy file name to clipboardExpand all lines: packages/stats/core/src/athena.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,7 +17,7 @@ export type AthenaData = Record<string, string> | 17 | 17 | export class AthenaQueryError extends Schema.TaggedErrorClass<AthenaQueryError>()("AthenaQueryError", { | 18 | 18 | message: Schema.String, | 19 | 19 | queryExecutionId: Schema.optional(Schema.String), | 20 | | - cause: Schema.optional(Schema.Defect()), | | 20 | + cause: Schema.optional(Schema.Defect), | 21 | 21 | }) {} | 22 | 22 |
| 23 | 23 | export class AthenaQueryTimeoutError extends Schema.TaggedErrorClass<AthenaQueryTimeoutError>()( | |
Collapse file Copy file name to clipboardExpand all lines: packages/stats/core/src/database.ts +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -45,14 +45,14 @@ export class DrizzleClient extends Context.Service<DrizzleClient, Drizzle>()("@o | 45 | 45 | } | 46 | 46 |
| 47 | 47 | export class DatabaseError extends Schema.TaggedErrorClass<DatabaseError>()("DatabaseError", { | 48 | | - cause: Schema.Defect(), | | 48 | + cause: Schema.Defect, | 49 | 49 | }) {} | 50 | 50 |
| 51 | 51 | export const catchDbError = Effect.mapError((cause) => DatabaseError.make({ cause })) | 52 | 52 |
| 53 | 53 | export class MigrationError extends Schema.TaggedErrorClass<MigrationError>()("MigrationError", { | 54 | 54 | message: Schema.String, | 55 | | - cause: Schema.optional(Schema.Defect()), | | 55 | + cause: Schema.optional(Schema.Defect), | 56 | 56 | }) {} | 57 | 57 |
| 58 | 58 | export const migrate = Effect.fn("Database.migrate")(function* () { | |
Collapse file Copy file name to clipboard +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +} |
|