Skip to content

Commit abbc033

Browse files
committed
feat: add script to un-nest strategies
1 parent 1a0903b commit abbc033

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

scripts/unest_strategy.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import 'dotenv/config';
2+
import db from '../src/helpers/mysql';
3+
4+
const STRATEGY_NAME = 'multichain';
5+
6+
// Usage: yarn ts-node scripts/unest_strategy.ts
7+
async function main() {
8+
const spaces = await db.queryAsync(
9+
`SELECT settings->'$.strategies[*].name', id, settings
10+
FROM spaces
11+
WHERE JSON_CONTAINS(settings->'$.strategies[*].name', ?)
12+
`,
13+
JSON.stringify([STRATEGY_NAME])
14+
);
15+
16+
console.log('Retrieved spaces count:', spaces.length);
17+
18+
for (const space of spaces) {
19+
const strategies = JSON.parse(space.settings).strategies;
20+
let strategiesWithoutNesting = strategies.filter((s: any) => s.name !== STRATEGY_NAME);
21+
const nestedStrategies = strategies.filter((s: any) => s.name === STRATEGY_NAME);
22+
23+
nestedStrategies.forEach((strategy: any) => {
24+
strategiesWithoutNesting = strategiesWithoutNesting.concat(strategy.params?.strategies ?? []);
25+
});
26+
27+
await db.queryAsync(
28+
`UPDATE spaces SET settings = JSON_SET(settings, '$.strategies', ?) WHERE id = ?`,
29+
[JSON.stringify(strategiesWithoutNesting), space.id]
30+
);
31+
}
32+
33+
console.log('Done! ✅');
34+
}
35+
36+
(async () => {
37+
try {
38+
await main();
39+
process.exit(0);
40+
} catch (e) {
41+
console.error(e);
42+
process.exit(1);
43+
}
44+
})();

0 commit comments

Comments
 (0)