Repository navigation
fix: harden deploy pre-rollout DB drift guardrails #197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 2 additions & 30 deletions
32
packages/backend/src/startup/verifyRequiredDatabaseState.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,5 @@ | ||
| import { getPrismaClient } from '@lucky/shared/utils' | ||
|
|
||
| const MISSING_SCHEMA_ERROR_CODE = 'ERR_DB_SCHEMA_MISSING' | ||
|
|
||
| type DatabaseStartupError = Error & { code?: string } | ||
| import { verifyRequiredDatabaseRelations } from '@lucky/shared/utils' | ||
|
|
||
| export async function verifyRequiredDatabaseState(): Promise<void> { | ||
| const prisma = getPrismaClient() | ||
|
|
||
| try { | ||
| await prisma.guildRoleGrant.count({ | ||
| take: 1, | ||
| }) | ||
| } catch (error) { | ||
| const maybePrismaError = error as { | ||
| code?: string | ||
| message?: string | ||
| meta?: { table?: string } | ||
| } | ||
|
|
||
| if (maybePrismaError.code === 'P2021') { | ||
| const table = maybePrismaError.meta?.table ?? 'guild_role_grants' | ||
| const startupError = new Error( | ||
| `Required database relation "${table}" is missing. Run migrations before starting backend.`, | ||
| { cause: error }, | ||
| ) as DatabaseStartupError | ||
| startupError.code = MISSING_SCHEMA_ERROR_CODE | ||
| throw startupError | ||
| } | ||
|
|
||
| throw error | ||
| } | ||
| await verifyRequiredDatabaseRelations() | ||
| } |
131 changes: 131 additions & 0 deletions
131
packages/backend/tests/unit/startup/requiredDatabaseRelations.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import { describe, expect, test, jest } from '@jest/globals' | ||
| import { | ||
| REQUIRED_DATABASE_RELATIONS, | ||
| verifyRequiredDatabaseRelations, | ||
| } from '@lucky/shared/utils/requiredDatabaseRelations' | ||
|
|
||
| type CountMock = jest.MockedFunction<(args: { take: 1 }) => Promise<number>> | ||
|
|
||
| function createPrismaStub() { | ||
| const guildRoleGrantCount = jest.fn(async () => 1) as CountMock | ||
| const guildAutomationManifestCount = jest.fn(async () => 1) as CountMock | ||
| const guildAutomationRunCount = jest.fn(async () => 1) as CountMock | ||
| const guildAutomationDriftCount = jest.fn(async () => 1) as CountMock | ||
|
|
||
| const prisma = { | ||
| guildRoleGrant: { count: guildRoleGrantCount }, | ||
| guildAutomationManifest: { count: guildAutomationManifestCount }, | ||
| guildAutomationRun: { count: guildAutomationRunCount }, | ||
| guildAutomationDrift: { count: guildAutomationDriftCount }, | ||
| } | ||
|
|
||
| return { | ||
| prisma, | ||
| guildRoleGrantCount, | ||
| guildAutomationManifestCount, | ||
| guildAutomationRunCount, | ||
| guildAutomationDriftCount, | ||
| } | ||
| } | ||
|
|
||
| function missingRelationMessage(table: string): string { | ||
| return ( | ||
| `Required database relation "${table}" is missing. ` + | ||
| 'Run `npx prisma migrate deploy` before starting backend.' | ||
| ) | ||
| } | ||
|
|
||
| describe('verifyRequiredDatabaseRelations', () => { | ||
| test('verifies all sentinel relations when schema is healthy', async () => { | ||
| const { | ||
| prisma, | ||
| guildRoleGrantCount, | ||
| guildAutomationManifestCount, | ||
| guildAutomationRunCount, | ||
| guildAutomationDriftCount, | ||
| } = createPrismaStub() | ||
|
|
||
| await expect( | ||
| verifyRequiredDatabaseRelations(prisma), | ||
| ).resolves.toBeUndefined() | ||
|
|
||
| expect(REQUIRED_DATABASE_RELATIONS).toEqual([ | ||
| 'guild_role_grants', | ||
| 'guild_automation_manifests', | ||
| 'guild_automation_runs', | ||
| 'guild_automation_drifts', | ||
| ]) | ||
| expect(guildRoleGrantCount).toHaveBeenCalledWith({ take: 1 }) | ||
| expect(guildAutomationManifestCount).toHaveBeenCalledWith({ take: 1 }) | ||
| expect(guildAutomationRunCount).toHaveBeenCalledWith({ take: 1 }) | ||
| expect(guildAutomationDriftCount).toHaveBeenCalledWith({ take: 1 }) | ||
| }) | ||
|
|
||
| test('maps missing guild_role_grants relation to migration guidance', async () => { | ||
| const { prisma, guildRoleGrantCount } = createPrismaStub() | ||
| guildRoleGrantCount.mockRejectedValueOnce({ | ||
| code: 'P2021', | ||
| meta: { table: 'guild_role_grants' }, | ||
| }) | ||
|
|
||
| await expect(verifyRequiredDatabaseRelations(prisma)).rejects.toMatchObject( | ||
| { | ||
| code: 'ERR_DB_SCHEMA_MISSING', | ||
| message: missingRelationMessage('guild_role_grants'), | ||
| }, | ||
| ) | ||
| }) | ||
|
|
||
| test('maps missing guild_automation_manifests relation to migration guidance', async () => { | ||
| const { prisma, guildAutomationManifestCount } = createPrismaStub() | ||
| guildAutomationManifestCount.mockRejectedValueOnce({ | ||
| code: 'P2021', | ||
| meta: { table: 'guild_automation_manifests' }, | ||
| }) | ||
|
|
||
| await expect(verifyRequiredDatabaseRelations(prisma)).rejects.toMatchObject( | ||
| { | ||
| code: 'ERR_DB_SCHEMA_MISSING', | ||
| message: missingRelationMessage('guild_automation_manifests'), | ||
| }, | ||
| ) | ||
| }) | ||
|
|
||
| test('maps missing guild_automation_runs relation to migration guidance', async () => { | ||
| const { prisma, guildAutomationRunCount } = createPrismaStub() | ||
| guildAutomationRunCount.mockRejectedValueOnce({ | ||
| code: 'P2021', | ||
| meta: { table: 'guild_automation_runs' }, | ||
| }) | ||
|
|
||
| await expect(verifyRequiredDatabaseRelations(prisma)).rejects.toMatchObject( | ||
| { | ||
| code: 'ERR_DB_SCHEMA_MISSING', | ||
| message: missingRelationMessage('guild_automation_runs'), | ||
| }, | ||
| ) | ||
| }) | ||
|
|
||
| test('maps missing guild_automation_drifts relation to migration guidance', async () => { | ||
| const { prisma, guildAutomationDriftCount } = createPrismaStub() | ||
| guildAutomationDriftCount.mockRejectedValueOnce({ | ||
| code: 'P2021', | ||
| meta: { table: 'guild_automation_drifts' }, | ||
| }) | ||
|
|
||
| await expect(verifyRequiredDatabaseRelations(prisma)).rejects.toMatchObject( | ||
| { | ||
| code: 'ERR_DB_SCHEMA_MISSING', | ||
| message: missingRelationMessage('guild_automation_drifts'), | ||
| }, | ||
| ) | ||
| }) | ||
|
|
||
| test('rethrows non-P2021 errors unchanged', async () => { | ||
| const { prisma, guildRoleGrantCount } = createPrismaStub() | ||
| const error = new Error('database offline') | ||
| guildRoleGrantCount.mockRejectedValueOnce(error) | ||
|
|
||
| await expect(verifyRequiredDatabaseRelations(prisma)).rejects.toBe(error) | ||
| }) | ||
| }) |
61 changes: 22 additions & 39 deletions
61
packages/backend/tests/unit/startup/verifyRequiredDatabaseState.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import type { PrismaClient } from '../generated/prisma/client.js' | ||
|
|
||
| import { getPrismaClient } from './database/prismaClient' | ||
|
|
||
| export const REQUIRED_DATABASE_STATE_ERROR_CODE = 'ERR_DB_SCHEMA_MISSING' | ||
| export const REQUIRED_DATABASE_RELATIONS = [ | ||
| 'guild_role_grants', | ||
| 'guild_automation_manifests', | ||
| 'guild_automation_runs', | ||
| 'guild_automation_drifts', | ||
| ] as const | ||
|
|
||
| type RequiredDatabaseRelationsPrismaModels = Pick< | ||
| PrismaClient, | ||
| | 'guildRoleGrant' | ||
| | 'guildAutomationManifest' | ||
| | 'guildAutomationRun' | ||
| | 'guildAutomationDrift' | ||
| > | ||
|
|
||
| export type RequiredDatabaseStatePrisma = { | ||
| guildRoleGrant: Pick< | ||
| RequiredDatabaseRelationsPrismaModels['guildRoleGrant'], | ||
| 'count' | ||
| > | ||
| guildAutomationManifest: Pick< | ||
| RequiredDatabaseRelationsPrismaModels['guildAutomationManifest'], | ||
| 'count' | ||
| > | ||
| guildAutomationRun: Pick< | ||
| RequiredDatabaseRelationsPrismaModels['guildAutomationRun'], | ||
| 'count' | ||
| > | ||
| guildAutomationDrift: Pick< | ||
| RequiredDatabaseRelationsPrismaModels['guildAutomationDrift'], | ||
| 'count' | ||
| > | ||
| } | ||
|
|
||
| type PrismaRelationError = { | ||
| code?: string | ||
| meta?: { table?: string } | ||
| } | ||
|
|
||
| type DatabaseStateError = Error & { | ||
| code?: string | ||
| } | ||
|
|
||
| type RelationCheck = { | ||
| table: (typeof REQUIRED_DATABASE_RELATIONS)[number] | ||
| check: (prisma: RequiredDatabaseStatePrisma) => Promise<unknown> | ||
| } | ||
|
|
||
| const relationChecks: RelationCheck[] = [ | ||
| { | ||
| table: 'guild_role_grants', | ||
| check: (prisma) => prisma.guildRoleGrant.count({ take: 1 }), | ||
| }, | ||
| { | ||
| table: 'guild_automation_manifests', | ||
| check: (prisma) => prisma.guildAutomationManifest.count({ take: 1 }), | ||
| }, | ||
| { | ||
| table: 'guild_automation_runs', | ||
| check: (prisma) => prisma.guildAutomationRun.count({ take: 1 }), | ||
| }, | ||
| { | ||
| table: 'guild_automation_drifts', | ||
| check: (prisma) => prisma.guildAutomationDrift.count({ take: 1 }), | ||
| }, | ||
| ] | ||
|
|
||
| export async function verifyRequiredDatabaseRelations( | ||
| prisma: RequiredDatabaseStatePrisma = getPrismaClient(), | ||
| ): Promise<void> { | ||
| for (const relationCheck of relationChecks) { | ||
| try { | ||
| await relationCheck.check(prisma) | ||
| } catch (error) { | ||
| const prismaError = error as PrismaRelationError | ||
| if (prismaError.code !== 'P2021') { | ||
| throw error | ||
| } | ||
|
|
||
| const table = prismaError.meta?.table ?? relationCheck.table | ||
| const startupError = new Error( | ||
| `Required database relation "${table}" is missing. ` + | ||
| 'Run `npx prisma migrate deploy` before starting backend.', | ||
| { cause: error }, | ||
| ) as DatabaseStateError | ||
| startupError.code = REQUIRED_DATABASE_STATE_ERROR_CODE | ||
| throw startupError | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.