Skip to content

Commit a0d715c

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

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

scripts/unest_strategy.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 = [
25+
...strategiesWithoutNesting,
26+
...(strategy.params?.strategies ?? [])
27+
];
28+
});
29+
30+
await db.queryAsync(
31+
`UPDATE spaces SET settings = JSON_SET(settings, '$.strategies', ?) WHERE id = ?`,
32+
[JSON.stringify(strategiesWithoutNesting), space.id]
33+
);
34+
}
35+
36+
console.log('Done! ✅');
37+
}
38+
39+
(async () => {
40+
try {
41+
await main();
42+
process.exit(0);
43+
} catch (e) {
44+
console.error(e);
45+
process.exit(1);
46+
}
47+
})();

0 commit comments

Comments
 (0)