Skip to content

Commit

Permalink
Initial automatic migration
Browse files Browse the repository at this point in the history
Co-authored-by: Mark Kim <marckong@users.noreply.github.com>
  • Loading branch information
gatzjames and marckong committed Nov 28, 2023
1 parent a5d8431 commit cf5cf0d
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions packages/insomnia/src/sync/vcs/migrate-untracked-workspaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { database } from '../../common/database';
import * as models from '../../models';
import { Project } from '../../models/project';
import { Workspace } from '../../models/workspace';
import { VCS } from './vcs';

type RemoteOrganizationId = string;
type RemoteFileId = string; // project id
type RemoteProjectId = string; // team project id
type LocalWorkspaceToLocalProjectMap = Record<RemoteFileId, RemoteProjectId>;

interface OwnershipSnapshot {
ownedByMe: boolean;
isPersonal: boolean;
fileIdMap: LocalWorkspaceToLocalProjectMap;
projectIds: RemoteProjectId[];
}

type RemoteFileSnapshot = Record<RemoteOrganizationId, OwnershipSnapshot>;

async function getUserFileSnapshots(): Promise<RemoteFileSnapshot> {
return {};
}

export async function migrateUntrackedWorkspaces(vcs: VCS) {
const untrackedWorkspaces = await database.find<Workspace>(models.workspace.type, {
parentId: null,
});

if (!untrackedWorkspaces?.length) {
return null;
}

const backendProjects = await vcs._allBackendProjects();

const workspacesWithBackendProject = untrackedWorkspaces.filter(workspace => {
const backendProject = backendProjects.find(p => p.rootDocumentId === workspace._id);
return Boolean(backendProject);
});

if (workspacesWithBackendProject.length > 0) {
const remoteFileSnapshots = await getUserFileSnapshots();

if (!remoteFileSnapshots || Object.keys(remoteFileSnapshots).length === 0) {
console.warn('No remote file snapshots found for user');
return null;
}

// Map of file id to project id
const fileToProjectMap = Object.entries(remoteFileSnapshots)
.map(([, snapshot]) => {
return snapshot.fileIdMap;
})
.flat()
.reduce((acc, fileIdMap) => {
return {
...acc,
...fileIdMap,
};
}, {});

for (const workspace of workspacesWithBackendProject) {
const projectRemoteId = fileToProjectMap[workspace._id];

if (projectRemoteId) {
const project = await database.getWhere<Project>(models.project.type, {
remoteId: projectRemoteId,
});

if (project) {
await models.workspace.update(workspace, {
parentId: project._id,
});
}
}
}
}

return 'Success';
};

0 comments on commit cf5cf0d

Please sign in to comment.