-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(knowledge): retry the Tin projection after a database refuses the extension #8039
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
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
66 changes: 66 additions & 0 deletions
66
packages/db/script-migrations/0019_tin_keyword_projection.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,66 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import type { Sql } from 'postgres' | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { | ||
| adoptTinKeywordProjection, | ||
| installTinKeywordProjection, | ||
| } from './0019_tin_keyword_projection' | ||
| import { ScriptMigrationDeferred } from './types' | ||
|
|
||
| /** A session that offers `tin` or not, and answers `CREATE EXTENSION` with `createError`. */ | ||
| function createSqlHarness(options: { available: boolean; createError?: { code: string } }) { | ||
| const statements: string[] = [] | ||
| const run = (strings: TemplateStringsArray) => { | ||
| const text = strings.join('?').replace(/\s+/g, ' ').trim() | ||
| statements.push(text) | ||
| if (text.includes('pg_available_extensions')) { | ||
| return Promise.resolve(options.available ? [{ '?column?': 1 }] : []) | ||
| } | ||
| return Promise.resolve([]) | ||
| } | ||
| const sql = run as unknown as Sql | ||
| sql.unsafe = vi.fn(async (text: string) => { | ||
| statements.push(text) | ||
| if (text.startsWith('CREATE EXTENSION') && options.createError) throw options.createError | ||
| return [] | ||
| }) as unknown as Sql['unsafe'] | ||
| return { sql, statements } | ||
| } | ||
|
|
||
| describe('installTinKeywordProjection', () => { | ||
| it('records a no-op where the database does not offer tin', async () => { | ||
| const { sql, statements } = createSqlHarness({ available: false }) | ||
| expect(await installTinKeywordProjection(sql)).toBeUndefined() | ||
| expect(statements.some((text) => text.startsWith('CREATE EXTENSION'))).toBe(false) | ||
| }) | ||
|
|
||
| it.each(['42501', '0A000'])( | ||
| 'defers without installing anything when the database refuses the extension (%s)', | ||
| async (code) => { | ||
| const { sql, statements } = createSqlHarness({ available: true, createError: { code } }) | ||
| await expect(installTinKeywordProjection(sql)).rejects.toBeInstanceOf(ScriptMigrationDeferred) | ||
| expect(statements.at(-1)).toBe('CREATE EXTENSION IF NOT EXISTS tin') | ||
| } | ||
| ) | ||
|
|
||
| it('is a no-op when run directly against a database that refuses the extension', async () => { | ||
| const { sql, statements } = createSqlHarness({ | ||
| available: true, | ||
| createError: { code: '42501' }, | ||
| }) | ||
| await expect(adoptTinKeywordProjection(sql)).resolves.toBeUndefined() | ||
| expect(statements.at(-1)).toBe('CREATE EXTENSION IF NOT EXISTS tin') | ||
| }) | ||
|
|
||
| it('fails a direct run on any other extension error', async () => { | ||
| const { sql } = createSqlHarness({ available: true, createError: { code: '53100' } }) | ||
| await expect(adoptTinKeywordProjection(sql)).rejects.toEqual({ code: '53100' }) | ||
| }) | ||
|
|
||
| it('fails the migration on any other extension error', async () => { | ||
| const { sql } = createSqlHarness({ available: true, createError: { code: '53100' } }) | ||
| await expect(installTinKeywordProjection(sql)).rejects.toEqual({ code: '53100' }) | ||
| }) | ||
| }) |
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,56 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import type { Sql } from 'postgres' | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { runScriptMigrations, scriptMigrations } from './index' | ||
| import { type ScriptMigration, ScriptMigrationDeferred } from './types' | ||
|
|
||
| const TIN = '0019_tin_keyword_projection' | ||
|
|
||
| /** Every registered migration but Tin's, so Tin is the only pending one. */ | ||
| const APPLIED_BEFORE_TIN = scriptMigrations | ||
| .filter(({ name }) => name !== TIN) | ||
| .map(({ name }) => name) | ||
|
|
||
| /** A session where `applied` is already recorded and the database refuses `tin`. */ | ||
| function createSqlHarness(applied: readonly string[]) { | ||
| const recorded: string[] = [] | ||
| const run = (strings: TemplateStringsArray, ...values: unknown[]) => { | ||
| const text = strings.join('?').replace(/\s+/g, ' ').trim() | ||
| if (text.startsWith('SELECT name FROM script_migrations')) { | ||
| return Promise.resolve(applied.map((name) => ({ name }))) | ||
| } | ||
| if (text.includes('pg_available_extensions')) return Promise.resolve([{ '?column?': 1 }]) | ||
| if (text.startsWith('INSERT INTO script_migrations')) recorded.push(values[0] as string) | ||
| return Promise.resolve([]) | ||
| } | ||
| const sql = run as unknown as Sql | ||
| sql.unsafe = vi.fn(async (text: string) => { | ||
| if (text.startsWith('CREATE EXTENSION')) throw { code: '42501' } | ||
| return [] | ||
| }) as unknown as Sql['unsafe'] | ||
| sql.begin = vi.fn(async (callback) => (callback as (tx: Sql) => unknown)(sql)) as Sql['begin'] | ||
| return { sql, recorded } | ||
| } | ||
|
|
||
| describe('runScriptMigrations', () => { | ||
| it('leaves a deferred migration unrecorded without failing the upgrade', async () => { | ||
| const { sql, recorded } = createSqlHarness(APPLIED_BEFORE_TIN) | ||
| await expect(runScriptMigrations(sql)).resolves.toBeUndefined() | ||
| expect(recorded).toEqual([]) | ||
| }) | ||
|
|
||
| it('applies and records the migrations that follow a deferred one', async () => { | ||
| const deferring: ScriptMigration = { | ||
| name: 'test_deferring', | ||
| up: async () => { | ||
| throw new ScriptMigrationDeferred('the database refused the test migration') | ||
| }, | ||
| } | ||
| const following: ScriptMigration = { name: 'test_following', up: async () => {} } | ||
| const { sql, recorded } = createSqlHarness([]) | ||
| await expect(runScriptMigrations(sql, [deferring, following])).resolves.toBeUndefined() | ||
| expect(recorded).toEqual(['test_following']) | ||
| }) | ||
| }) |
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
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.