Skip to content
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

Batch service install inserts #1398

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 29 additions & 39 deletions src/features/ci-cd/hooks/service-installs.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import _ from 'lodash';
import { sbvrUtils, hooks, permissions } from '@balena/pinejs';
import type { Filter, FilterObj } from 'pinejs-client-core';
import type {
Device,
PickDeferred,
Service,
ServiceInstall,
} from '../../../balena-model';
import type { Device, Service } from '../../../balena-model';

const createReleaseServiceInstalls = async (
api: sbvrUtils.PinejsClient,
Expand Down Expand Up @@ -73,14 +68,6 @@ const createReleaseServiceInstalls = async (
resource: 'device',
options: {
$select: 'id',
$expand: {
service_install: {
$select: 'installs__service',
$filter: {
installs__service: { $in: serviceIds },
},
},
},
$filter: {
// Pass the device filters instead of IDs, since a using $in errors with `code: '08P01'` for more than 66k IDs.
...(Array.isArray(deviceFilterOrIds)
Expand All @@ -96,32 +83,35 @@ const createReleaseServiceInstalls = async (
}),
},
},
})) as Array<
Pick<Device, 'id'> & {
service_install: Array<PickDeferred<ServiceInstall, 'installs__service'>>;
}
>;
})) as Array<Pick<Device, 'id'>>;
if (devicesToAddServiceInstalls.length === 0) {
return;
}

await Promise.all(
devicesToAddServiceInstalls.map(async (device) => {
const existingServiceIds = device.service_install.map(
(si) => si.installs__service.__id,
);
const deviceServiceIds = _.difference(serviceIds, existingServiceIds);
await Promise.all(
deviceServiceIds.map(async (serviceId) => {
// Create a service_install for this pair of service and device
await api.post({
resource: 'service_install',
body: {
device: device.id,
installs__service: serviceId,
},
options: { returnResource: false },
});
}),
);
}),
const deviceIds = devicesToAddServiceInstalls.map((d) => d.id);

await api.passthrough.tx!.executeSql(
`\
INSERT INTO "service install" ("device", "installs-service")
SELECT d."id" AS "device", s."id" AS "installs-service"
FROM "device" d
CROSS JOIN "service" s
WHERE d."id" IN (${_.range(1, deviceIds.length + 1)
.map((i) => `$${i}`)
.join(',')})
AND s."id" IN (${_.range(
deviceIds.length + 1,
deviceIds.length + serviceIds.length + 1,
)
.map((i) => `$${i}`)
.join(',')})
AND NOT EXISTS (
SELECT 1
FROM "service install" si
WHERE si."device" = d."id"
AND si."installs-service" = s."id"
) ;`,
[...deviceIds, ...serviceIds],
);
};

Expand Down
Loading