Add migration for workspace scope and apply defaults (#3031)

This commit is contained in:
Opender Singh 2021-02-02 16:02:17 +13:00 committed by GitHub
parent 1e9868152c
commit 99b75213e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 59 additions and 12 deletions

View File

@ -69,4 +69,25 @@ describe('migrate()', () => {
expect(spec).not.toBe(null);
expect(spec.fileName).toBe(workspace.name);
});
it('translates the scope correctly', async () => {
const specW = await models.workspace.create({ scope: 'spec' });
const debugW = await models.workspace.create({ scope: 'debug' });
const nullW = await models.workspace.create({ scope: null });
const somethingElseW = await models.workspace.create({ scope: 'something' });
const designerW = await models.workspace.create({ scope: 'designer' });
const collectionW = await models.workspace.create({ scope: 'collection' });
await models.workspace.migrate(specW);
await models.workspace.migrate(debugW);
await models.workspace.migrate(nullW);
await models.workspace.migrate(somethingElseW);
expect(specW.scope).toBe('designer');
expect(debugW.scope).toBe('collection');
expect(nullW.scope).toBe('collection');
expect(somethingElseW.scope).toBe('collection');
expect(designerW.scope).toBe('designer');
expect(collectionW.scope).toBe('collection');
});
});

View File

@ -2,8 +2,7 @@
import type { BaseModel } from './index';
import * as models from './index';
import * as db from '../common/database';
import { getAppId, getAppName } from '../common/constants';
import { APP_ID_DESIGNER } from '../../config';
import { getAppName } from '../common/constants';
export const name = 'Workspace';
export const type = 'Workspace';
@ -14,7 +13,7 @@ export const canSync = true;
type BaseWorkspace = {
name: string,
description: string,
scope: 'spec' | 'debug' | null,
scope: 'designer' | 'collection',
};
export type Workspace = BaseModel & BaseWorkspace;
@ -23,7 +22,7 @@ export function init() {
return {
name: 'New Workspace',
description: '',
scope: null,
scope: 'collection',
};
}
@ -31,6 +30,7 @@ export async function migrate(doc: Workspace): Promise<Workspace> {
doc = await _migrateExtractClientCertificates(doc);
doc = await _migrateEnsureName(doc);
await models.apiSpec.getOrCreateForParentId(doc._id, { fileName: doc.name });
doc = _migrateScope(doc);
return doc;
}
@ -46,7 +46,8 @@ export async function all(): Promise<Array<Workspace>> {
const workspaces = await db.all(type);
if (workspaces.length === 0) {
await create({ name: getAppName(), scope: getAppId() === APP_ID_DESIGNER ? 'spec' : null });
// Create default workspace
await create({ name: getAppName(), scope: 'collection' });
return all();
} else {
return workspaces;
@ -105,3 +106,28 @@ async function _migrateEnsureName(workspace: Workspace): Promise<Workspace> {
return workspace;
}
/**
* Ensure workspace scope is set to a valid entry
*/
function _migrateScope(workspace: Workspace): Workspace {
if (workspace.scope === 'designer' || workspace.scope === 'collection') {
return workspace;
}
// Translate the old value
type OldScopeTypes = 'spec' | 'debug' | null;
switch ((workspace.scope: OldScopeTypes)) {
case 'spec': {
workspace.scope = 'designer';
break;
}
case 'debug':
case null:
default:
workspace.scope = 'collection';
break;
}
return workspace;
}

View File

@ -54,7 +54,7 @@ describe('app.import.*', () => {
name: 'New',
parentId: null,
type: 'Workspace',
scope: null,
scope: 'collection',
},
]);
expect(await db.all(models.request.type)).toEqual([
@ -105,7 +105,7 @@ describe('app.import.*', () => {
name: 'New',
parentId: null,
type: 'Workspace',
scope: null,
scope: 'collection',
},
]);
expect(await db.all(models.request.type)).toEqual([
@ -182,7 +182,7 @@ describe('app.export.*', () => {
modified: 222,
name: 'New Workspace',
parentId: null,
scope: null,
scope: 'collection',
},
{
_id: 'req_1',

View File

@ -94,7 +94,7 @@ class DocumentCardDropdown extends React.PureComponent<Props, State> {
}
if (isLastWorkspace) {
await models.workspace.create({ name: getAppName(), scope: 'spec' });
await models.workspace.create({ name: getAppName(), scope: 'designer' });
}
await models.stats.incrementDeletedRequestsForDescendents(workspace);

View File

@ -220,7 +220,7 @@ class WorkspaceDropdown extends React.PureComponent<Props, State> {
submitName: 'Create',
selectText: true,
onComplete: async name => {
const workspace = await models.workspace.create({ name });
const workspace = await models.workspace.create({ name, scope: 'collection' });
this.props.handleSetActiveWorkspace(workspace._id);
},
});

View File

@ -82,7 +82,7 @@ class WrapperHome extends React.PureComponent<Props, State> {
onComplete: async name => {
await models.workspace.create({
name,
scope: 'spec',
scope: 'designer',
});
trackEvent('Workspace', 'Create');
@ -353,7 +353,7 @@ class WrapperHome extends React.PureComponent<Props, State> {
let label: string = 'Insomnia';
let defaultActivity = ACTIVITY_DEBUG;
if (spec || w.scope === 'spec') {
if (spec || w.scope === 'designer') {
label = '';
if (specFormat === 'openapi') {
label = `OpenAPI ${specFormatVersion}`;