-
Notifications
You must be signed in to change notification settings - Fork 516
test(cli): cover migration up and repair (CLI-2269) #6376
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
7ttp
merged 5 commits into
develop
from
7ttp/cli-2269-migration-command-family-coverage
Aug 31, 2026
+229
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
baa1107
test: add migration up and repair live coverage
7ttp 61980b9
test: scope migration up cleanup to owned state
7ttp 9499bdb
Nits
7ttp a8a869c
few more nits
7ttp 5d7d42b
Merge remote-tracking branch 'upstream/develop' into 7ttp/cli-2269-mi…
7ttp 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
91 changes: 91 additions & 0 deletions
91
apps/cli/src/legacy/commands/migration/repair/repair.live.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,91 @@ | ||
| import { mkdir, unlink, writeFile } from "node:fs/promises"; | ||
| import { join } from "node:path"; | ||
| import { expect } from "vitest"; | ||
|
|
||
| import { | ||
| liveMigrationVersion, | ||
| queryLiveDb, | ||
| requireLiveSuccess, | ||
| test, | ||
| throwWithCleanup, | ||
| } from "../../../../../tests/helpers/live.ts"; | ||
|
|
||
| test("amends the migration history status on the remote database", async ({ | ||
| cli, | ||
| project, | ||
| workspace, | ||
| }) => { | ||
| const version = liveMigrationVersion(); | ||
| const migrations = join(workspace.path, "supabase", "migrations"); | ||
| await mkdir(migrations, { recursive: true }); | ||
| const migrationFile = join(migrations, `${version}_e2e_repair.sql`); | ||
| // `repair --status applied` records the file's statements in migration | ||
| // history without executing them, so this table is never actually created. | ||
| await writeFile(migrationFile, `create table if not exists e2e_repair_${version} (id int);\n`); | ||
|
|
||
| let targetError: unknown; | ||
| let versionReverted = false; | ||
| const cleanupErrors: Array<unknown> = []; | ||
| try { | ||
| const applied = await cli([ | ||
| "migration", | ||
| "repair", | ||
| version, | ||
| "--status", | ||
| "applied", | ||
| "--db-url", | ||
| project.dbUrl, | ||
| ]); | ||
| expect(applied.exitCode, applied.stderr).toBe(0); | ||
| expect(applied.stderr, applied.stdout).toContain("=> applied"); | ||
| await unlink(migrationFile); | ||
|
|
||
| const recorded = await queryLiveDb( | ||
| project.dbUrl, | ||
| "select version from supabase_migrations.schema_migrations where version = $1", | ||
| [version], | ||
| ); | ||
| expect(recorded).toHaveLength(1); | ||
|
|
||
| const reverted = await cli([ | ||
| "migration", | ||
| "repair", | ||
| version, | ||
| "--status", | ||
| "reverted", | ||
| "--db-url", | ||
| project.dbUrl, | ||
| ]); | ||
| expect(reverted.exitCode, reverted.stderr).toBe(0); | ||
| expect(reverted.stderr, reverted.stdout).toContain("=> reverted"); | ||
|
|
||
| const remaining = await queryLiveDb( | ||
| project.dbUrl, | ||
| "select version from supabase_migrations.schema_migrations where version = $1", | ||
| [version], | ||
| ); | ||
| expect(remaining).toHaveLength(0); | ||
| // Only skip the teardown revert once the row is verifiably gone. | ||
| versionReverted = true; | ||
| } catch (error) { | ||
| targetError = error; | ||
| } finally { | ||
| if (!versionReverted) { | ||
| try { | ||
| const cleanup = await cli([ | ||
| "migration", | ||
| "repair", | ||
| version, | ||
| "--status", | ||
| "reverted", | ||
| "--db-url", | ||
| project.dbUrl, | ||
| ]); | ||
| requireLiveSuccess(cleanup, "migration repair cleanup"); | ||
| } catch (error) { | ||
| cleanupErrors.push(error); | ||
| } | ||
| } | ||
| } | ||
| throwWithCleanup(targetError, cleanupErrors); | ||
| }); | ||
102 changes: 102 additions & 0 deletions
102
apps/cli/src/legacy/commands/migration/up/up.live.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,102 @@ | ||
| import { mkdir, rm, writeFile } from "node:fs/promises"; | ||
| import { join } from "node:path"; | ||
| import { expect } from "vitest"; | ||
|
|
||
| import { | ||
| liveMigrationVersion, | ||
| queryLiveDb, | ||
| requireLiveSuccess, | ||
| test, | ||
| throwWithCleanup, | ||
| } from "../../../../../tests/helpers/live.ts"; | ||
|
|
||
| test("applies a test-written migration to the remote database", async ({ | ||
| cli, | ||
| project, | ||
| workspace, | ||
| }) => { | ||
| const version = liveMigrationVersion(); | ||
| const migrations = join(workspace.path, "supabase", "migrations"); | ||
| await mkdir(migrations, { recursive: true }); | ||
|
|
||
| // The serial suite shares one remote project, so seed a local stub for every | ||
| // version already in remote history — otherwise `migration up` rejects them | ||
| // as missing locally. The history table may not exist yet on a fresh project. | ||
| let remoteVersions: Array<{ version: string }> = []; | ||
| try { | ||
| remoteVersions = await queryLiveDb( | ||
| project.dbUrl, | ||
| "select version from supabase_migrations.schema_migrations order by version", | ||
| ); | ||
| } catch (error) { | ||
| // 42P01 (undefined relation) covers the fresh-project case where the | ||
| // history table or its schema does not exist yet; anything else is a real | ||
| // failure the test must surface. | ||
| if ((error as { code?: string }).code !== "42P01") throw error; | ||
| remoteVersions = []; | ||
| } | ||
| for (const row of remoteVersions) { | ||
| await writeFile( | ||
| join(migrations, `${row.version}_preexisting_remote.sql`), | ||
| "-- stub for a version already in remote history\n", | ||
| ); | ||
| } | ||
|
|
||
| const migrationFile = join(migrations, `${version}_e2e_up.sql`); | ||
| await writeFile(migrationFile, `create table if not exists e2e_up_${version} (id int);\n`); | ||
|
|
||
| let targetError: unknown; | ||
| const cleanupErrors: Array<unknown> = []; | ||
| try { | ||
| const applied = await cli(["migration", "up", "--db-url", project.dbUrl]); | ||
| expect(applied.exitCode, applied.stderr).toBe(0); | ||
| expect(applied.stderr, applied.stdout).toContain("Applying migration"); | ||
|
7ttp marked this conversation as resolved.
|
||
|
|
||
| const history = await queryLiveDb( | ||
| project.dbUrl, | ||
| "select version from supabase_migrations.schema_migrations where version = $1", | ||
| [version], | ||
| ); | ||
| expect(history).toHaveLength(1); | ||
|
|
||
| const created = await queryLiveDb(project.dbUrl, "select to_regclass($1) as table_oid", [ | ||
| `public.e2e_up_${version}`, | ||
| ]); | ||
| expect(created[0]?.["table_oid"], "migration up must execute the migration sql").not.toBeNull(); | ||
| } catch (error) { | ||
| targetError = error; | ||
| } finally { | ||
| try { | ||
| await rm(migrationFile, { force: true }); | ||
| } catch (error) { | ||
| cleanupErrors.push(error); | ||
| } | ||
| try { | ||
| const dropped = await cli([ | ||
| "db", | ||
| "query", | ||
| `drop table if exists e2e_up_${version}`, | ||
| "--db-url", | ||
| project.dbUrl, | ||
| ]); | ||
| requireLiveSuccess(dropped, "db query cleanup after migration up"); | ||
| } catch (error) { | ||
| cleanupErrors.push(error); | ||
| } | ||
| try { | ||
| const reverted = await cli([ | ||
| "migration", | ||
| "repair", | ||
| version, | ||
| "--status", | ||
| "reverted", | ||
| "--db-url", | ||
| project.dbUrl, | ||
| ]); | ||
| requireLiveSuccess(reverted, "migration repair cleanup after migration up"); | ||
| } catch (error) { | ||
| cleanupErrors.push(error); | ||
| } | ||
| } | ||
| throwWithCleanup(targetError, cleanupErrors); | ||
| }); | ||
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.