This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmigrate.ts
257 lines (233 loc) · 5.59 KB
/
migrate.ts
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* office-maker-api migration script
*
* This script copies all data in DynamoDB and S3 from one environment to another environment
* It is useful when you create a new environment and need some test data
*
* Example) migrate from `stg` environment to `dev` environment
* ~$ ./migrate.sh stg dev
*
* Remark: if AWS Backup comes to ap-northeast-1 region, you might use it instead.
*/
import * as aws from 'aws-sdk';
// Specify region by environment variable `REGION`. Default is `ap-northeast-1`
const region = process.env.REGION || 'ap-northeast-1';
const dynamo = new aws.DynamoDB.DocumentClient({
region
});
const s3 = new aws.S3({
region
});
// Split an array into smaller chunks
const chunksOf = <T>(arr: T[], len: number) => {
let chunks: T[][] = [],
i = 0,
n = arr.length;
while (i < n) {
chunks.push(arr.slice(i, (i += len)));
}
return chunks;
};
const scanAndCopyTable = async (sourceTable: string, targetTable: string) => {
const scanResult = await dynamo
.scan({
TableName: sourceTable
})
.promise();
await chunksOf(scanResult.Items, 25).forEach(async (items, index) => {
await dynamo
.batchWrite({
RequestItems: {
[targetTable]: items.map(item => {
return {
PutRequest: {
Item: item
}
};
})
}
})
.promise();
console.info(
`Copying: ${sourceTable} --> ${targetTable}, ${(index + 1) *
25} items done...`
);
});
console.info(`Migration done: ${sourceTable} --> ${targetTable}`);
};
type QueryOption = {
hashKey: {
name: string;
value: string;
};
rangeKey?: {
name: string;
value: string;
};
};
const queryAndCopyTable = async (
sourceTable: string,
targetTable: string,
option: QueryOption
) => {
const queryResult = await dynamo
.query({
TableName: sourceTable,
KeyConditionExpression: option.rangeKey
? '#hash = :hash AND #range = :range'
: '#hash = :hash',
ExpressionAttributeNames: Object.assign(
{
'#hash': option.hashKey.name
},
option.rangeKey && { '#range': option.rangeKey.name }
),
ExpressionAttributeValues: Object.assign(
{
':hash': option.hashKey.value
},
option.rangeKey && { ':range': option.rangeKey.value }
)
})
.promise();
await chunksOf(queryResult.Items, 25).forEach(async (items, index) => {
await dynamo
.batchWrite({
RequestItems: {
[targetTable]: items.map(item => {
return {
PutRequest: {
Item: item
}
};
})
}
})
.promise();
console.info(
`Copying: ${sourceTable} --> ${targetTable}, ${(index + 1) *
25} items done...`
);
});
console.info(`Migration done: ${sourceTable} --> ${targetTable}`);
};
const performCopyTable = (
sourceTable: string,
targetTable: string,
option?: QueryOption
) => {
if (option) {
return queryAndCopyTable(sourceTable, targetTable, option);
} else {
return scanAndCopyTable(sourceTable, targetTable);
}
};
const performCopyS3 = async (
sourceBucket: string,
targetBucket: string,
option?: {
prefix: string;
}
) => {
const listResult = await s3
.listObjectsV2(
Object.assign(
{
Bucket: sourceBucket
},
option && { Prefix: option.prefix }
)
)
.promise();
await listResult.Contents.forEach(async content => {
s3.copyObject({
Bucket: targetBucket,
CopySource: `${sourceBucket}/${content.Key}`,
Key: content.Key
});
});
console.info(`Migration done: ${sourceBucket} --> ${targetBucket}`);
};
if (process.argv.length < 4) {
console.log(
`office-maker-api migration script
Usage:
~$ ts-node migrate.ts stg dev
~$ FLOOR_ID=xxxxxxxx ts-node migrate.ts stg dev # if you want to migrate only a floor`
);
} else {
const source = process.argv[2];
const target = process.argv[3];
if (target == 'prod') {
console.error('Do not write to the prod account. Abort.');
process.exit(1);
}
const copyTable = (name, option?: QueryOption) => {
const tableNameOf = (env, name) => `office-maker-map-${env}_${name}`;
performCopyTable(
tableNameOf(source, name),
tableNameOf(target, name),
option
);
};
const copyS3 = (option?: { prefix: string }) => {
const bucketNameOf = env => `office-maker-storage-${env}`;
performCopyS3(bucketNameOf(source), bucketNameOf(target), option);
};
copyTable('colors');
copyTable('prototypes');
copyTable(
'edit_floors',
process.env.FLOOR_ID && {
hashKey: {
name: 'tenantId',
value: 'worksap.co.jp'
},
rangeKey: {
name: 'id',
value: process.env.FLOOR_ID
}
}
);
copyTable(
'edit_objects',
process.env.FLOOR_ID && {
hashKey: {
name: 'floorId',
value: process.env.FLOOR_ID
}
}
);
copyTable(
'public_floors',
process.env.FLOOR_ID && {
hashKey: {
name: 'tenantId',
value: 'worksap.co.jp'
},
rangeKey: {
name: 'id',
value: process.env.FLOOR_ID
}
}
);
copyTable(
'public_objects',
process.env.FLOOR_ID && {
hashKey: {
name: 'floorId',
value: process.env.FLOOR_ID
}
}
);
copyS3(
process.env.FLOOR_ID && {
prefix: `files/floors/${process.env.FLOOR_ID}`
}
);
copyS3(
process.env.FLOOR_ID && {
prefix: `images/floors/${process.env.FLOOR_ID}`
}
);
}