Skip to content

Commit 227a559

Browse files
authored
Merge branch 'master' into dependabot/npm_and_yarn/puppeteer-24.10.1
2 parents 23c243c + ea102fd commit 227a559

3 files changed

Lines changed: 79 additions & 18 deletions

File tree

bin/scripts/add_indexes.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ pluginManager.dbConnection().then((countlyDb) => {
2929
() => countlyDb.collection('app_users' + app._id).ensureIndex({"tsd": 1}, { background: true }, cb),
3030
() => countlyDb.collection('app_users' + app._id).ensureIndex({"did": 1}, { background: true }, cb),
3131
() => countlyDb.collection('app_users' + app._id).dropIndex("lac_1_ls_1", cb),
32-
() => countlyDb.collection('app_user_merges' + app._id).ensureIndex({cd: 1}, {expireAfterSeconds: 60 * 60 * 3, background: true}, cb),
33-
() => countlyDb.collection('metric_changes' + app._id).ensureIndex({ts: -1}, { background: true }, cb),
3432
() => countlyDb.collection('metric_changes' + app._id).ensureIndex({ts: 1, "cc.o": 1}, { background: true }, cb),
3533
() => countlyDb.collection('metric_changes' + app._id).ensureIndex({uid: 1}, { background: true }, cb)
3634
];
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* MongoDB script to delete all collections from a database
3+
* that start with "drill_events" string, but excluding the exact "drill_events" collection
4+
*
5+
*
6+
* Server: MongoDB
7+
* Path: any
8+
* Command: mongosh -u uname -p 'password' --authenticationDatabase admin delete_old_drill_events.js
9+
*/
10+
11+
/* global db, print, quit */
12+
13+
// Set the database name
14+
const dbName = "countly_drill";
15+
16+
// Set to true for dry run, false to actually delete collections
17+
const dryRun = true;
18+
19+
print(`Using database: ${dbName}`);
20+
var cly = db;
21+
// If we need to switch to a different database
22+
if (db.getName() !== dbName) {
23+
cly = db.getSiblingDB(dbName);
24+
}
25+
26+
console.log(`Operating on database: ${dbName}`);
27+
28+
// Get all collection names that start with "drill_events" but are not exactly "drill_events"
29+
const collections = cly.getCollectionInfos().map(info => info.name).filter(collName =>
30+
collName.startsWith("drill_events") && collName !== "drill_events"
31+
);
32+
33+
// Print the collections that will be deleted
34+
print("Collections to be deleted:");
35+
collections.forEach(collName => print(`- ${collName}`));
36+
37+
// Ask for confirmation
38+
if (collections.length === 0) {
39+
print("No matching collections found to delete.");
40+
quit();
41+
}
42+
43+
print(`\nFound ${collections.length} collections to delete.`);
44+
45+
46+
if (dryRun) {
47+
print("Dry run mode is enabled. No collections will be deleted.");
48+
print("To proceed with deletion, set 'dryRun' to false in the script.");
49+
quit();
50+
}
51+
52+
// Delete each collection
53+
let deletedCount = 0;
54+
collections.forEach(collName => {
55+
try {
56+
cly[collName].drop();
57+
print(`Dropped collection: ${collName}`);
58+
deletedCount++;
59+
}
60+
catch (error) {
61+
print(`Error dropping collection ${collName}: ${error}`);
62+
}
63+
});
64+
65+
// Print summary
66+
print(`\nOperation completed. Deleted ${deletedCount} out of ${collections.length} collections.`);

bin/scripts/sharding/sharding.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
* Sharding Countly collections when DB requires authentication, provide it to authDB.auth command in the code
33
* Server: mongodb
44
* Path: any
5-
* Command: mongosh < sharding.js
5+
* Command: mongosh -u uname -p 'password' --authenticationDatabase admin sharding.js
66
*/
77

8-
/* global Mongo, print, printjson */
9-
var COUNTLY_DRILL = 'countly_drill',
10-
COUNTLY = 'countly',
11-
COUNT_TO_SHARD = 100000;
8+
/* global db, print, printjson */
9+
10+
// Set countly_drill database name
11+
const COUNTLY_DRILL = 'countly_drill';
12+
// Set countly database name
13+
const COUNTLY = 'countly';
14+
// Set the threshold for sharding collections
15+
const COUNT_TO_SHARD = 100000;
1216

1317
var EXCEPTIONS = [
1418
/^system\./,
@@ -27,14 +31,8 @@ var COUNTLY_TO_SHARD = [
2731
"feedback",
2832
];
2933

30-
var conn = new Mongo(),
31-
authDB = conn.getDB('admin');
32-
33-
// need to update this info
34-
authDB.auth('<username>', '<password>');
35-
36-
var cly = conn.getDB(COUNTLY),
37-
drill = conn.getDB(COUNTLY_DRILL);
34+
var cly = db.getSiblingDB(COUNTLY),
35+
drill = db.getSiblingDB(COUNTLY_DRILL);
3836

3937
var clyCollections = cly.getCollectionNames(), collections = clyCollections.concat(drill.getCollectionNames()), check = [];
4038

@@ -61,8 +59,7 @@ check.forEach(function(c) {
6159
var db = clyCollections.indexOf(c) === -1 ? drill : cly,
6260
dbName = clyCollections.indexOf(c) === -1 ? COUNTLY_DRILL : COUNTLY,
6361
count = db[c].count(),
64-
capped = db[c].stats()['capped'],
65-
status = db[c].getShardVersion().ok;
62+
capped = db[c].stats()['capped'];
6663

6764
COUNTLY_TO_SHARD.some((e) => {
6865
if (c.indexOf(e) == 0) {
@@ -71,7 +68,7 @@ check.forEach(function(c) {
7168
}
7269
});
7370

74-
if (!capped && count > COUNT_TO_SHARD && !status && !exceptional) {
71+
if (!capped && count > COUNT_TO_SHARD && !exceptional) {
7572
print('Creating hashed index & enabling sharding for collection "' + c + '"... ');
7673

7774
db.getCollection(c).createIndex({ _id: 'hashed' });

0 commit comments

Comments
 (0)