|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright CERN and copyright holders of ALICE O2. This software is |
| 4 | + * distributed under the terms of the GNU General Public License v3 (GPL |
| 5 | + * Version 3), copied verbatim in the file "COPYING". |
| 6 | + * |
| 7 | + * See http://alice-o2.web.cern.ch/license for full licensing information. |
| 8 | + * |
| 9 | + * In applying this license CERN does not waive the privileges and immunities |
| 10 | + * granted to it by virtue of its status as an Intergovernmental Organization |
| 11 | + * or submit itself to any jurisdiction. |
| 12 | + */ |
| 13 | + |
| 14 | +'use strict'; |
| 15 | + |
| 16 | +const { Sequelize } = require('sequelize'); |
| 17 | + |
| 18 | +/** @type {import('sequelize-cli').Migration} */ |
| 19 | +module.exports = { |
| 20 | + up: async (queryInterface) => queryInterface.sequelize.transaction(async (transaction) => { |
| 21 | + await queryInterface.createTable('data_pass_versions', { |
| 22 | + id: { |
| 23 | + type: Sequelize.INTEGER, |
| 24 | + primaryKey: true, |
| 25 | + autoIncrement: true, |
| 26 | + allowNull: false, |
| 27 | + }, |
| 28 | + data_pass_id: { |
| 29 | + type: Sequelize.INTEGER, |
| 30 | + allowNull: false, |
| 31 | + references: { |
| 32 | + model: 'data_passes', |
| 33 | + key: 'id', |
| 34 | + }, |
| 35 | + }, |
| 36 | + description: { |
| 37 | + type: Sequelize.STRING, |
| 38 | + unique: true, |
| 39 | + allowNull: false, |
| 40 | + }, |
| 41 | + output_size: { |
| 42 | + type: Sequelize.BIGINT, |
| 43 | + }, |
| 44 | + reconstructed_events_count: { |
| 45 | + type: Sequelize.INTEGER, |
| 46 | + }, |
| 47 | + last_seen: { |
| 48 | + type: Sequelize.INTEGER, |
| 49 | + comment: 'Change of this property in MonALISA means recent job activity that changed data pass version properties', |
| 50 | + }, |
| 51 | + |
| 52 | + // Timestamps |
| 53 | + created_at: { |
| 54 | + allowNull: false, |
| 55 | + type: Sequelize.DATE, |
| 56 | + defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), |
| 57 | + }, |
| 58 | + updated_at: { |
| 59 | + allowNull: false, |
| 60 | + type: Sequelize.DATE, |
| 61 | + defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), |
| 62 | + }, |
| 63 | + }, { transaction }); |
| 64 | + |
| 65 | + // Migrate |
| 66 | + await queryInterface.sequelize.query(` |
| 67 | + INSERT INTO data_pass_versions (data_pass_id, description, output_size, reconstructed_events_count) |
| 68 | + SELECT id, description, output_size, reconstructed_events_count |
| 69 | + FROM data_passes |
| 70 | + `, { transaction }); |
| 71 | + |
| 72 | + await queryInterface.removeColumn('data_passes', 'description', { transaction }); |
| 73 | + await queryInterface.removeColumn('data_passes', 'output_size', { transaction }); |
| 74 | + await queryInterface.removeColumn('data_passes', 'reconstructed_events_count', { transaction }); |
| 75 | + await queryInterface.removeColumn('data_passes', 'last_run_number', { transaction }); |
| 76 | + }), |
| 77 | + |
| 78 | + down: async (queryInterface) => queryInterface.sequelize.transaction(async (transaction) => { |
| 79 | + await queryInterface.addColumn('data_passes', 'description', { type: Sequelize.TEXT }, { transaction }); |
| 80 | + await queryInterface.addColumn('data_passes', 'output_size', { type: Sequelize.BIGINT }, { transaction }); |
| 81 | + await queryInterface.addColumn('data_passes', 'reconstructed_events_count', { type: Sequelize.INTEGER }, { transaction }); |
| 82 | + await queryInterface.addColumn('data_passes', 'last_run_number', { type: Sequelize.INTEGER }, { transaction }); |
| 83 | + |
| 84 | + // Migrate |
| 85 | + await queryInterface.sequelize.query(` |
| 86 | + UPDATE data_passes |
| 87 | + INNER JOIN ( |
| 88 | + WITH lastwin as ( |
| 89 | + SELECT |
| 90 | + id, |
| 91 | + data_pass_id, |
| 92 | + description, |
| 93 | + output_size, |
| 94 | + reconstructed_events_count, |
| 95 | + ROW_NUMBER() OVER (PARTITION BY data_pass_id ORDER BY id DESC) as rn |
| 96 | + FROM data_pass_versions |
| 97 | + ) |
| 98 | + SELECT data_pass_id, description, output_size, reconstructed_events_count |
| 99 | + FROM lastwin |
| 100 | + WHERE rn = 1 |
| 101 | + ) as recent_data_pass_version |
| 102 | +
|
| 103 | + ON recent_data_pass_version.data_pass_id = data_passes.id |
| 104 | +
|
| 105 | + SET data_passes.description = recent_data_pass_version.description, |
| 106 | + data_passes.output_size = recent_data_pass_version.output_size, |
| 107 | + data_passes.reconstructed_events_count = recent_data_pass_version.reconstructed_events_count |
| 108 | + |
| 109 | + `, { transaction }); |
| 110 | + |
| 111 | + await queryInterface.dropTable('data_pass_versions', { transaction }); |
| 112 | + }), |
| 113 | +}; |
0 commit comments