Skip to content

Commit

Permalink
Support named collections in _default scope (#94)
Browse files Browse the repository at this point in the history
Motivation
------------
Currently doesn't work correctly with collections in the _default scope
except for the _default collection.

Modifications
---------------
Check for _default in both scope and collection before assuming that an
index is in the default collection.

Fixes #93
  • Loading branch information
brantburnett authored Oct 18, 2021
1 parent 5da7a13 commit fa00576
Show file tree
Hide file tree
Showing 6 changed files with 16,267 additions and 17,055 deletions.
2 changes: 1 addition & 1 deletion packages/couchbase-index-manager-cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ export const IndexValidators: ValidatorSet<IndexConfigurationBase> = {
}
},
post_validate: function(): void {
if (!!this.scope !== !!this.collection) {
throw new Error('if scope is supplied collection must also be supplied');
}

if (!this.is_primary) {
const isDrop = this.lifecycle && this.lifecycle.drop;

Expand Down
4 changes: 2 additions & 2 deletions packages/couchbase-index-manager/app/index-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ function isStatusMatch(index: CouchbaseIndex, status: IndexStatus): boolean {
}

export function getKeyspace(bucket: string, scope = DEFAULT_SCOPE, collection = DEFAULT_COLLECTION): string {
if (scope === DEFAULT_SCOPE) {
if (scope === DEFAULT_SCOPE && collection === DEFAULT_COLLECTION) {
return ensureEscaped(bucket);
} else {
return `${ensureEscaped(bucket)}.${ensureEscaped(scope)}.${ensureEscaped(collection)}`;
Expand Down Expand Up @@ -260,7 +260,7 @@ export class IndexManager {
private getAlterStatement(indexName: string, scope: string, collection: string, withClause: WithClause): string {
let statement: string;

if (scope === DEFAULT_SCOPE) {
if (scope === DEFAULT_SCOPE && collection === DEFAULT_COLLECTION) {
// We need to use the old syntax for the default collection for backward compatibilty
statement = `ALTER INDEX ${ensureEscaped(this.bucketName)}.${ensureEscaped(indexName)} WITH `;
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/couchbase-index-manager/app/plan/index-mutation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IndexDefinition } from "../definition";
import { DEFAULT_SCOPE, IndexManager } from "../index-manager";
import { DEFAULT_COLLECTION, DEFAULT_SCOPE, IndexManager } from "../index-manager";
import { Logger } from "../options";

/**
Expand All @@ -15,7 +15,7 @@ export abstract class IndexMutation {
* A display name for the index which includes the scope and collection if non-default.
*/
get displayName(): string {
if (this.scope === DEFAULT_SCOPE) {
if (this.scope === DEFAULT_SCOPE && this.collection === DEFAULT_COLLECTION) {
return this.name;
} else {
return `${this.scope}.${this.collection}.${this.name}`;
Expand Down
4 changes: 2 additions & 2 deletions packages/couchbase-index-manager/app/plan/plan.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from 'chalk';
import { padStart, flatten } from 'lodash';
import { DEFAULT_SCOPE, IndexManager, WaitForIndexBuildOptions } from '../index-manager';
import { DEFAULT_COLLECTION, DEFAULT_SCOPE, IndexManager, WaitForIndexBuildOptions } from '../index-manager';
import { IndexMutation } from './index-mutation';
import { Logger } from '../options';

Expand Down Expand Up @@ -172,7 +172,7 @@ export class Plan {

// Build each collection separately
for (const collection of collections) {
if (collection.scope === DEFAULT_SCOPE) {
if (collection.scope === DEFAULT_SCOPE && collection.collection === DEFAULT_COLLECTION) {
this.options.logger.info(chalk.greenBright('Building indexes...'));
} else {
this.options.logger.info(chalk.greenBright(`Building indexes on ${collection.scope}.${collection.collection}...`));
Expand Down
Loading

0 comments on commit fa00576

Please sign in to comment.