Skip to content

chore: script to backfill slugs#3909

Closed
MichaelUnkey wants to merge 2 commits intomainfrom
backfill-workspace-slug
Closed

chore: script to backfill slugs#3909
MichaelUnkey wants to merge 2 commits intomainfrom
backfill-workspace-slug

Conversation

@MichaelUnkey
Copy link
Collaborator

@MichaelUnkey MichaelUnkey commented Sep 2, 2025

What does this PR do?

Fixes # (issue)

If there is not an issue for this, please create one first. This is used to tracking purposes and also helps use understand why this PR exists

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Chore (refactoring code, technical debt, workflow improvements)
  • Enhancement (small improvements)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How should this be tested?

  • Test in branch 🤷
  • Should fill in any null slugs with lowercase with - for spaced and no other characters based on workspace name.

Checklist

Required

  • Filled out the "How to test" section in this PR
  • Read Contributing Guide
  • Self-reviewed my own code
  • Commented on my code in hard-to-understand areas
  • Ran pnpm build
  • Ran pnpm fmt
  • Checked for warnings, there are none
  • Removed all console.logs
  • Merged the latest changes from main onto my branch with git pull origin main
  • My changes don't cause any responsiveness issues

Appreciated

  • If a UI change was made: Added a screen recording or screenshots to this PR
  • Updated the Unkey Docs if changes were necessary

Summary by CodeRabbit

  • Chores
    • Added a migration to backfill missing workspace slugs, creating consistent, URL-friendly identifiers for existing workspaces.
    • Runs in batched processing with automatic conflict resolution to ensure uniqueness.
    • Improves operational visibility with per-batch progress, warnings for conflicts, and final summary statistics; no user-facing UI changes.

@changeset-bot
Copy link

changeset-bot bot commented Sep 2, 2025

⚠️ No Changeset found

Latest commit: 6da9eff

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel
Copy link

vercel bot commented Sep 2, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

2 Skipped Deployments
Project Deployment Preview Comments Updated (UTC)
dashboard Ignored Ignored Preview Sep 2, 2025 2:35pm
engineering Ignored Ignored Preview Sep 2, 2025 2:35pm

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 2, 2025

📝 Walkthrough

Walkthrough

Adds a new TypeScript migration script that backfills null workspace slugs by normalizing names, ensuring uniqueness with numeric suffixes, processing records in paginated batches, updating via drizzle, and logging progress and final counts.

Changes

Cohort / File(s) Summary
Migration script
tools/migrate/backfill-workspace-slugs.ts
New script to backfill null workspaces.slug values: generates normalized slugs (lowercase, NFKD, trim, spaces→hyphens, remove non-alnum except hyphens, collapse/trim hyphens), ensures uniqueness with incremental -1, -2 suffixes, processes batches (limit 1000 ordered by id), updates rows via drizzle/mysqlDrizzle, logs per-batch and per-workspace progress and conflicts, handles errors, closes DB connection, and prints final summary.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant CLI as CLI
  participant Script as backfill-workspace-slugs.ts
  participant ORM as drizzle (mysqlDrizzle)
  participant DB as MySQL (workspaces)

  CLI->>Script: start
  Script->>ORM: init connection (env: DATABASE_*)
  loop until no more rows
    Script->>DB: SELECT * FROM workspaces WHERE slug IS NULL ORDER BY id LIMIT 1000
    DB-->>Script: batch
    alt batch not empty
      loop for each workspace
        Script->>Script: compute normalized slug(name)
        Script->>DB: SELECT 1 FROM workspaces WHERE slug = candidate
        alt exists
          Script->>Script: append numeric suffix (-1, -2, ...)
        end
        Script->>DB: UPDATE workspaces SET slug = finalSlug WHERE id = ...
        DB-->>Script: update result
        Script-->>CLI: log per-workspace result/warning
      end
    else no rows
      Script-->>Script: break loop
    end
  end
  Script->>ORM: close connection
  Script-->>CLI: print summary (processed/updated/errors)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • mcstepp
  • ogzhanolguncu
  • perkinsjr
  • Flo4604
  • chronark
  • imeyer
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch backfill-workspace-slug

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@MichaelUnkey MichaelUnkey marked this pull request as ready for review September 2, 2025 13:28
@github-actions
Copy link
Contributor

github-actions bot commented Sep 2, 2025

Thank you for following the naming conventions for pull request titles! 🙏

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 7

📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 2e3b72d and 6bb3b5d.

📒 Files selected for processing (1)
  • tools/migrate/backfill-workspace-slugs.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
**/*.{js,jsx,ts,tsx}

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.{js,jsx,ts,tsx}: Use Biome for formatting and linting in TypeScript/JavaScript projects
Prefer named exports over default exports in TypeScript/JavaScript, except for Next.js pages

Files:

  • tools/migrate/backfill-workspace-slugs.ts
**/*.{ts,tsx}

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.{ts,tsx}: Follow strict TypeScript configuration
Use Zod for runtime validation in TypeScript projects

Files:

  • tools/migrate/backfill-workspace-slugs.ts
**/*.{env,js,ts,go}

📄 CodeRabbit inference engine (CLAUDE.md)

All environment variables must follow the format: UNKEY_<SERVICE_NAME>_VARNAME

Files:

  • tools/migrate/backfill-workspace-slugs.ts
🧬 Code graph analysis (1)
tools/migrate/backfill-workspace-slugs.ts (1)
internal/db/src/schema/workspaces.ts (1)
  • workspaces (16-104)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Build / Build
  • GitHub Check: Test Go API Local / Test
  • GitHub Check: Test API / API Test Local
🔇 Additional comments (2)
tools/migrate/backfill-workspace-slugs.ts (2)

42-49: Pagination on string PKs: OK, but confirm id collation is stable

Using id > cursor with varchar PKs is fine if collation is consistent and IDs are unique (they are). Just confirm IDs are not case-variant in practice; otherwise ordering could be surprising across collations.


35-41: Progress logging is clear and batch-sized; LGTM

Nice running totals and batch cursor logging—useful for long runs and resumability.

Also applies to: 112-123

Comment on lines +17 to +25
function generateSlug(name: string): string {
return name
.toLowerCase()
.trim()
.replace(/\s+/g, "-") // Replace spaces with hyphens
.replace(/[^a-z0-9-]/g, "") // Remove all special characters except hyphens
.replace(/-+/g, "-") // Replace multiple consecutive hyphens with single hyphen
.replace(/^-+|-+$/g, ""); // Remove leading and trailing hyphens
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Slug normalization: enforce 64-char limit, handle Unicode/diacritics, and collapse empty results predictably

Current regex drops all non [a-z0-9-], which turns many non‑ASCII names into empty slugs and can exceed DB limit (varchar(64)). Add diacritic stripping, predictable fallback, and length enforcement with room for numeric suffixes.

Apply:

-function generateSlug(name: string): string {
-  return name
-    .toLowerCase()
-    .trim()
-    .replace(/\s+/g, "-") // Replace spaces with hyphens
-    .replace(/[^a-z0-9-]/g, "") // Remove all special characters except hyphens
-    .replace(/-+/g, "-") // Replace multiple consecutive hyphens with single hyphen
-    .replace(/^-+|-+$/g, ""); // Remove leading and trailing hyphens
-}
+function generateSlug(name: string): string {
+  // 1) normalize Unicode, strip diacritics; 2) canonicalize; 3) trim edges
+  const base = name
+    .toLowerCase()
+    .normalize("NFKD")
+    .replace(/[\u0300-\u036f]/g, "") // remove combining marks
+    .trim()
+    .replace(/\s+/g, "-")
+    .replace(/[^a-z0-9-]/g, "")
+    .replace(/-+/g, "-")
+    .replace(/^-+|-+$/g, "");
+
+  // reserve space for potential "-<n>" suffix later; keep at most 61 chars here
+  const MAX_BASE = 61;
+  const trimmed = base.slice(0, MAX_BASE);
+  return trimmed || "workspace";
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function generateSlug(name: string): string {
return name
.toLowerCase()
.trim()
.replace(/\s+/g, "-") // Replace spaces with hyphens
.replace(/[^a-z0-9-]/g, "") // Remove all special characters except hyphens
.replace(/-+/g, "-") // Replace multiple consecutive hyphens with single hyphen
.replace(/^-+|-+$/g, ""); // Remove leading and trailing hyphens
}
function generateSlug(name: string): string {
// 1) normalize Unicode, strip diacritics; 2) canonicalize; 3) trim edges
const base = name
.toLowerCase()
.normalize("NFKD")
.replace(/[\u0300-\u036f]/g, "") // remove combining marks
.trim()
.replace(/\s+/g, "-") // Replace spaces with hyphens
.replace(/[^a-z0-9-]/g, "") // Remove all special characters except hyphens
.replace(/-+/g, "-") // Collapse multiple hyphens
.replace(/^-+|-+$/g, ""); // Trim leading/trailing hyphens
// reserve space for a "-<n>" suffix later; limit to 61 chars for the base
const MAX_BASE = 61;
const trimmed = base.slice(0, MAX_BASE);
// if slug is empty after stripping, fall back to a predictable constant
return trimmed || "workspace";
}

Comment on lines +28 to +34
const conn = await mysql.createConnection(
`mysql://${process.env.DATABASE_USERNAME}:${process.env.DATABASE_PASSWORD}@${process.env.DATABASE_HOST}:3306/unkey?ssl={}`,
);

await conn.ping();
const db = mysqlDrizzle(conn, { schema, mode: "default" });

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Env vars naming + validation; connection config should not embed credentials in a URL

  • Violates guideline: envs must be UNKEY__VARNAME.
  • No runtime validation; undefined yields malformed URL.
  • Passing ?ssl={} is brittle; prefer typed options.

Apply:

+import { z } from "zod";
@@
-  const conn = await mysql.createConnection(
-    `mysql://${process.env.DATABASE_USERNAME}:${process.env.DATABASE_PASSWORD}@${process.env.DATABASE_HOST}:3306/unkey?ssl={}`,
-  );
+  const Env = z
+    .object({
+      UNKEY_DB_HOST: z.string().min(1),
+      UNKEY_DB_USERNAME: z.string().min(1),
+      UNKEY_DB_PASSWORD: z.string().min(1),
+      UNKEY_DB_NAME: z.string().min(1).default("unkey"),
+    })
+    .parse(process.env);
+
+  const conn = await mysql.createConnection({
+    host: Env.UNKEY_DB_HOST,
+    user: Env.UNKEY_DB_USERNAME,
+    password: Env.UNKEY_DB_PASSWORD,
+    database: Env.UNKEY_DB_NAME,
+    // TODO: configure SSL per deployment; placeholder enables TLS negotiation without CA
+    ssl: {},
+  });

Verification to run:


🏁 Script executed:

#!/bin/bash
# Ensure no legacy env names remain
rg -nP 'process\.env\.(DATABASE_(USERNAME|PASSWORD|HOST)|DATABASE_URL)'

Length of output: 1386


Refactor all migration scripts to use validated UNKEY_DB_ env vars and object-based MySQL connections*

  • Rename legacy process.env.DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_HOST, (and any DATABASE_URL) in tools/migrate/*.ts to UNKEY_DB_USERNAME, UNKEY_DB_PASSWORD, UNKEY_DB_HOST, UNKEY_DB_NAME and validate them (e.g., via zod).
  • Replace string-based URL connections
    mysql.createConnection(
      `mysql://${user}:${pass}@${host}:3306/${db}?ssl={}`,
    )
    with object config:
    mysql.createConnection({
      host: Env.UNKEY_DB_HOST,
      user: Env.UNKEY_DB_USERNAME,
      password: Env.UNKEY_DB_PASSWORD,
      database: Env.UNKEY_DB_NAME,
      ssl: {}, // configure per deployment
    });
🤖 Prompt for AI Agents
In tools/migrate/backfill-workspace-slugs.ts around lines 28 to 34, the script
still uses legacy process.env.DATABASE_* vars and a string-based MySQL URL;
update it to read and validate UNKEY_DB_USERNAME, UNKEY_DB_PASSWORD,
UNKEY_DB_HOST, and UNKEY_DB_NAME (use a small zod schema or equivalent
validation and throw with a clear message on failure), then call
mysql.createConnection with an object config (host, user, password, database,
ssl) using the validated Env values instead of composing a connection string;
ensure ssl is passed as an empty object or appropriate config per deployment.

Comment on lines +63 to +74
// Process each workspace
for (const workspace of workspaces) {
try {
const slug = generateSlug(workspace.name);

if (!slug) {
console.warn(
`Workspace ${workspace.id} (${workspace.name}) generated empty slug, skipping`,
);
continue;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Empty slugs: consider deterministic fallback instead of skipping

Currently logs and skips; that leaves holes. Prefer workspace-<shortId> or trunc(id) to guarantee progress.

Example change within this block after if (!slug) { ... }:

-        if (!slug) {
-          console.warn(
-            `Workspace ${workspace.id} (${workspace.name}) generated empty slug, skipping`,
-          );
-          continue;
-        }
+        if (!slug) {
+          // deterministic fallback from id
+          const fallback = `workspace-${workspace.id.slice(0, 8).toLowerCase().replace(/[^a-z0-9]/g, "")}`;
+          console.warn(`Empty slug from name; falling back to '${fallback}' for ${workspace.id}`);
+          // proceed with fallback
+          (slug as string) = fallback;
+        }

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In tools/migrate/backfill-workspace-slugs.ts around lines 63 to 74, the script
currently logs and skips workspaces when generateSlug returns an empty value,
leaving gaps; instead generate a deterministic fallback slug (e.g.,
"workspace-<shortId>" using a stable prefix plus a truncated or hashed form of
workspace.id) and use that value so the migration always assigns a slug; ensure
the fallback is collision-resistant enough for your system (truncate id to 8-12
chars or hash+encode), and replace the continue with assigning the fallback slug
and proceeding with the existing save/update logic.

Comment on lines +115 to +128
await conn.end();

console.info("Migration completed!");
console.info({
totalProcessed: processed,
totalUpdated: updated,
totalErrors: errors,
});
}

main().catch((error) => {
console.error("Migration failed:", error);
process.exit(1);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Always close the DB connection with finally

If an error throws before Line 115, the connection is never closed.

Apply:

-async function main() {
+async function main() {
-  const conn = await mysql.createConnection( /* ... */ );
-  await conn.ping();
-  const db = mysqlDrizzle(conn, { schema, mode: "default" });
-  // work...
-  await conn.end();
+  const conn = await mysql.createConnection( /* ... */ );
+  try {
+    await conn.ping();
+    const db = mysqlDrizzle(conn, { schema, mode: "default" });
+    // work...
+  } finally {
+    await conn.end().catch(() => {});
+  }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
await conn.end();
console.info("Migration completed!");
console.info({
totalProcessed: processed,
totalUpdated: updated,
totalErrors: errors,
});
}
main().catch((error) => {
console.error("Migration failed:", error);
process.exit(1);
});
async function main() {
const conn = await mysql.createConnection( /* ... */ );
try {
await conn.ping();
const db = mysqlDrizzle(conn, { schema, mode: "default" });
// work...
} finally {
// Always attempt to close the connection, but don't let errors here mask the original one
await conn.end().catch(() => {});
}
console.info("Migration completed!");
console.info({
totalProcessed: processed,
totalUpdated: updated,
totalErrors: errors,
});
}
main().catch((error) => {
console.error("Migration failed:", error);
process.exit(1);
});
🤖 Prompt for AI Agents
In tools/migrate/backfill-workspace-slugs.ts around lines 115-128, the DB
connection is only closed after normal completion so if an error is thrown
earlier the connection is leaked; wrap the main logic that opens and uses conn
in a try/catch/finally (or add a finally to main()) and in the finally block
check that conn is defined and call await conn.end() (guarding against multiple
closes), then rethrow or handle errors as appropriate so the existing
main().catch(...) still logs and exits.

Copy link
Collaborator

@chronark chronark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

♻️ Duplicate comments (5)
tools/migrate/backfill-workspace-slugs.ts (5)

35-43: Use UNKEY_DB_ envs + validate with Zod; avoid URL credentials*

Complies with repo guidelines; prevents malformed URLs and leaked secrets in connection strings.

-async function main() {
-  const conn = await mysql.createConnection(
-    `mysql://${process.env.DATABASE_USERNAME}:${process.env.DATABASE_PASSWORD}@${process.env.DATABASE_HOST}:3306/unkey?ssl={}`,
-  );
-
-  await conn.ping();
-  const db = mysqlDrizzle(conn, { schema, mode: "default" });
+async function main() {
+  const Env = z
+    .object({
+      UNKEY_DB_HOST: z.string().min(1),
+      UNKEY_DB_USERNAME: z.string().min(1),
+      UNKEY_DB_PASSWORD: z.string().min(1),
+      UNKEY_DB_NAME: z.string().min(1).default("unkey"),
+    })
+    .parse(process.env);
+
+  const conn = await mysql.createConnection({
+    host: Env.UNKEY_DB_HOST,
+    user: Env.UNKEY_DB_USERNAME,
+    password: Env.UNKEY_DB_PASSWORD,
+    database: Env.UNKEY_DB_NAME,
+    ssl: {},
+  });
+
+  await conn.ping();
+  const db = mysqlDrizzle(conn, { schema, mode: "default" });

Also add:

+import { z } from "zod";

at the top of the file (after Line 2).


43-44: Always close the DB connection with finally

Prevents connection leaks on errors mid-run.

-  console.log("Starting workspace slug backfill migration...");
+  console.log("Starting workspace slug backfill migration...");
+  try {
@@
-  await conn.end();
-
-  console.info("Migration completed!");
-  console.info({
-    totalProcessed: processed,
-    totalUpdated: updated,
-    totalErrors: errors,
-  });
-}
+  console.info("Migration completed!");
+  console.info({
+    totalProcessed: processed,
+    totalUpdated: updated,
+    totalErrors: errors,
+  });
+  } finally {
+    await conn.end().catch(() => {});
+  }
+}

Also applies to: 154-162


17-33: Slug normalization: explicitly strip diacritics

Normalization alone leaves combining marks until the later filter; strip them explicitly for clarity and fewer surprises on future regex changes.

   const base = name
     .toLowerCase()
     .normalize("NFKD")
+    .replace(/[\u0300-\u036f]/g, "") // strip combining marks
     .trim()
     .replace(/\s+/g, "-") // Replace spaces with hyphens
     .replace(/[^a-z0-9-]/g, "") // Remove all special characters except hyphens
     .replace(/-+/g, "-") // Collapse multiple hyphens
     .replace(/^-+|-+$/g, ""); // Trim leading/trailing hyphens

74-81: Don’t skip empty slugs; use deterministic fallback from id

Ensures forward progress for non‑ASCII names and avoids gaps.

-        const slug = generateSlug(workspace.name);
+        let slug = generateSlug(workspace.name);
@@
-        if (!slug) {
-          console.warn(
-            `Workspace ${workspace.id} (${workspace.name}) generated empty slug, skipping`,
-          );
-          continue;
-        }
+        if (!slug) {
+          const fallback = `workspace-${workspace.id
+            .slice(0, 8)
+            .toLowerCase()
+            .replace(/[^a-z0-9]/g, "")}`;
+          console.warn(
+            `Empty slug from name; falling back to '${fallback}' for ${workspace.id}`,
+          );
+          slug = fallback;
+        }

125-136: Avoid extra SELECT by checking affectedRows from the UPDATE

Saves a round-trip; still safe with the IS NULL guard.

-        await db
+        const result = await db
           .update(schema.workspaces)
           .set({ slug: finalSlug })
           .where(and(eq(schema.workspaces.id, workspace.id), isNull(schema.workspaces.slug)));
-
-        // Check if the update actually affected a row by verifying the database state
-        const updatedWorkspace = await db.query.workspaces.findFirst({
-          where: (table, { eq }) => eq(table.id, workspace.id),
-        });
-        const rowUpdated = updatedWorkspace?.slug === finalSlug;
+        // drizzle-mysql2 returns a ResultSetHeader/OkPacket with affectedRows
+        // @ts-expect-error drizzle mysql2 driver returns affectedRows
+        const rowUpdated = (result?.affectedRows ?? 0) > 0;
📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 6bb3b5d and 6da9eff.

📒 Files selected for processing (1)
  • tools/migrate/backfill-workspace-slugs.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
**/*.{js,jsx,ts,tsx}

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.{js,jsx,ts,tsx}: Use Biome for formatting and linting in TypeScript/JavaScript projects
Prefer named exports over default exports in TypeScript/JavaScript, except for Next.js pages

Files:

  • tools/migrate/backfill-workspace-slugs.ts
**/*.{ts,tsx}

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.{ts,tsx}: Follow strict TypeScript configuration
Use Zod for runtime validation in TypeScript projects

Files:

  • tools/migrate/backfill-workspace-slugs.ts
**/*.{env,js,ts,go}

📄 CodeRabbit inference engine (CLAUDE.md)

All environment variables must follow the format: UNKEY_<SERVICE_NAME>_VARNAME

Files:

  • tools/migrate/backfill-workspace-slugs.ts
🧠 Learnings (1)
📚 Learning: 2025-07-28T19:42:37.047Z
Learnt from: mcstepp
PR: unkeyed/unkey#3662
File: apps/dashboard/app/(app)/projects/page.tsx:74-81
Timestamp: 2025-07-28T19:42:37.047Z
Learning: In apps/dashboard/app/(app)/projects/page.tsx, the user mcstepp prefers to keep placeholder functions like generateSlug inline during POC/demonstration phases rather than extracting them to utility modules, with plans to refactor later when the feature matures beyond the proof-of-concept stage.

Applied to files:

  • tools/migrate/backfill-workspace-slugs.ts
🧬 Code graph analysis (1)
tools/migrate/backfill-workspace-slugs.ts (1)
internal/db/src/schema/workspaces.ts (1)
  • workspaces (16-104)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Test Go API Local / Test
  • GitHub Check: Build / Build
  • GitHub Check: Test API / API Test Local
🔇 Additional comments (2)
tools/migrate/backfill-workspace-slugs.ts (2)

96-117: Nice: uniqueness loop handles numeric suffixes and 64-char cap

Good job preventing “-1-1” and enforcing the varchar(64) limit.


50-56: Verify lexical ordering of workspace IDs
Ensure that workspaces.id is a lexicographically sortable string (e.g. ULID/KSUID) before using gt(table.id, cursor) for pagination; if it’s a numeric or UUID that isn’t lex-ordered, switch to a numeric or timestamp cursor instead.

Comment on lines +1 to +3
import { and, eq, isNull, mysqlDrizzle, schema } from "@unkey/db";
import mysql from "mysql2/promise";

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Imports: add Zod

Required for the env validation above.

 import { and, eq, isNull, mysqlDrizzle, schema } from "@unkey/db";
 import mysql from "mysql2/promise";
+import { z } from "zod";
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import { and, eq, isNull, mysqlDrizzle, schema } from "@unkey/db";
import mysql from "mysql2/promise";
import { and, eq, isNull, mysqlDrizzle, schema } from "@unkey/db";
import mysql from "mysql2/promise";
import { z } from "zod";
🤖 Prompt for AI Agents
In tools/migrate/backfill-workspace-slugs.ts around lines 1 to 3, the file uses
Zod for env validation but does not import it; add the Zod import (for example
import { z } from "zod") to the existing imports at the top of the file so the
env validation code can reference Zod.

@chronark chronark deleted the backfill-workspace-slug branch November 5, 2025 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants