-
-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enedis: Add migration to fix existing aggregates (#1790)
- Loading branch information
1 parent
d2d5cd7
commit 0ae4d71
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
server/migrations/20230518062954-enedis-reset-aggregate.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => {}, | ||
}; |