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

[CORL-914] Consolidate MongoDB Migrations #2831

Merged
merged 1 commit into from Feb 20, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 21 additions & 11 deletions src/core/server/services/migrate/indexing.ts
@@ -1,10 +1,12 @@
import { merge } from "lodash";
import { Collection, IndexOptions } from "mongodb";
import { Collection, Db, IndexOptions } from "mongodb";
import now from "performance-now";

import { Writable } from "coral-common/types";
import logger from "coral-server/logger";

import collections from "../mongodb/collections";

type IndexType = 1 | -1 | "text";

export type IndexSpecification<T> = {
Expand Down Expand Up @@ -61,28 +63,36 @@ export function createIndexFactory<T>(

export function createConnectionOrderVariants<T>(
createIndexFn: IndexCreationFunction<T>,
variants: Array<IndexSpecification<T>>,
indexOptions: IndexOptions = { background: true }
variants: Array<IndexSpecification<T>>
) {
return async (
indexSpec: IndexSpecification<T>,
variantIndexOptions: IndexOptions = {}
) => {
return async (indexSpec: IndexSpecification<T>) => {
/**
* createIndexVariant will create a variant on the specified `indexSpec` that
* will include the new variation.
*
* @param variantSpec the spec that makes this variant different
*/
const createIndexVariant = (variantSpec: IndexSpecification<T>) =>
createIndexFn(
merge({}, indexSpec, variantSpec),
merge({}, indexOptions, variantIndexOptions)
);
createIndexFn(merge({}, indexSpec, variantSpec), { background: true });

// Create all the variants.
for (const variant of variants) {
await createIndexVariant(variant);
}
};
}

export const createIndexesFactory = (mongo: Db) => ({
users: createIndexFactory(collections.users(mongo)),
invites: createIndexFactory(collections.invites(mongo)),
tenants: createIndexFactory(collections.tenants(mongo)),
comments: createIndexFactory(collections.comments(mongo)),
stories: createIndexFactory(collections.stories(mongo)),
commentActions: createIndexFactory(collections.commentActions(mongo)),
commentModerationActions: createIndexFactory(
collections.commentModerationActions(mongo)
),
queries: createIndexFactory(collections.queries(mongo)),
migrations: createIndexFactory(collections.migrations(mongo)),
sites: createIndexFactory(collections.sites(mongo)),
});

This file was deleted.