mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* 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();
|
||||
})();
|
||||
Reference in New Issue
Block a user