Add a materialized view to capture the first time a type has been seen for export

This will make it easier to create an interface which allows exporting events as
they've come up.
This commit is contained in:
R. Tyler Croy 2018-10-26 15:49:09 -07:00
parent a01c698485
commit b989ec888d
No known key found for this signature in database
GPG Key ID: 1426C7DC3F51E16F
2 changed files with 22 additions and 0 deletions

View File

@ -0,0 +1,11 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.sequelize.query('CREATE MATERIALIZED VIEW first_type AS SELECT DISTINCT(type), MIN("createdAt") FROM events GROUP BY type');
},
down: (queryInterface, Sequelize) => {
return queryInterface.sequelize.query('DROP MATERIALIZED VIEW first_type');
}
};

View File

@ -35,4 +35,15 @@ Object.keys(db).forEach(modelName => {
db.sequelize = sequelize;
db.Sequelize = Sequelize;
/*
* Periodically refresh the materialized view for holding the first types
*/
setInterval(() => {
logger.info('Refreshing the `first_type` materialized view');
sequelize.query('REFRESH MATERIALIZED VIEW first_type');
}, ((60 * 1000) * 60));
export default db;