Skip to content

fix(set-head): query for specific version of the object in the index when verifying existence of the object version before setting head version #151

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

Merged
merged 1 commit into from
May 27, 2025
Merged
Show file tree
Hide file tree
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
32 changes: 32 additions & 0 deletions lib/plugins/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* @param {WarehouseApp} fastify Fastify instance
* @returns {Promise<void>} Promise representing plugin initialization result
*/
async function (fastify) {

Check warning on line 33 in lib/plugins/object.js

View workflow job for this annotation

GitHub Actions / test

Async function has too many statements (16). Maximum allowed is 15
const { dynamo } = fastify;

fastify.decorate(
Expand Down Expand Up @@ -457,6 +457,38 @@
}
);

/**
* Check if an Object version exists
*
* @param {Object} opts Method parameters
* @param {string} opts.name Object name
* @param {string} opts.env Environment
* @param {string} opts.version Object versions
* @returns {Promise<boolean>} True if the object version exists, false otherwise
*/
fastify.decorate(
'hasObjectVersion',
async function hasObjectVersion({ name, env, version }) {
const params = {
IndexName: 'keyname-env-index',
KeyConditionExpression: 'keyname = :name AND env = :env',
FilterExpression: 'version = :version',
ExpressionAttributeValues: {
':name': name,
':env': env,
':version': version
},
TableName: OBJECT_VARIANTS_TABLE
};
const { Items: items } = await dynamo.query(params).promise();
if (!items || items.length === 0) {
return false;
}
// Check if the version exists in the list
return items.some((item) => item.version === version);
}
);

fastify.decorate(
'putObjectVariant',
/**
Expand Down
6 changes: 3 additions & 3 deletions lib/routes/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,9 +647,9 @@ module.exports =
headVersion = fromEnvVersion.headVersion;
}

const [obj, objVersions] = await Promise.all([
const [obj, hasObjectVersion = false] = await Promise.all([
fastify.getObject({ name, env }),
fastify.getAllObjectVersions({ name, env })
fastify.hasObjectVersion({ name, env, version: headVersion })
]);

if (!obj) {
Expand All @@ -658,7 +658,7 @@ module.exports =
);
}

if (!objVersions.includes(headVersion)) {
if (!hasObjectVersion) {
throw fastify.httpErrors.notFound(
`Version ${headVersion} not found in '${env}'`
);
Expand Down
Loading