From 47fab9102e571d39eb0b1b2e856c2cc6e949d1f6 Mon Sep 17 00:00:00 2001 From: mariscalromeroalejandro Date: Thu, 24 Apr 2025 13:37:43 +0200 Subject: [PATCH 1/2] migrateJsonToDB --- QualityControl/scripts/data-migration.js | 65 ++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 QualityControl/scripts/data-migration.js diff --git a/QualityControl/scripts/data-migration.js b/QualityControl/scripts/data-migration.js new file mode 100644 index 000000000..e3336ed20 --- /dev/null +++ b/QualityControl/scripts/data-migration.js @@ -0,0 +1,65 @@ +import fs from 'fs'; +import { User, Layout, Tab, Chart, GridTabCell } from './models'; + +// IMPORTANT: Specify the path to the JSON file to be migrated here +const JSON_FILE_PATH = ''; + +const rawData = fs.readFileSync(JSON_FILE_PATH, 'utf-8'); +const data = JSON.parse(rawData); + +/** + * Migrates data from JSON file to the database. + */ +async function migrateJsonToDB() { + for (const user of data.users) { + await User.create({ + id: user.id, + username: user.username, + name: user.name, + }); + } + + for (const layout of data.layouts) { + await Layout.create({ + id: layout.id, + name: layout.name, + description: layout.description, + display_timestamp: layout.displayTimestamp, + auto_tab_change_interval: layout.autoTabChange, + owner_username: data.users.find((u) => u.id === layout.owner_id)?.username, + }); + + for (const tab of layout.tabs) { + await Tab.create({ + id: tab.id, + name: tab.name, + layout_id: layout.id, + column_count: tab.columns, + }); + + for (const obj of tab.objects) { + await Chart.create({ + id: obj.id, + object_name: obj.name, + ignore_defaults: obj.ignoreDefaults, + }); + + await GridTabCell.create({ + chart_id: obj.id, + row: obj.y, + col: obj.x, + tab_id: tab.id, + row_span: obj.h, + col_span: obj.w, + }); + } + } + } +} + +migrateJsonToDB() + .then(() => { + console.log('✅ Successfully migrated data to the database'); + }).catch((error) => { + console.error('❌ Error migrating data:', error); + }); From 6a865214e8a600e9a83991750040caa31a7e28f8 Mon Sep 17 00:00:00 2001 From: mariscalromeroalejandro Date: Thu, 24 Apr 2025 14:05:40 +0200 Subject: [PATCH 2/2] add chartOptions to data migration file --- QualityControl/scripts/data-migration.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/QualityControl/scripts/data-migration.js b/QualityControl/scripts/data-migration.js index e3336ed20..c0517663b 100644 --- a/QualityControl/scripts/data-migration.js +++ b/QualityControl/scripts/data-migration.js @@ -1,8 +1,10 @@ import fs from 'fs'; -import { User, Layout, Tab, Chart, GridTabCell } from './models'; +import { User, Layout, Tab, Chart, GridTabCell, ChartOption } from './../lib/database/models/index.js'; +import { LogManager } from '@aliceo2/web-ui'; // IMPORTANT: Specify the path to the JSON file to be migrated here const JSON_FILE_PATH = ''; +const _logger = new LogManager.getLogger('data-migration'); const rawData = fs.readFileSync(JSON_FILE_PATH, 'utf-8'); const data = JSON.parse(rawData); @@ -38,12 +40,24 @@ async function migrateJsonToDB() { }); for (const obj of tab.objects) { - await Chart.create({ + const newChart = await Chart.create({ id: obj.id, object_name: obj.name, ignore_defaults: obj.ignoreDefaults, }); + for (const optionName of obj.options) { + const option = await Option.findOne({ where: { name: optionName } }); + if (option) { + await ChartOption.create({ + chart_id: newChart.id, + option_id: option.id, + created_at: new Date(), + updated_at: new Date(), + }); + } + } + await GridTabCell.create({ chart_id: obj.id, row: obj.y, @@ -59,7 +73,7 @@ async function migrateJsonToDB() { migrateJsonToDB() .then(() => { - console.log('✅ Successfully migrated data to the database'); + _logger.infoMessage('Data migration completed successfully.'); }).catch((error) => { - console.error('❌ Error migrating data:', error); + _logger.errorMessage(`Data migration failed: ${error.message}`); });