overleaf/services/web/migrations/20230405190240_oauth_refresh_tokens_index.js
Liangjun Song b69ff9417a Merge pull request #20731 from overleaf/revert-20480-ls-convert-migration-scripts-to-esm
Revert "Convert migration scripts to ESM"

GitOrigin-RevId: ca9ddda6ddee8964f72797d3b653ef9cee6a7a49
2024-10-14 10:57:02 +00:00

47 lines
1.6 KiB
JavaScript

const Helpers = require('./lib/helpers')
exports.tags = ['server-ce', 'server-pro', 'saas']
const OLD_INDEX = {
key: { refreshToken: 1 },
name: 'refreshToken_1',
unique: true,
}
const NEW_INDEX = {
key: { refreshToken: 1 },
name: 'refreshToken_1',
unique: true,
partialFilterExpression: { refreshToken: { $exists: true } },
}
const TMP_INDEX = {
key: { refreshToken: 1, dummyField: 1 },
name: 'refreshToken_tmp',
}
exports.migrate = async client => {
const { db } = client
// Create a temporary index so that the refresh tokens are not left unindexed
// while we drop the index and recreate it.
await Helpers.addIndexesToCollection(db.oauthAccessTokens, [TMP_INDEX])
// Drop and recreate the index with different options
await Helpers.dropIndexesFromCollection(db.oauthAccessTokens, [OLD_INDEX])
await Helpers.addIndexesToCollection(db.oauthAccessTokens, [NEW_INDEX])
// Drop the temporary index
await Helpers.dropIndexesFromCollection(db.oauthAccessTokens, [TMP_INDEX])
}
exports.rollback = async client => {
const { db } = client
// Create a temporary index so that the refresh tokens are not left unindexed
// while we drop the index and recreate it.
await Helpers.addIndexesToCollection(db.oauthAccessTokens, [TMP_INDEX])
// Drop and recreate the index with different options
await Helpers.dropIndexesFromCollection(db.oauthAccessTokens, [NEW_INDEX])
await Helpers.addIndexesToCollection(db.oauthAccessTokens, [OLD_INDEX])
// Drop the temporary index
await Helpers.dropIndexesFromCollection(db.oauthAccessTokens, [TMP_INDEX])
}