Skip to content

Commit 8c2b9be

Browse files
andrew-farriesXata
authored andcommitted
Wait for completion on pgroll migration push (#1434)
1 parent 6e223b3 commit 8c2b9be

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

.changeset/light-cycles-repair.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
"@xata.io/client": major
2+
'@xata.io/client': major
33
---
44

55
Make XataApiClient to use ES Proxies

cli/src/commands/push/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Args, Flags } from '@oclif/core';
22
import { Schemas } from '@xata.io/client';
3+
import { PgRollMigrationDefinition } from '@xata.io/pgroll';
34
import { BaseCommand } from '../../base.js';
45
import {
56
LocalMigrationFile,
@@ -11,10 +12,10 @@ import {
1112
allMigrationsPgRollFormat,
1213
getBranchDetailsWithPgRoll,
1314
isBranchPgRollEnabled,
14-
isMigrationPgRollFormat
15+
isMigrationPgRollFormat,
16+
waitForMigrationToFinish
1517
} from '../../migrations/pgroll.js';
1618
import { MigrationFilePgroll } from '../../migrations/schema.js';
17-
import { PgRollMigrationDefinition } from '@xata.io/pgroll';
1819

1920
export default class Push extends BaseCommand<typeof Push> {
2021
static description = 'Push local changes to a remote Xata branch';
@@ -103,10 +104,12 @@ export default class Push extends BaseCommand<typeof Push> {
103104
.flatMap((migration) => PgRollMigrationDefinition.parse(migration));
104105
for (const migration of migrationsToPush) {
105106
try {
106-
await xata.api.migrations.applyMigration({
107+
const { jobID } = await xata.api.migrations.applyMigration({
107108
pathParams: { workspace, region, dbBranchName: `${database}:${branch}` },
108109
body: migration
109110
});
111+
112+
await waitForMigrationToFinish(xata.api, workspace, region, database, branch, jobID);
110113
} catch (e) {
111114
this.log(`Failed to push ${migration} with ${e}. Stopping.`);
112115
this.exit(1);

cli/src/commands/push/push.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ const baseFetch = (url: string, request: any) => {
188188
}
189189
})
190190
};
191+
} else if (
192+
url === `https://test-1234.us-east-1.xata.sh/db/db1:main/migrations/jobs/1234` &&
193+
request.method === 'GET'
194+
) {
195+
return {
196+
ok: true,
197+
json: async () => ({ status: 'completed' })
198+
};
191199
}
192200

193201
throw new Error(`Unexpected fetch request: ${url} ${request.method}`);

cli/src/migrations/pgroll.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { Schemas } from '@xata.io/client';
2-
import { migrationsDir, readMigrationsDir } from './files.js';
1+
import { Schemas, XataApiClient } from '@xata.io/client';
32
import path from 'path';
4-
import { safeJSONParse, safeReadFile } from '../utils/files.js';
5-
import { migrationFilePgroll, MigrationFilePgroll } from './schema.js';
63
import { XataClient } from '../base.js';
4+
import { safeJSONParse, safeReadFile } from '../utils/files.js';
5+
import { migrationsDir, readMigrationsDir } from './files.js';
6+
import { MigrationFilePgroll, migrationFilePgroll } from './schema.js';
77

88
export const isBranchPgRollEnabled = (details: Schemas.DBBranch) => {
99
// @ts-expect-error TODO: Fix this when api is finalized
@@ -121,3 +121,26 @@ export async function getBranchDetailsWithPgRoll(
121121

122122
return details;
123123
}
124+
125+
export async function waitForMigrationToFinish(
126+
api: XataApiClient,
127+
workspace: string,
128+
region: string,
129+
database: string,
130+
branch: string,
131+
jobId: string
132+
) {
133+
const { status, error } = await api.migrations.getMigrationJobStatus({
134+
pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, jobId }
135+
});
136+
if (status === 'failed') {
137+
throw new Error(`Migration failed, ${error}`);
138+
}
139+
140+
if (status === 'completed') {
141+
return;
142+
}
143+
144+
await new Promise((resolve) => setTimeout(resolve, 1000));
145+
return await waitForMigrationToFinish(api, workspace, region, database, branch, jobId);
146+
}

test/integration/sql.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ describe('SQL proxy', () => {
373373
expect(record2).toBeDefined();
374374
expect(record2?.[4]).toBe('[C] Planes');
375375
});
376-
376+
377377
test('xata.sql has a connection string', async () => {
378378
expect(xata.sql.connectionString).toBeDefined();
379379
expect(xata.sql.connectionString).toMatch(

0 commit comments

Comments
 (0)