Skip to content

Commit 5bc7e82

Browse files
committed
Add pagination support in xata pull (#1477)
1 parent 7f7f14e commit 5bc7e82

File tree

6 files changed

+31
-33
lines changed

6 files changed

+31
-33
lines changed

cli/src/commands/pull/index.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,22 @@ export default class Pull extends BaseCommand<typeof Pull> {
5151

5252
const details = await getBranchDetailsWithPgRoll(xata, { workspace, region, database, branch });
5353

54-
let logs: Schemas.MigrationHistoryItem[] | Schemas.Commit[] = [];
54+
let logs: (Schemas.MigrationHistoryItem | Schemas.Commit)[] = [];
55+
let cursor = undefined;
5556
if (isBranchPgRollEnabled(details)) {
56-
const { migrations } = await xata.api.migrations.getMigrationHistory({
57-
pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }
58-
});
59-
logs = migrations;
57+
do {
58+
const { migrations, cursor: newCursor } = await xata.api.migrations.getMigrationHistory({
59+
pathParams: { workspace, region, dbBranchName: `${database}:${branch}` },
60+
queryParams: { cursor, limit: 200 }
61+
});
62+
63+
logs = logs.concat(migrations);
64+
cursor = newCursor;
65+
} while (cursor !== undefined);
6066
} else {
6167
const data = await xata.api.migrations.getBranchSchemaHistory({
6268
pathParams: { workspace, region, dbBranchName: `${database}:${branch}` },
63-
body: {
64-
// TODO: Fix pagination in the API to start from last known migration and not from the beginning
65-
// Also paginate until we get all migrations
66-
page: { size: 200 }
67-
}
69+
body: { page: { size: 200 } }
6870
});
6971
logs = data.logs;
7072
}

cli/src/commands/pull/pull.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ const pgrollFetchSingle = (url: string, request: any) => {
230230
schema: { tables: [{ name: 'table1', columns: [{ name: 'a', type: 'string' }] }] }
231231
})
232232
};
233-
} else if (url === `${baseUrl}/migrations/history` && request.method === 'GET') {
233+
} else if (url === `${baseUrl}/migrations/history?limit=200` && request.method === 'GET') {
234234
return {
235235
ok: true,
236236
json: async () => ({
@@ -251,7 +251,7 @@ const pgrollFetchMultiple = (url: string, request: any) => {
251251
schema: { tables: [{ name: 'table1', columns: [{ name: 'a', type: 'string' }] }] }
252252
})
253253
};
254-
} else if (url === `${baseUrl}/migrations/history` && request.method === 'GET') {
254+
} else if (url === `${baseUrl}/migrations/history?limit=200` && request.method === 'GET') {
255255
return {
256256
ok: true,
257257
json: async () => ({

cli/src/commands/push/index.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,22 @@ export default class Push extends BaseCommand<typeof Push> {
4848

4949
const details = await getBranchDetailsWithPgRoll(xata, { workspace, region, database, branch });
5050

51-
let logs: Schemas.MigrationHistoryItem[] | Schemas.Commit[] = [];
51+
let logs: (Schemas.MigrationHistoryItem | Schemas.Commit)[] = [];
52+
let cursor = undefined;
5253
if (isBranchPgRollEnabled(details)) {
53-
const { migrations } = await xata.api.migrations.getMigrationHistory({
54-
pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }
55-
});
56-
logs = migrations;
54+
do {
55+
const { migrations, cursor: newCursor } = await xata.api.migrations.getMigrationHistory({
56+
pathParams: { workspace, region, dbBranchName: `${database}:${branch}` },
57+
queryParams: { cursor, limit: 200 }
58+
});
59+
60+
logs = logs.concat(migrations);
61+
cursor = newCursor;
62+
} while (cursor !== undefined);
5763
} else {
5864
const data = await xata.api.migrations.getBranchSchemaHistory({
5965
pathParams: { workspace, region, dbBranchName: `${database}:${branch}` },
60-
body: {
61-
// TODO: Fix pagination in the API to start from last known migration and not from the beginning
62-
// Also paginate until we get all migrations
63-
page: { size: 200 }
64-
}
66+
body: { page: { size: 200 } }
6567
});
6668
logs = data.logs;
6769
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ const pgrollFetchSingle = (url: string, request: any, type: 'inferred' | 'pgroll
244244
schema: { tables: [{ name: 'table1', columns: [{ name: 'a', type: 'string' }] }] }
245245
})
246246
};
247-
} else if (url === `${baseUrl}/migrations/history` && request.method === 'GET') {
247+
} else if (url === `${baseUrl}/migrations/history?limit=200` && request.method === 'GET') {
248248
return {
249249
ok: true,
250250
json: async () => (type === 'inferred' ? { migrations: [pgrollMigration3] } : { migrations: [pgrollMigration1] })
@@ -263,7 +263,7 @@ const pgrollFetchEmpty = (url: string, request: any) => {
263263
schema: { tables: [{ name: 'table1', columns: [{ name: 'a', type: 'string' }] }] }
264264
})
265265
};
266-
} else if (url === `${baseUrl}/migrations/history` && request.method === 'GET') {
266+
} else if (url === `${baseUrl}/migrations/history?limit=200` && request.method === 'GET') {
267267
return {
268268
ok: true,
269269
json: async () => ({

cli/src/migrations/files.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export async function removeLocalMigrations() {
9797
}
9898
}
9999

100-
export function commitToMigrationFile(logs: Schemas.Commit[] | Schemas.MigrationHistoryItem[]): LocalMigrationFile[] {
100+
export function commitToMigrationFile(logs: (Schemas.Commit | Schemas.MigrationHistoryItem)[]): LocalMigrationFile[] {
101101
// Schema history comes in reverse order, so we need to reverse it
102102
return logs.reverse().map((log) =>
103103
isMigrationPgRollFormat(log)

pnpm-lock.yaml

Lines changed: 2 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)