Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit d93d533

Browse files
committed
Cleanup
1 parent cfce23d commit d93d533

27 files changed

+1857
-0
lines changed

scripts/mongodb-migration/README.MD

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Migrate mongodb to dynamodb
2+
The following config parameters are supported, they are defined in `config/default.js` and can be configured in env variables:
3+
4+
5+
| Name | Description | Default |
6+
| :------------------------------------- | :----------------------------------------: | :------------------------------: |
7+
| LOG_LEVEL | The log level | info |
8+
| MONGODB_URL | The MongoDB URL. This needs to be the same MongoDB used by topcoder-x-receiver, topcoder-x-processor, and topcoder-x-site | mongodb://127.0.0.1:27017/topcoderx |
9+
|COLLECTION_COUNTS | The counts of testing data for each model| |
10+
|AWS_ACCESS_KEY_ID | The Amazon certificate key to use when connecting. Use local dynamodb you can set fake value |FAKE_ACCESS_KEY_ID |
11+
|AWS_SECRET_ACCESS_KEY | The Amazon certificate access key to use when connecting. Use local dynamodb you can set fake value |FAKE_SECRET_ACCESS_KEY |
12+
|AWS_REGION | The Amazon certificate region to use when connecting. Use local dynamodb you can set fake value |FAKE_REGION |
13+
|IS_LOCAL | Use Amazon DynamoDB Local or server | 'true' |
14+
| MIGRATION_DELAY_TIME | The delay used to avoid throughput errors from DynamoDB | 5000 |
15+
| MIGRATION_DELAY_INTERVAL | The number of requests made before we delay the next call to DynamoDB | 50 |
16+
17+
## Scripts
18+
19+
migrate mongodb data to dynamodb
20+
```
21+
npm run migrate-data
22+
```
23+
24+
create empty tables to dynamodb
25+
```
26+
npm run create-empty-tables
27+
```
28+
29+
## Test Data
30+
31+
**NOTE** This is a *destructive* call. It will overwrite existing data in mongodb. Do not do this against production
32+
33+
Create testing data to mongodb, this command will generate bulk fake data to testing migration.
34+
You also can create regular data by opcoder-x-receiver/topcoder-x-processor/topcoder-x-site
35+
```
36+
npm run create-test-data
37+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
/*
4+
* Copyright (c) 2018 TopCoder, Inc. All rights reserved.
5+
*/
6+
7+
/**
8+
* This module contains the configurations of the app.
9+
* Changes in 1.1:
10+
* @author TCSCODER
11+
* @version 1.1
12+
*/
13+
14+
module.exports = {
15+
MONGODB_URL: process.env.MONGODB_URL || ' ',
16+
COLLECTION_COUNTS: process.env.COLLECTION_COUNTS || 100,
17+
DYNAMODB: {
18+
AWS_ACCESS_KEY_ID: process.env.AWS_ACCESS_KEY_ID || ' ',
19+
AWS_SECRET_ACCESS_KEY: process.env.AWS_SECRET_ACCESS_KEY || ' ',
20+
AWS_REGION: process.env.AWS_REGION || 'us-east-1',
21+
IS_LOCAL: process.env.IS_LOCAL || 'false',
22+
},
23+
MIGRATION_DELAY_TIME: 5 * 1000, // 5 sec
24+
MIGRATION_DELAY_INTERVAL: 50
25+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2017 TopCoder, Inc. All rights reserved.
3+
*/
4+
5+
/**
6+
* Define constants.
7+
*
8+
* @author TCSCODER
9+
* @version 1.0
10+
*/
11+
12+
13+
// The user roles
14+
const USER_ROLES = {
15+
OWNER: 'owner',
16+
};
17+
18+
// The user types
19+
const USER_TYPES = {
20+
GITHUB: 'github',
21+
GITLAB: 'gitlab',
22+
};
23+
24+
25+
module.exports = {
26+
USER_ROLES,
27+
USER_TYPES,
28+
};
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) 2018 TopCoder, Inc. All rights reserved.
3+
*/
4+
5+
/**
6+
* Create empty tables for DynamoDB
7+
*
8+
* @author TCSCODER
9+
* @version 1.0
10+
*/
11+
12+
const Path = require('path');
13+
const fs = require('fs');
14+
const dynamoose = require('dynamoose');
15+
const config = require('config');
16+
const logger = require('./utils/logger');
17+
const schemaPath = './dynamodb/models';
18+
19+
dynamoose.AWS.config.update({
20+
accessKeyId: config.DYNAMODB.AWS_ACCESS_KEY_ID,
21+
secretAccessKey: config.DYNAMODB.AWS_SECRET_ACCESS_KEY,
22+
region: config.DYNAMODB.AWS_REGION,
23+
});
24+
25+
if (config.DYNAMODB.IS_LOCAL === 'true') {
26+
dynamoose.local();
27+
}
28+
29+
dynamoose.setDefaults({
30+
create: true,
31+
update: true,
32+
});
33+
34+
const Table = dynamoose.Table;
35+
36+
fs.readdirSync(schemaPath).forEach((file) => { // eslint-disable-line no-sync
37+
if (file !== 'index.js') {
38+
const filename = file.split('.')[0];
39+
const fileFullPath = Path.join(__dirname + schemaPath.replace('.', ''), filename);
40+
const schema = require(fileFullPath); // eslint-disable-line global-require
41+
const table_name = 'Topcoder_X.' + filename
42+
const table = new Table(table_name, schema, null, dynamoose);
43+
table.create((err) => {
44+
if(!err) {
45+
logger.info(`*** Table ${table_name} has been created ***`);
46+
} else {
47+
// if table exists, delete and re-create with empty
48+
table.delete((err) => {
49+
if (!err) {
50+
table.create((err) => {
51+
if (!err) {
52+
logger.info(`*** Table ${table_name} has been created ***`);
53+
} else
54+
{
55+
logger.info(`*** Table ${table_name} created failed -- ${err.message} ***`);
56+
}
57+
});
58+
} else {
59+
logger.info(`*** Delete exist Table ${table_name} failed -- ${err.message} ***`);
60+
}
61+
})
62+
}
63+
});
64+
}
65+
});
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
/*
2+
* Copyright (c) 2018 TopCoder, Inc. All rights reserved.
3+
*/
4+
5+
/**
6+
* This create test data
7+
*
8+
* @author TCSCODER
9+
* @version 1.0
10+
*/
11+
12+
13+
const config = require('config');
14+
const models = require('./mongodb/models');
15+
const logger = require('./utils/logger');
16+
const helper = require('./utils/helper');
17+
18+
const projectIds = [];
19+
20+
/**
21+
* Create user data
22+
*/
23+
async function createBulkUserData() {
24+
logger.info('*** create user testing data ***');
25+
for (let i = 0; i < config.COLLECTION_COUNTS; i += 1) {
26+
const data = {
27+
role: 'owner',
28+
type: i % 2 === 0 ? 'github' : 'gitlab',
29+
userProviderId:i + 1,
30+
accessToken: helper.generateIdentifier(),
31+
accessTokenExpiration: new Date(),
32+
refreshToken: helper.generateIdentifier(),
33+
username: `username#${i+1}`,
34+
};
35+
36+
const user = new models.User(data);
37+
await user.save();
38+
}
39+
40+
}
41+
42+
/**
43+
* Create usermapping data
44+
*/
45+
async function createBulkUserMappingData() {
46+
logger.info('*** create usermapping testing data ***');
47+
for (let i = 0; i < config.COLLECTION_COUNTS; i += 1) {
48+
const data = {
49+
topcoderUsername: `topcoderUsername${i+1}`,
50+
githubUsername: `githubUsername${i+1}`,
51+
gitlabUsername: `gitlabUsername${i+1}`,
52+
githubUserId: i + 1,
53+
gitlabUserId: i + 1,
54+
};
55+
56+
const usermapping = new models.UserMapping(data);
57+
await usermapping.save();
58+
}
59+
}
60+
61+
/**
62+
* Create project data
63+
*/
64+
async function createBulkProjectData() {
65+
logger.info('*** create project testing data ***');
66+
for (let i = 0; i < config.COLLECTION_COUNTS; i += 1) {
67+
const data = {
68+
title: `title#${i}`,
69+
tcDirectId: i + 1,
70+
repoUrl: `http://repoUrl${i+1}.com`,
71+
archived: i % 2 === 0 ?'false' : 'true',
72+
owner: `username#${i+1}`,
73+
secretWebhookKey: helper.generateIdentifier(),
74+
copilot: `username#${i+1}`,
75+
};
76+
77+
const project = new models.Project(data);
78+
await project.save();
79+
projectIds.push(project.id);
80+
}
81+
}
82+
83+
/**
84+
* Create issue data
85+
*/
86+
async function createBulkIssueData() {
87+
logger.info('*** create issue testing data ***');
88+
for (let i = 0; i < config.COLLECTION_COUNTS; i += 1) {
89+
const data = {
90+
number: i + 1,
91+
title: `issue title #${i}`,
92+
body: `This #${i} issue body`,
93+
prizes: [i+1],
94+
repositoryId: i + 1,
95+
labels: ['tcx_Assigned', 'tcx_ReadyForReview'],
96+
assignee: `assignee#${i}`,
97+
challengeId: i + 1,
98+
projectId: projectIds[i % 2 + 1],
99+
assignedAt: new Date(),
100+
provider: i % 2 === 0 ? 'github' : 'gitlab',
101+
};
102+
103+
const issue = new models.Issue(data);
104+
await issue.save();
105+
}
106+
}
107+
108+
/**
109+
* Create copilotpayment data
110+
*/
111+
async function createBulkCopilotPaymentData() {
112+
logger.info('*** create copilot payment testing data ***');
113+
for (let i = 0; i < config.COLLECTION_COUNTS; i += 1) {
114+
const data = {
115+
project: projectIds[i % 2 + 1],
116+
amount: (i + 1) * 20,
117+
description: `payment description #${i}`,
118+
challengeId: i + 1,
119+
closed: i % 2 === 0 ? 'false' : 'true',
120+
username: `username#${i+1}`,
121+
status: 'challenge_create_successful',
122+
};
123+
124+
const payment = new models.CopilotPayment(data);
125+
await payment.save();
126+
}
127+
}
128+
129+
/**
130+
* Create owner user team data
131+
*/
132+
async function createBulkOwnerUserTeamData() {
133+
logger.info('*** create OwnerUserTeam testing data ***');
134+
for (let i = 0; i < config.COLLECTION_COUNTS; i += 1) {
135+
const data = {
136+
ownerUsername: `owner username #${i+1}`,
137+
type: i % 2 === 0 ? 'gitlab' : 'github',
138+
teamId: `teamId#${i+1}`,
139+
ownerToken: helper.generateIdentifier(),
140+
identifier: helper.generateIdentifier(),
141+
};
142+
143+
const team = new models.OwnerUserTeam(data);
144+
await team.save();
145+
}
146+
}
147+
148+
/**
149+
* Create owner user group data
150+
*/
151+
async function createBulkOwnerUserGroupData() {
152+
logger.info('*** create OwnerUserGroup testing data ***');
153+
for (let i = 0; i < config.COLLECTION_COUNTS; i += 1) {
154+
const data = {
155+
ownerUsername: `owner username #${i+1}`,
156+
type: i % 2 === 0 ? 'gitlab' : 'github',
157+
groupId: `groupId#${i+1}`,
158+
identifier: helper.generateIdentifier(),
159+
};
160+
161+
const group = new models.OwnerUserGroup(data);
162+
await group.save();
163+
}
164+
}
165+
166+
/**
167+
* Clean mongoDB database
168+
*/
169+
async function cleanup() {
170+
await models.OwnerUserGroup.remove({});
171+
await models.OwnerUserTeam.remove({});
172+
await models.CopilotPayment.remove({});
173+
await models.Issue.remove({});
174+
await models.Project.remove({});
175+
await models.UserMapping.remove({});
176+
await models.User.remove({});
177+
}
178+
179+
async function start() {
180+
await cleanup();
181+
await createBulkUserData();
182+
await createBulkUserMappingData();
183+
await createBulkProjectData();
184+
await createBulkIssueData();
185+
await createBulkCopilotPaymentData();
186+
await createBulkOwnerUserTeamData();
187+
await createBulkOwnerUserGroupData();
188+
189+
logger.info('create testing data finish!');
190+
process.exit(0);
191+
}
192+
193+
194+
start();

0 commit comments

Comments
 (0)