generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmigrate.ts
More file actions
213 lines (188 loc) · 6.95 KB
/
migrate.ts
File metadata and controls
213 lines (188 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-explicit-any */
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import * as os from 'os';
import { flags } from '@salesforce/command';
import { Messages } from '@salesforce/core';
import '../../../utils/prototypes';
import OmniStudioBaseCommand from '../../basecommand';
import { DataRaptorMigrationTool } from '../../../migration/dataraptor';
import { DebugTimer, MigratedObject, MigratedRecordInfo } from '../../../utils';
import { MigrationResult, MigrationTool } from '../../../migration/interfaces';
import { ResultsBuilder } from '../../../utils/resultsbuilder';
import { CardMigrationTool } from '../../../migration/flexcard';
import { OmniScriptExportType, OmniScriptMigrationTool } from '../../../migration/omniscript';
// Initialize Messages with the current plugin directory
Messages.importMessagesDirectory(__dirname);
// Load the specific messages for this file. Messages from @salesforce/command, @salesforce/core,
// or any library that is using the messages framework can also be loaded this way.
const messages = Messages.loadMessages('@salesforce/plugin-omnistudio-migration-tool', 'migrate');
export default class Migrate extends OmniStudioBaseCommand {
public static description = messages.getMessage('commandDescription');
public static examples = messages.getMessage('examples').split(os.EOL);
public static args = [{ name: 'file' }];
protected static flagsConfig = {
namespace: flags.string({
char: 'n',
description: messages.getMessage('namespaceFlagDescription'),
}),
only: flags.string({
char: 'o',
description: messages.getMessage('onlyFlagDescription'),
}),
allversions: flags.boolean({
char: 'a',
description: messages.getMessage('allVersionsDescription'),
required: false,
}),
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public async run(): Promise<any> {
const namespace = (this.flags.namespace || 'vlocity_ins') as string;
const apiVersion = (this.flags.apiversion || '55.0') as string;
const migrateOnly = (this.flags.only || '') as string;
const allVersions = this.flags.allversions || false;
// this.org is guaranteed because requiresUsername=true, as opposed to supportsUsername
const conn = this.org.getConnection();
conn.setApiVersion(apiVersion);
// Let's time every step
DebugTimer.getInstance().start();
// Register the migration objects
let migrationObjects: MigrationTool[] = [];
if (!migrateOnly) {
migrationObjects = [
new DataRaptorMigrationTool(namespace, conn, this.logger, messages, this.ux),
new OmniScriptMigrationTool(
OmniScriptExportType.All,
namespace,
conn,
this.logger,
messages,
this.ux,
allVersions
),
new CardMigrationTool(namespace, conn, this.logger, messages, this.ux, allVersions),
];
} else {
switch (migrateOnly) {
case 'os':
migrationObjects.push(
new OmniScriptMigrationTool(
OmniScriptExportType.OS,
namespace,
conn,
this.logger,
messages,
this.ux,
allVersions
)
);
break;
case 'ip':
migrationObjects.push(
new OmniScriptMigrationTool(
OmniScriptExportType.IP,
namespace,
conn,
this.logger,
messages,
this.ux,
allVersions
)
);
break;
case 'fc':
migrationObjects.push(new CardMigrationTool(namespace, conn, this.logger, messages, this.ux, allVersions));
break;
case 'dr':
migrationObjects.push(new DataRaptorMigrationTool(namespace, conn, this.logger, messages, this.ux));
break;
default:
throw new Error(messages.getMessage('invalidOnlyFlag'));
}
}
// Migrate individual objects
const debugTimer = DebugTimer.getInstance();
let objectMigrationResults: MigratedObject[] = [];
// We need to truncate the standard objects first
let allTruncateComplete = true;
for (const cls of migrationObjects.reverse()) {
try {
this.ux.log('Cleaning: ' + cls.getName());
debugTimer.lap('Cleaning: ' + cls.getName());
await cls.truncate();
} catch (ex: any) {
allTruncateComplete = false;
objectMigrationResults.push({
name: cls.getName(),
errors: [ex.message],
});
}
}
if (allTruncateComplete) {
for (const cls of migrationObjects.reverse()) {
try {
this.ux.log('Migrating: ' + cls.getName());
debugTimer.lap('Migrating: ' + cls.getName());
const results = await cls.migrate();
objectMigrationResults = objectMigrationResults.concat(
results.map((r) => {
return {
name: r.name,
data: this.mergeRecordAndUploadResults(r, cls),
};
})
);
} catch (ex: any) {
this.logger.error(JSON.stringify(ex));
objectMigrationResults.push({
name: cls.getName(),
errors: [ex.message],
});
}
}
}
// Stop the debug timer
const timer = DebugTimer.getInstance().stop();
await ResultsBuilder.generate(objectMigrationResults, conn.instanceUrl);
// save timer to debug logger
this.logger.debug(timer);
// Return results needed for --json flag
return { objectMigrationResults };
}
private mergeRecordAndUploadResults(
migrationResults: MigrationResult,
migrationTool: MigrationTool
): MigratedRecordInfo[] {
const mergedResults: MigratedRecordInfo[] = [];
for (const record of Array.from(migrationResults.records.values())) {
const obj = {
id: record['Id'],
name: migrationTool.getRecordName(record),
status: 'Skipped',
errors: record['errors'],
migratedId: undefined,
warnings: [],
migratedName: '',
};
if (migrationResults.results.has(record['Id'])) {
const recordResults = migrationResults.results.get(record['Id']);
let errors: any[] = obj.errors || [];
errors = errors.concat(recordResults.errors || []);
obj.status = !recordResults || recordResults.hasErrors ? 'Error' : 'Complete';
obj.errors = errors;
obj.migratedId = recordResults.id;
obj.warnings = recordResults.warnings;
obj.migratedName = recordResults.newName;
}
mergedResults.push(obj);
}
return mergedResults;
}
}