From a7a56c16775a2caf8e30b5bc1bee82093e911115 Mon Sep 17 00:00:00 2001 From: Hyo Date: Fri, 20 Nov 2026 23:32:45 +0900 Subject: [PATCH] chore: remove @convex-dev/auth 0.0.x tables and migration Production ran migrateToAuthV2 on 2026-11-20: every GitHub account is on users.githubAccountId and the 0.0.x auth tables are empty. Drop them from the schema, delete the migration and the authAccounts fallback in createUserFromGithub, and stop cleaning the old tables in test:dev. --- convex/_generated/api.d.ts | 2 - convex/schema.ts | 3 +- convex/users/internalMutation.ts | 7 -- convex/users/migrations.ts | 87 ------------- convex/users/tables.ts | 53 -------- packages/cli/scripts/test-dev.mjs | 10 +- test/convex/users/authV2.test.ts | 166 ------------------------- test/convex/users/githubSignIn.test.ts | 70 +++++++++++ 8 files changed, 72 insertions(+), 326 deletions(-) delete mode 100644 convex/users/migrations.ts delete mode 100644 test/convex/users/authV2.test.ts create mode 100644 test/convex/users/githubSignIn.test.ts diff --git a/convex/_generated/api.d.ts b/convex/_generated/api.d.ts index c4054db..d440f59 100644 --- a/convex/_generated/api.d.ts +++ b/convex/_generated/api.d.ts @@ -176,7 +176,6 @@ import type * as users_githubAccount from "../users/githubAccount.js"; import type * as users_internalMutation from "../users/internalMutation.js"; import type * as users_internalQuery from "../users/internalQuery.js"; import type * as users_legal from "../users/legal.js"; -import type * as users_migrations from "../users/migrations.js"; import type * as users_mutation from "../users/mutation.js"; import type * as users_query from "../users/query.js"; import type * as users_tables from "../users/tables.js"; @@ -356,7 +355,6 @@ declare const fullApi: ApiFromModules<{ "users/internalMutation": typeof users_internalMutation; "users/internalQuery": typeof users_internalQuery; "users/legal": typeof users_legal; - "users/migrations": typeof users_migrations; "users/mutation": typeof users_mutation; "users/query": typeof users_query; "users/tables": typeof users_tables; diff --git a/convex/schema.ts b/convex/schema.ts index 2f8c90e..24f8a1c 100644 --- a/convex/schema.ts +++ b/convex/schema.ts @@ -1,6 +1,6 @@ import { defineSchema, defineTable } from "convex/server"; import { v } from "convex/values"; -import { legacyAuthTables, usersTable } from "./users/tables"; +import { usersTable } from "./users/tables"; import { chatRecoveryTargetValidator } from "./_ai/chatRecovery"; import { reviewBatchResultValidator } from "./pullRequests/review"; import { reviewTraceStepValidator } from "./pullRequests/records"; @@ -1856,6 +1856,5 @@ const applicationTables = { export default defineSchema({ users: usersTable, - ...legacyAuthTables, ...applicationTables, }); diff --git a/convex/users/internalMutation.ts b/convex/users/internalMutation.ts index b9b3342..cfea906 100644 --- a/convex/users/internalMutation.ts +++ b/convex/users/internalMutation.ts @@ -2,7 +2,6 @@ import { v } from "convex/values"; import { vGithubProfile } from "@convex-dev/auth/methods/github/server"; import type { GithubProfile } from "@convex-dev/auth/methods/github/server"; import { internalMutation } from "../_generated/server"; -import { readLegacyGitHubUserId } from "./migrations"; const githubSignIn = v.object({ name: v.literal("github"), @@ -36,12 +35,6 @@ export const createUserFromGithub = internalMutation({ ) .unique(); if (existing) return existing._id; - // Signed in after the 2.0 deploy but before migrateToAuthV2 linked them. - const legacy = await readLegacyGitHubUserId(ctx, githubAccountId); - if (legacy) { - await ctx.db.patch(legacy, { githubAccountId }); - return legacy; - } return await ctx.db.insert("users", { ...profileFields(method.profile, Date.now()), githubAccountId: method.profile.id, diff --git a/convex/users/migrations.ts b/convex/users/migrations.ts deleted file mode 100644 index 87c8f26..0000000 --- a/convex/users/migrations.ts +++ /dev/null @@ -1,87 +0,0 @@ -import { v } from "convex/values"; -import type { Id } from "../_generated/dataModel"; -import { internalMutation, type QueryCtx } from "../_generated/server"; - -// Everything here goes once production has run migrateToAuthV2 and the -// legacy tables are dropped from the schema. - -/** - * The user a GitHub account belonged to under @convex-dev/auth 0.0.x, for a - * sign-in that lands before migrateToAuthV2 has linked it. - */ -export async function readLegacyGitHubUserId( - ctx: Pick, - githubAccountId: string, -): Promise | null> { - const account = await ctx.db - .query("authAccounts") - .withIndex("providerAndAccountId", (q) => - q.eq("provider", "github").eq("providerAccountId", githubAccountId), - ) - .unique(); - return account && (await ctx.db.get(account.userId)) ? account.userId : null; -} - -const LEGACY_AUTH_TABLES = [ - "authAccounts", - "authSessions", - "authRefreshTokens", - "authVerificationCodes", - "authVerifiers", - "authRateLimits", -] as const; -const MIGRATION_BATCH = 1_000; - -/** - * One-off move from @convex-dev/auth 0.0.x to 2.0: copy each GitHub account - * onto its user, then delete the old auth rows. Deleting sessions and refresh - * tokens signs every client out. Run it until `done` is true; each run handles - * one bounded batch. - */ -export const migrateToAuthV2 = internalMutation({ - args: {}, - returns: v.object({ - linkedUsers: v.number(), - deletedRows: v.number(), - done: v.boolean(), - }), - handler: async (ctx) => { - const accounts = await ctx.db.query("authAccounts").take(MIGRATION_BATCH); - let linkedUsers = 0; - for (const account of accounts) { - if (account.provider !== "github") continue; - const user = await ctx.db.get(account.userId); - if (!user) continue; - if (user.githubAccountId === account.providerAccountId) continue; - const claimed = await ctx.db - .query("users") - .withIndex("by_github_account_id", (q) => - q.eq("githubAccountId", account.providerAccountId), - ) - .first(); - if (user.githubAccountId !== undefined || claimed) - throw new Error( - `GitHub account ${account.providerAccountId} conflicts with an existing link`, - ); - await ctx.db.patch(user._id, { - githubAccountId: account.providerAccountId, - }); - linkedUsers++; - } - - let deletedRows = 0; - for (const table of LEGACY_AUTH_TABLES) { - const rows = await ctx.db - .query(table) - .take(MIGRATION_BATCH - deletedRows); - for (const row of rows) await ctx.db.delete(row._id); - deletedRows += rows.length; - if (deletedRows >= MIGRATION_BATCH) break; - } - return { - linkedUsers, - deletedRows, - done: deletedRows < MIGRATION_BATCH, - }; - }, -}); diff --git a/convex/users/tables.ts b/convex/users/tables.ts index ddafc0b..51a06b1 100644 --- a/convex/users/tables.ts +++ b/convex/users/tables.ts @@ -17,56 +17,3 @@ export const usersTable = defineTable({ phoneVerificationTime: v.optional(v.number()), isAnonymous: v.optional(v.boolean()), }).index("by_github_account_id", ["githubAccountId"]); - -/** - * The tables of @convex-dev/auth 0.0.x. Kept only until migrateToAuthV2 has - * copied each GitHub account into users.githubAccountId, then removed. The - * GitHub auth component records each account on its first 2.0 sign-in. - */ -export const legacyAuthTables = { - authSessions: defineTable({ - userId: v.id("users"), - expirationTime: v.number(), - }).index("userId", ["userId"]), - authAccounts: defineTable({ - userId: v.id("users"), - provider: v.string(), - providerAccountId: v.string(), - secret: v.optional(v.string()), - emailVerified: v.optional(v.string()), - phoneVerified: v.optional(v.string()), - }) - .index("userIdAndProvider", ["userId", "provider"]) - .index("providerAndAccountId", ["provider", "providerAccountId"]), - authRefreshTokens: defineTable({ - sessionId: v.id("authSessions"), - expirationTime: v.number(), - firstUsedTime: v.optional(v.number()), - parentRefreshTokenId: v.optional(v.id("authRefreshTokens")), - }) - .index("sessionId", ["sessionId"]) - .index("sessionIdAndParentRefreshTokenId", [ - "sessionId", - "parentRefreshTokenId", - ]), - authVerificationCodes: defineTable({ - accountId: v.id("authAccounts"), - provider: v.string(), - code: v.string(), - expirationTime: v.number(), - verifier: v.optional(v.string()), - emailVerified: v.optional(v.string()), - phoneVerified: v.optional(v.string()), - }) - .index("accountId", ["accountId"]) - .index("code", ["code"]), - authVerifiers: defineTable({ - sessionId: v.optional(v.id("authSessions")), - signature: v.optional(v.string()), - }).index("signature", ["signature"]), - authRateLimits: defineTable({ - identifier: v.string(), - lastAttemptTime: v.number(), - attemptsLeft: v.number(), - }).index("identifier", ["identifier"]), -}; diff --git a/packages/cli/scripts/test-dev.mjs b/packages/cli/scripts/test-dev.mjs index b8548df..05cd740 100644 --- a/packages/cli/scripts/test-dev.mjs +++ b/packages/cli/scripts/test-dev.mjs @@ -318,15 +318,7 @@ async function cleanup() { const tables = [...schema.matchAll(/^ (\w+): defineTable\(/gm)].map( (match) => match[1], ); - tables.push( - "users", - "authAccounts", - "authSessions", - "authRefreshTokens", - "authVerificationCodes", - "authVerifiers", - "authRateLimits", - ); + tables.push("users"); const readAll = () => query( `const result={};for(const table of ${JSON.stringify(tables)})result[table]=await ctx.db.query(table).take(1001);return result;`, diff --git a/test/convex/users/authV2.test.ts b/test/convex/users/authV2.test.ts deleted file mode 100644 index e56c32d..0000000 --- a/test/convex/users/authV2.test.ts +++ /dev/null @@ -1,166 +0,0 @@ -import { convexTest } from "convex-test"; -import { describe, expect, it } from "vitest"; -import { internal } from "../../../convex/_generated/api"; -import schema from "../../../convex/schema"; - -const modules = { - "../../../convex/_generated/api.ts": () => - import("../../../convex/_generated/api"), - "../../../convex/users/internalMutation.ts": () => - import("../../../convex/users/internalMutation"), - "../../../convex/users/migrations.ts": () => - import("../../../convex/users/migrations"), -}; - -const profile = (id: string, login = "octocat") => ({ - id, - login, - email: `${login}@example.com`, - emailVerified: true, - avatarUrl: `https://avatars.example/${login}`, -}); - -describe("GitHub sign-in user callbacks", () => { - it("creates one user per GitHub account and reuses a user from before 2.0", async () => { - const t = convexTest(schema, modules); - const existing = await t.run((ctx) => - ctx.db.insert("users", { name: "old", githubAccountId: "654" }), - ); - - const returning = await t.mutation( - internal.users.internalMutation.createUserFromGithub, - { method: { name: "github", profile: profile("654") } }, - ); - const newcomer = await t.mutation( - internal.users.internalMutation.createUserFromGithub, - { method: { name: "github", profile: profile("777", "newcomer") } }, - ); - - expect(returning).toBe(existing); - const users = await t.run((ctx) => ctx.db.query("users").collect()); - expect(users).toHaveLength(2); - expect(users.find((user) => user._id === newcomer)).toMatchObject({ - name: "newcomer", - githubAccountId: "777", - email: "newcomer@example.com", - image: "https://avatars.example/newcomer", - }); - }); - - it("refreshes the profile and refuses a different GitHub account", async () => { - const t = convexTest(schema, modules); - const userId = await t.run((ctx) => - ctx.db.insert("users", { name: "old", githubAccountId: "654" }), - ); - - await t.mutation(internal.users.internalMutation.updateUserFromGithub, { - method: { name: "github", profile: profile("654", "renamed") }, - userId, - }); - await expect( - t.mutation(internal.users.internalMutation.updateUserFromGithub, { - method: { name: "github", profile: profile("999", "attacker") }, - userId, - }), - ).rejects.toThrow("identity changed"); - - expect(await t.run((ctx) => ctx.db.get(userId))).toMatchObject({ - name: "renamed", - githubAccountId: "654", - }); - }); -}); - -describe("@convex-dev/auth 2.0 migration", () => { - it("keeps the account of someone who signs in before the migration runs", async () => { - const t = convexTest(schema, modules); - const userId = await t.run(async (ctx) => { - const userId = await ctx.db.insert("users", { name: "octocat" }); - await ctx.db.insert("authAccounts", { - userId, - provider: "github", - providerAccountId: "654", - }); - return userId; - }); - - const signedIn = await t.mutation( - internal.users.internalMutation.createUserFromGithub, - { method: { name: "github", profile: profile("654") } }, - ); - const migrated = await t.mutation( - internal.users.migrations.migrateToAuthV2, - {}, - ); - - expect(signedIn).toBe(userId); - expect(migrated).toMatchObject({ linkedUsers: 0, done: true }); - await expect( - t.run((ctx) => ctx.db.query("users").collect()), - ).resolves.toEqual([expect.objectContaining({ githubAccountId: "654" })]); - }); - - it("links each GitHub account to its user and deletes the old auth rows", async () => { - const t = convexTest(schema, modules); - const userId = await t.run(async (ctx) => { - const userId = await ctx.db.insert("users", { name: "octocat" }); - await ctx.db.insert("authAccounts", { - userId, - provider: "github", - providerAccountId: "654", - }); - const sessionId = await ctx.db.insert("authSessions", { - userId, - expirationTime: Date.now() + 1_000, - }); - await ctx.db.insert("authRefreshTokens", { - sessionId, - expirationTime: Date.now() + 1_000, - }); - return userId; - }); - - const first = await t.mutation( - internal.users.migrations.migrateToAuthV2, - {}, - ); - const again = await t.mutation( - internal.users.migrations.migrateToAuthV2, - {}, - ); - - expect(first).toEqual({ linkedUsers: 1, deletedRows: 3, done: true }); - expect(again).toEqual({ linkedUsers: 0, deletedRows: 0, done: true }); - expect((await t.run((ctx) => ctx.db.get(userId)))?.githubAccountId).toBe( - "654", - ); - for (const table of [ - "authAccounts", - "authSessions", - "authRefreshTokens", - ] as const) - await expect( - t.run((ctx) => ctx.db.query(table).collect()), - ).resolves.toEqual([]); - }); - - it("stops without deleting anything when a GitHub account is claimed twice", async () => { - const t = convexTest(schema, modules); - await t.run(async (ctx) => { - await ctx.db.insert("users", { githubAccountId: "654" }); - const other = await ctx.db.insert("users", {}); - await ctx.db.insert("authAccounts", { - userId: other, - provider: "github", - providerAccountId: "654", - }); - }); - - await expect( - t.mutation(internal.users.migrations.migrateToAuthV2, {}), - ).rejects.toThrow("conflicts"); - await expect( - t.run((ctx) => ctx.db.query("authAccounts").collect()), - ).resolves.toHaveLength(1); - }); -}); diff --git a/test/convex/users/githubSignIn.test.ts b/test/convex/users/githubSignIn.test.ts new file mode 100644 index 0000000..d0a5f0e --- /dev/null +++ b/test/convex/users/githubSignIn.test.ts @@ -0,0 +1,70 @@ +import { convexTest } from "convex-test"; +import { describe, expect, it } from "vitest"; +import { internal } from "../../../convex/_generated/api"; +import schema from "../../../convex/schema"; + +const modules = { + "../../../convex/_generated/api.ts": () => + import("../../../convex/_generated/api"), + "../../../convex/users/internalMutation.ts": () => + import("../../../convex/users/internalMutation"), +}; + +const profile = (id: string, login = "octocat") => ({ + id, + login, + email: `${login}@example.com`, + emailVerified: true, + avatarUrl: `https://avatars.example/${login}`, +}); + +describe("GitHub sign-in user callbacks", () => { + it("creates one user per GitHub account and reuses a user from before 2.0", async () => { + const t = convexTest(schema, modules); + const existing = await t.run((ctx) => + ctx.db.insert("users", { name: "old", githubAccountId: "654" }), + ); + + const returning = await t.mutation( + internal.users.internalMutation.createUserFromGithub, + { method: { name: "github", profile: profile("654") } }, + ); + const newcomer = await t.mutation( + internal.users.internalMutation.createUserFromGithub, + { method: { name: "github", profile: profile("777", "newcomer") } }, + ); + + expect(returning).toBe(existing); + const users = await t.run((ctx) => ctx.db.query("users").collect()); + expect(users).toHaveLength(2); + expect(users.find((user) => user._id === newcomer)).toMatchObject({ + name: "newcomer", + githubAccountId: "777", + email: "newcomer@example.com", + image: "https://avatars.example/newcomer", + }); + }); + + it("refreshes the profile and refuses a different GitHub account", async () => { + const t = convexTest(schema, modules); + const userId = await t.run((ctx) => + ctx.db.insert("users", { name: "old", githubAccountId: "654" }), + ); + + await t.mutation(internal.users.internalMutation.updateUserFromGithub, { + method: { name: "github", profile: profile("654", "renamed") }, + userId, + }); + await expect( + t.mutation(internal.users.internalMutation.updateUserFromGithub, { + method: { name: "github", profile: profile("999", "attacker") }, + userId, + }), + ).rejects.toThrow("identity changed"); + + expect(await t.run((ctx) => ctx.db.get(userId))).toMatchObject({ + name: "renamed", + githubAccountId: "654", + }); + }); +});