Files
starr-philproperties/apps/api/scripts/backfill-durations.js
T

43 lines
1.2 KiB
JavaScript

/**
* One-time backfill: recompute duration_seconds for every lesson
* that has at least one saved page block.
*
* Run from the project root:
* node scripts/backfill-durations.js
*/
const path = require('path');
require('dotenv').config({ path: path.join(__dirname, '../.env') });
const sequelize = require('../config/db.config');
const LessonPage = require('../models/courses/lesson_page.mdl');
const { recomputeDurations } = require('../utils/duration.util');
(async () => {
await sequelize.authenticate();
console.log('DB connected.\n');
const pages = await LessonPage.findAll({
attributes: ['lesson_id', 'blocks'],
where: sequelize.literal(`jsonb_array_length(blocks::jsonb) > 0`),
});
console.log(`Found ${pages.length} lessons with blocks. Recomputing...`);
let ok = 0, fail = 0;
for (const page of pages) {
try {
await recomputeDurations(page.lesson_id);
process.stdout.write('.');
ok++;
} catch (err) {
process.stdout.write('✗');
console.error(`\n lesson ${page.lesson_id}: ${err.message}`);
fail++;
}
}
console.log(`\n\nDone — ${ok} recomputed, ${fail} failed.`);
await sequelize.close();
})();