diff --git a/migrations/20240906-add-templateId-and-archive-index-to-builds.js b/migrations/20240906-add-templateId-and-archive-index-to-builds.js new file mode 100644 index 00000000..351a3ea9 --- /dev/null +++ b/migrations/20240906-add-templateId-and-archive-index-to-builds.js @@ -0,0 +1,21 @@ +/* eslint-disable new-cap */ + +'use strict'; + +const prefix = process.env.DATASTORE_SEQUELIZE_PREFIX || ''; +const table = `${prefix}jobs`; + +module.exports = { + // eslint-disable-next-line no-unused-vars + up: async (queryInterface, Sequelize) => { + await queryInterface.sequelize.transaction(async transaction => { + await queryInterface.removeIndex(table, `${table}_templateId`, { transaction }); + + await queryInterface.addIndex(table, { + name: `${table}_templateId_archived`, + fields: ['templateId', 'archived'], + transaction + }); + }); + } +}; diff --git a/models/job.js b/models/job.js index 0f130b16..31b433b6 100644 --- a/models/job.js +++ b/models/job.js @@ -140,5 +140,5 @@ module.exports = { * @property indexes * @type {Array} */ - indexes: [{ fields: ['pipelineId', 'state'] }, { fields: ['state'] }, { fields: ['templateId'] }] + indexes: [{ fields: ['pipelineId', 'state'] }, { fields: ['state'] }, { fields: ['templateId', 'archived'] }] }; diff --git a/test/models/job.test.js b/test/models/job.test.js index 9e6c0623..149e29d8 100644 --- a/test/models/job.test.js +++ b/test/models/job.test.js @@ -86,7 +86,11 @@ describe('model job', () => { describe('indexes', () => { it('defines the correct indexes', () => { - const expected = [{ fields: ['pipelineId', 'state'] }, { fields: ['state'] }, { fields: ['templateId'] }]; + const expected = [ + { fields: ['pipelineId', 'state'] }, + { fields: ['state'] }, + { fields: ['templateId', 'archived'] } + ]; const { indexes } = models.job; expected.forEach(indexName => {