From 0ae4d71c60349375a719f77cda91457ec77409ff Mon Sep 17 00:00:00 2001 From: Pierre-Gilles Leymarie Date: Thu, 18 May 2023 13:40:05 +0700 Subject: [PATCH] Enedis: Add migration to fix existing aggregates (#1790) --- .../20230518062954-enedis-reset-aggregate.js | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 server/migrations/20230518062954-enedis-reset-aggregate.js diff --git a/server/migrations/20230518062954-enedis-reset-aggregate.js b/server/migrations/20230518062954-enedis-reset-aggregate.js new file mode 100644 index 0000000000..a3a7d82d0c --- /dev/null +++ b/server/migrations/20230518062954-enedis-reset-aggregate.js @@ -0,0 +1,44 @@ +const Promise = require('bluebird'); +const db = require('../models'); +const logger = require('../utils/logger'); +const { DEVICE_FEATURE_CATEGORIES, DEVICE_FEATURE_TYPES } = require('../utils/constants'); + +module.exports = { + up: async (queryInterface, Sequelize) => { + const service = await db.Service.findOne({ + where: { + name: 'enedis', + }, + }); + if (service === null) { + return; + } + logger.info(`Enedis migration: Found service enedis = ${service.id}`); + const enedisDevices = await db.Device.findAll({ + where: { + service_id: service.id, + }, + }); + logger.info(`Enedis migration: Found ${enedisDevices.length} enedis devices`); + await Promise.each(enedisDevices, async (enedisDevice) => { + const deviceFeature = await db.DeviceFeature.findOne({ + where: { + device_id: enedisDevice.id, + category: DEVICE_FEATURE_CATEGORIES.ENERGY_SENSOR, + type: DEVICE_FEATURE_TYPES.ENERGY_SENSOR.DAILY_CONSUMPTION, + }, + }); + if (deviceFeature === null) { + return; + } + logger.info(`Enedis migration: Updating device_feature ${deviceFeature.id} with updated aggregate`); + deviceFeature.set({ + last_monthly_aggregate: null, + last_daily_aggregate: null, + last_hourly_aggregate: null, + }); + await deviceFeature.save(); + }); + }, + down: async (queryInterface, Sequelize) => {}, +};