directus
102 строки · 2.7 Кб
1import type { Knex } from 'knex';
2
3export async function up(knex: Knex) {
4const panels = await knex('directus_panels').where('type', '=', 'metric').select();
5
6const updates: Promise<number>[] = [];
7
8for (const panel of panels) {
9let options = panel.options;
10
11// Check if the options are stringified and parse them
12const wasStringified = typeof options === 'string';
13
14if (wasStringified) {
15options = JSON.parse(options);
16}
17
18// Not expected, just to be on the safe side
19if (!options) continue;
20
21let needsUpdate = false;
22
23// Check and update abbreviate -> notation
24if (options.abbreviate === true) {
25options.notation = 'compact';
26delete options.abbreviate;
27needsUpdate = true;
28}
29
30// Check and update decimals -> minimumFractionDigits and maximumFractionDigits
31if (typeof options.decimals === 'number') {
32options.minimumFractionDigits = options.decimals;
33options.maximumFractionDigits = options.decimals;
34delete options.decimals;
35needsUpdate = true;
36}
37
38// Update the row with modified options if necessary
39if (needsUpdate) {
40// Convert the options back to string if they were stringified initially
41if (wasStringified) {
42options = JSON.stringify(options);
43}
44
45updates.push(knex('directus_panels').update({ options }).where('id', panel.id));
46}
47}
48
49return Promise.all(updates);
50}
51
52export async function down(knex: Knex) {
53const panels = await knex('directus_panels').where('type', '=', 'metric').select();
54
55const updates: Promise<number>[] = [];
56
57for (const panel of panels) {
58let options = panel.options;
59
60// Check if the options are stringified and parse them
61const wasStringified = typeof options === 'string';
62
63if (wasStringified) {
64options = JSON.parse(options);
65}
66
67// Not expected, just to be on the safe side
68if (!options) continue;
69
70let needsUpdate = false;
71
72// Revert notation -> abbreviate
73if (options.notation === 'compact') {
74options.abbreviate = true;
75delete options.notation;
76needsUpdate = true;
77}
78
79// Revert minimumFractionDigits and maximumFractionDigits -> decimals
80if (
81typeof options.minimumFractionDigits === 'number' &&
82options.minimumFractionDigits === options.maximumFractionDigits
83) {
84options.decimals = options.minimumFractionDigits;
85delete options.minimumFractionDigits;
86delete options.maximumFractionDigits;
87needsUpdate = true;
88}
89
90// Update the row with reverted options if necessary
91if (needsUpdate) {
92// Convert the options back to string if they were stringified initially
93if (wasStringified) {
94options = JSON.stringify(options);
95}
96
97updates.push(knex('directus_panels').update({ options }).where('id', panel.id));
98}
99}
100
101return Promise.all(updates);
102}
103