Skip to content

Commit

Permalink
Enedis: Add migration to fix existing aggregates (#1790)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Gilles authored May 18, 2023
1 parent d2d5cd7 commit 0ae4d71
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions server/migrations/20230518062954-enedis-reset-aggregate.js
Original file line number Diff line number Diff line change
@@ -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) => {},
};

0 comments on commit 0ae4d71

Please sign in to comment.