Skip to content

Commit a79963d

Browse files
authored
fix: improve query performance (#7099)
1 parent fbdc7d2 commit a79963d

File tree

5 files changed

+29
-30
lines changed

5 files changed

+29
-30
lines changed

integration-tests/testkit/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,12 @@ export function generateUnique() {
102102
capitalize: false,
103103
});
104104
}
105+
106+
export function assertNonNull<T>(
107+
value: T | null,
108+
message = 'Expected non-null value.',
109+
): asserts value is T {
110+
if (value === null) {
111+
throw new Error(message);
112+
}
113+
}

integration-tests/tests/api/schema/delete.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { parse, print } from 'graphql';
33
import { updateSchemaComposition } from 'testkit/flow';
44
import { ProjectType } from 'testkit/gql/graphql';
55
import { initSeed } from 'testkit/seed';
6-
import { getServiceHost } from 'testkit/utils';
6+
import { assertNonNull, getServiceHost } from 'testkit/utils';
77
import { GetObjectCommand, S3Client } from '@aws-sdk/client-s3';
88
import { createStorage } from '@hive/storage';
99
import { sortSDL } from '@theguild/federation-composition';
@@ -171,11 +171,12 @@ test.concurrent(
171171
.then(r => r.expectNoGraphQLErrors());
172172
expect(deleteServiceResult.schemaDelete.__typename).toBe('SchemaDeleteSuccess');
173173

174-
const latestVersion = await storage.getLatestVersion({
174+
const latestVersion = await storage.getMaybeLatestVersion({
175175
targetId: target.id,
176176
projectId: project.id,
177177
organizationId: organization.id,
178178
});
179+
assertNonNull(latestVersion);
179180

180181
expect(latestVersion.compositeSchemaSDL).toMatchInlineSnapshot(`
181182
type Query {
@@ -302,11 +303,12 @@ test.concurrent(
302303
.then(r => r.expectNoGraphQLErrors());
303304
expect(deleteServiceResult.schemaDelete.__typename).toBe('SchemaDeleteSuccess');
304305

305-
const latestVersion = await storage.getLatestVersion({
306+
const latestVersion = await storage.getMaybeLatestVersion({
306307
targetId: target.id,
307308
projectId: project.id,
308309
organizationId: organization.id,
309310
});
311+
assertNonNull(latestVersion);
310312

311313
expect(latestVersion.compositeSchemaSDL).toEqual(null);
312314
expect(latestVersion.schemaCompositionErrors).toMatchInlineSnapshot(`

integration-tests/tests/api/schema/publish.spec.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { graphql } from 'testkit/gql';
44
/* eslint-disable no-process-env */
55
import { ProjectType } from 'testkit/gql/graphql';
66
import { execute } from 'testkit/graphql';
7-
import { getServiceHost } from 'testkit/utils';
7+
import { assertNonNull, getServiceHost } from 'testkit/utils';
88
// eslint-disable-next-line import/no-extraneous-dependencies
99
import { createStorage } from '@hive/storage';
1010
import { createTarget, publishSchema, updateSchemaComposition } from '../../../testkit/flow';
@@ -606,11 +606,12 @@ describe('schema publishing changes are persisted', () => {
606606
return;
607607
}
608608

609-
const latestVersion = await storage.getLatestVersion({
609+
const latestVersion = await storage.getMaybeLatestVersion({
610610
targetId: target.id,
611611
projectId: project.id,
612612
organizationId: organization.id,
613613
});
614+
assertNonNull(latestVersion);
614615

615616
const changes = await storage.getSchemaChangesForVersion({
616617
versionId: latestVersion.id,
@@ -2710,11 +2711,12 @@ test('Target.schemaVersion: result is read from the database', async () => {
27102711
return;
27112712
}
27122713

2713-
const latestVersion = await storage.getLatestVersion({
2714+
const latestVersion = await storage.getMaybeLatestVersion({
27142715
targetId: target.id,
27152716
projectId: project.id,
27162717
organizationId: organization.id,
27172718
});
2719+
assertNonNull(latestVersion);
27182720

27192721
const result = await execute({
27202722
document: SchemaCompareToPreviousVersionQuery,
@@ -2842,11 +2844,12 @@ test('Composition Error (Federation 2) can be served from the database', async (
28422844
return;
28432845
}
28442846

2845-
const latestVersion = await storage.getLatestVersion({
2847+
const latestVersion = await storage.getMaybeLatestVersion({
28462848
targetId: target.id,
28472849
projectId: project.id,
28482850
organizationId: organization.id,
28492851
});
2852+
assertNonNull(latestVersion);
28502853

28512854
const result = await execute({
28522855
document: SchemaCompareToPreviousVersionQuery,
@@ -2994,11 +2997,12 @@ test('Composition Network Failure (Federation 2)', async () => {
29942997
return;
29952998
}
29962999

2997-
const latestVersion = await storage.getLatestVersion({
3000+
const latestVersion = await storage.getMaybeLatestVersion({
29983001
targetId: target.id,
29993002
projectId: project.id,
30003003
organizationId: organization.id,
30013004
});
3005+
assertNonNull(latestVersion);
30023006

30033007
const result = await execute({
30043008
document: SchemaCompareToPreviousVersionQuery,

packages/services/api/src/modules/shared/providers/storage.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,6 @@ export interface Storage {
361361

362362
getMaybeLatestValidVersion(_: { targetId: string }): Promise<SchemaVersion | null | never>;
363363

364-
getLatestVersion(_: TargetSelector): Promise<SchemaVersion | never>;
365-
366364
getMaybeLatestVersion(_: TargetSelector): Promise<SchemaVersion | null>;
367365

368366
/** Find the version before a schema version */

packages/services/storage/src/index.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,31 +2023,17 @@ export async function createStorage(
20232023

20242024
return SchemaVersionModel.parse(version);
20252025
},
2026-
async getLatestVersion(args) {
2027-
const version = await pool.maybeOne<unknown>(
2028-
sql`/* getLatestVersion */
2029-
SELECT
2030-
${schemaVersionSQLFields(sql`sv.`)}
2031-
FROM schema_versions as sv
2032-
LEFT JOIN targets as t ON (t.id = sv.target_id)
2033-
WHERE sv.target_id = ${args.targetId} AND t.project_id = ${args.projectId}
2034-
ORDER BY sv.created_at DESC
2035-
LIMIT 1
2036-
`,
2037-
);
2038-
2039-
return SchemaVersionModel.parse(version);
2040-
},
2041-
20422026
async getMaybeLatestVersion(args) {
20432027
const version = await pool.maybeOne<unknown>(
20442028
sql`/* getMaybeLatestVersion */
20452029
SELECT
20462030
${schemaVersionSQLFields(sql`sv.`)}
2047-
FROM schema_versions as sv
2048-
LEFT JOIN targets as t ON (t.id = sv.target_id)
2049-
WHERE sv.target_id = ${args.targetId} AND t.project_id = ${args.projectId}
2050-
ORDER BY sv.created_at DESC
2031+
FROM
2032+
"schema_versions" AS "sv"
2033+
WHERE
2034+
"sv"."target_id" = ${args.targetId}
2035+
ORDER BY
2036+
"sv"."created_at" DESC
20512037
LIMIT 1
20522038
`,
20532039
);

0 commit comments

Comments
 (0)