Skip to content

Commit

Permalink
Check existing versions before deploy.
Browse files Browse the repository at this point in the history
  • Loading branch information
jhochmuth committed Aug 28, 2023
1 parent ff094a6 commit 12989fa
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 6 deletions.
43 changes: 37 additions & 6 deletions packages/core/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import rocksetConfigure from '@rockset/client';
import rocksetConfigure, { MainApi } from '@rockset/client';
import {
DeployHooks,
LambdaEntity,
Expand Down Expand Up @@ -217,9 +217,11 @@ export async function deleteQueryLambdas(options: LambdaDeleteOptions) {
// TODO add tests for this
export async function deployQueryLambdas(
hooks: DeployHooks = {},
options: LambdaDeployOptions
options: LambdaDeployOptions,
mockClient?: MainApi
) {
const [srcPath, client] = await Promise.all([getSrcPath(), createClient()]);
const client = mockClient ?? (await createClient());
const srcPath = await getSrcPath();

// Grab all files
const allFiles = await getFiles(srcPath);
Expand Down Expand Up @@ -250,7 +252,8 @@ export async function deployQueryLambdas(

return Promise.all(
lambdaEntities.map(async (lambdaEntity) => {
const { ws, name: lambda, sql: text, fullName } = lambdaEntity;
const { ws, name: lambda, sql: text, fullName, config } = lambdaEntity;
const { description, default_parameters: defaultParameters } = config;

if (
(!options.workspace && !options.lambda) ||
Expand All @@ -264,6 +267,34 @@ export async function deployQueryLambdas(
if (options.dryRun) {
return;
}

// If the user is changing tags, the QL must be deployed so that the version is known
if (!options.tag) {
try {
const savedQueryLambdas = await client.queryLambdas.listQueryLambdaVersions(
ws,
lambda
);

// If an equivalent version already exists, we can skip deploy
const hasEquivalentVersion = savedQueryLambdas?.data?.some(
(lambda) =>
description === lambda.description &&
text === lambda.sql?.query &&
_.isEqual(defaultParameters, lambda.sql?.default_parameters)
);

if (hasEquivalentVersion) {
hooks.onSkipQueryLambda?.(lambdaEntity.fullName);
return;
}
} catch (e) {
console.warn(
`Failed to list query lambda versions for comparison, assume QL ${lambdaEntity.fullName} must be deployed: ${e}`
);
}
}

try {
if (options.createMissingWorkspace && !remoteWorkspacesMap?.[ws]) {
await client.workspaces.createWorkspace({
Expand All @@ -275,10 +306,10 @@ export async function deployQueryLambdas(
ws,
lambda,
{
description: lambdaEntity.config.description,
description: description,
sql: {
query: text,
default_parameters: lambdaEntity.config.default_parameters,
default_parameters: defaultParameters,
},
},
/* create if not present */ true
Expand Down
66 changes: 66 additions & 0 deletions packages/core/src/tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,70 @@ describe('Main external facing functions', () => {
]);
})
);

test(
'Test deploy',
mfs('emptyRoot', async () => {
// Write a lambda to commons.foo, and expect to see it listed
const willSkip = createEmptyQLEntity(
parseLambdaQualifiedName('commons.willSkip'),
'blah'
);
willSkip.sql = 'SELECT 1';
const willDeploy = createEmptyQLEntity(
parseLambdaQualifiedName('commons.willDeploy'),
'blah'
);
willDeploy.sql = 'SELECT 2';

await Promise.all([writeLambda(willSkip), writeLambda(willDeploy)]);

const startHook = jest.fn();
const skipHook = jest.fn();
const successHook = jest.fn();

const mockClient = {
workspaces: { listWorkspaces: jest.fn(() => ['commons']) },
queryLambdas: {
listQueryLambdaVersions: jest.fn(() => ({
data: [
{
sql: { query: 'SELECT 1', default_parameters: [] },
description: 'blah',
},
],
})),
updateQueryLambda: jest.fn((_, lambda) => {
if (lambda === 'willDeploy') {
return { version: 'willDeployVersion' };
}
return { version: 'willSkipVersion' };
}),
},
};

await main.deployQueryLambdas(
{
onDeployStart: startHook,
onSkipQueryLambda: skipHook,
onDeployVersionSuccess: successHook,
},
{},
// @ts-ignore
mockClient
);

expect(startHook).toHaveBeenCalledTimes(2);

expect(skipHook).toHaveBeenCalledTimes(1);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
expect(skipHook.mock.calls?.[0]?.[0]).toBe('commons.willSkip');

expect(successHook).toHaveBeenCalledTimes(1);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
expect(successHook.mock.calls?.[0]?.[0]?.version).toBe(
'willDeployVersion'
);
})
);
});

0 comments on commit 12989fa

Please sign in to comment.