Skip to content

Commit

Permalink
add migration to set widget ids
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus-relief committed Apr 4, 2024
1 parent ab9af7d commit 806ca64
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions migrations/1712183147024-add-widget-ids.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Dashboard } from '@models';
import { startDatabaseForMigration } from '../src/utils/migrations/database.helper';
import { v4 as uuidv4 } from 'uuid';

/**
* Sample function of up migration
*
* @returns just migrate data.
*/
export const up = async () => {
await startDatabaseForMigration();

// For each dashboard, we parse the structure to add the widget ids
const dashboards = await Dashboard.find();
dashboards.forEach((dashboard) => {
const structure = dashboard.structure;
if (Array.isArray(structure)) {
structure.forEach((widget) => {
// First we check to see if the id is already in the expected format
// which is "widget-<uuid>" using a regex
if (
!/^widget-[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/.test(
widget.id
)
) {
// If it's not, we generate a new id and assign it
widget.id = `widget-${uuidv4()}`;
widget.settings.id = widget.id;
}
});
dashboard.markModified('structure');
}
});

// Save all the dashboards
await Dashboard.bulkSave(dashboards);
};

/**
* Sample function of down migration
*
* @returns just migrate data.
*/
export const down = async () => {
/*
Code you downgrade script here!
*/
};

0 comments on commit 806ca64

Please sign in to comment.