From 94877debc183a7b676c4cb52480119e2de092afd Mon Sep 17 00:00:00 2001 From: bkellam Date: Thu, 30 Oct 2025 21:10:52 -0700 Subject: [PATCH] migrate to using enums for connectionType and external_codeHostType --- .../migration.sql | 14 +++++++++ .../migration.sql | 22 +++++++++++++ packages/db/prisma/schema.prisma | 31 +++++++++++++++++-- 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 packages/db/prisma/migrations/20251031012851_change_connection_type_to_enum/migration.sql create mode 100644 packages/db/prisma/migrations/20251031014307_change_repo_code_host_type_to_enum/migration.sql diff --git a/packages/db/prisma/migrations/20251031012851_change_connection_type_to_enum/migration.sql b/packages/db/prisma/migrations/20251031012851_change_connection_type_to_enum/migration.sql new file mode 100644 index 00000000..6580b522 --- /dev/null +++ b/packages/db/prisma/migrations/20251031012851_change_connection_type_to_enum/migration.sql @@ -0,0 +1,14 @@ +/* + Migrates the `connectionType` column from text to a enum. The values in this field are known to + be one of the following: github, gitlab, gitea, gerrit, bitbucket, azuredevops, git. + + This is occording to what we would expect to be in a valid config file for the schema version at commit 4899c9fbc755851af2ddcce99f4a4200f2faa4f6. + See: https://github.com/sourcebot-dev/sourcebot/blob/4899c9fbc755851af2ddcce99f4a4200f2faa4f6/packages/schemas/src/v3/connection.type.ts#L3 +*/ +-- CreateEnum +CREATE TYPE "ConnectionType" AS ENUM ('github', 'gitlab', 'gitea', 'gerrit', 'bitbucket', 'azuredevops', 'git'); + +-- AlterTable - Convert existing column to enum type without dropping data +ALTER TABLE "Connection" + ALTER COLUMN "connectionType" TYPE "ConnectionType" + USING "connectionType"::text::"ConnectionType"; diff --git a/packages/db/prisma/migrations/20251031014307_change_repo_code_host_type_to_enum/migration.sql b/packages/db/prisma/migrations/20251031014307_change_repo_code_host_type_to_enum/migration.sql new file mode 100644 index 00000000..1d3fb23b --- /dev/null +++ b/packages/db/prisma/migrations/20251031014307_change_repo_code_host_type_to_enum/migration.sql @@ -0,0 +1,22 @@ +/* + Migrates the `external_codeHostType` column from text to a enum. The values in this field are known to + be one of the following: github, gitlab, gitea, gerrit, bitbucket-server, bitbucket-cloud, generic-git-host, azuredevops. + + This is occording to what we would expect to be in the database written as of commit 4899c9fbc755851af2ddcce99f4a4200f2faa4f6. + See: + - https://github.com/sourcebot-dev/sourcebot/blob/4899c9fbc755851af2ddcce99f4a4200f2faa4f6/packages/backend/src/repoCompileUtils.ts#L57 + - https://github.com/sourcebot-dev/sourcebot/blob/4899c9fbc755851af2ddcce99f4a4200f2faa4f6/packages/backend/src/repoCompileUtils.ts#L135 + - https://github.com/sourcebot-dev/sourcebot/blob/4899c9fbc755851af2ddcce99f4a4200f2faa4f6/packages/backend/src/repoCompileUtils.ts#L208 + - https://github.com/sourcebot-dev/sourcebot/blob/4899c9fbc755851af2ddcce99f4a4200f2faa4f6/packages/backend/src/repoCompileUtils.ts#L291 + - https://github.com/sourcebot-dev/sourcebot/blob/4899c9fbc755851af2ddcce99f4a4200f2faa4f6/packages/backend/src/repoCompileUtils.ts#L407 + - https://github.com/sourcebot-dev/sourcebot/blob/4899c9fbc755851af2ddcce99f4a4200f2faa4f6/packages/backend/src/repoCompileUtils.ts#L510 + - https://github.com/sourcebot-dev/sourcebot/blob/4899c9fbc755851af2ddcce99f4a4200f2faa4f6/packages/backend/src/repoCompileUtils.ts#L574 + - https://github.com/sourcebot-dev/sourcebot/blob/4899c9fbc755851af2ddcce99f4a4200f2faa4f6/packages/backend/src/repoCompileUtils.ts#L642 +*/ +-- CreateEnum +CREATE TYPE "CodeHostType" AS ENUM ('github', 'gitlab', 'gitea', 'gerrit', 'bitbucket-server', 'bitbucket-cloud', 'generic-git-host', 'azuredevops'); + +-- AlterTable - Convert existing column to enum type without dropping data +ALTER TABLE "Repo" + ALTER COLUMN "external_codeHostType" TYPE "CodeHostType" + USING "external_codeHostType"::text::"CodeHostType"; diff --git a/packages/db/prisma/schema.prisma b/packages/db/prisma/schema.prisma index 93adb717..313de621 100644 --- a/packages/db/prisma/schema.prisma +++ b/packages/db/prisma/schema.prisma @@ -29,6 +29,21 @@ enum ChatVisibility { PUBLIC } +/// @note: The @map annotation is required to maintain backwards compatibility +/// with the existing database. +/// @note: In the generated client, these mapped values will be in pascalCase. +/// This behaviour will change in prisma v7. See: https://github.com/prisma/prisma/issues/8446#issuecomment-3356119713 +enum CodeHostType { + github + gitlab + gitea + gerrit + bitbucketServer @map("bitbucket-server") + bitbucketCloud @map("bitbucket-cloud") + genericGitHost @map("generic-git-host") + azuredevops +} + model Repo { id Int @id @default(autoincrement()) name String /// Full repo name, including the vcs hostname (ex. github.com/sourcebot-dev/sourcebot) @@ -53,7 +68,7 @@ model Repo { indexedCommitHash String? /// The commit hash of the last indexed commit (on HEAD). external_id String /// The id of the repo in the external service - external_codeHostType String /// The type of the external service (e.g., github, gitlab, etc.) + external_codeHostType CodeHostType /// The type of the external service (e.g., github, gitlab, etc.) external_codeHostUrl String /// The base url of the external service (e.g., https://github.com) org Org @relation(fields: [orgId], references: [id], onDelete: Cascade) @@ -125,6 +140,18 @@ model SearchContext { @@unique([name, orgId]) } +/// Matches the union of `type` fields in the schema. +/// @see: schemas/v3/connection.type.ts +enum ConnectionType { + github + gitlab + gitea + gerrit + bitbucket + azuredevops + git +} + model Connection { id Int @id @default(autoincrement()) name String @@ -135,7 +162,7 @@ model Connection { repos RepoToConnection[] // The type of connection (e.g., github, gitlab, etc.) - connectionType String + connectionType ConnectionType syncJobs ConnectionSyncJob[] /// When the connection was last synced successfully.