feat(F-116): completed feature
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
* DATABASE_URL=... node scripts/seed-legacy-categories.mjs --dry-run
|
||||
*/
|
||||
import pg from 'pg';
|
||||
import { buildImportPlan } from '../dist/modules/categories/legacy/legacy-catalog.js';
|
||||
import { buildImportPlan, buildRenamePlan } from '../dist/modules/categories/legacy/legacy-catalog.js';
|
||||
|
||||
const DATABASE_URL = process.env.DATABASE_URL;
|
||||
if (!DATABASE_URL) {
|
||||
@@ -39,6 +39,7 @@ try {
|
||||
const parentBySlug = new Map(categories.rows.map((r) => [r.slug, r.id]));
|
||||
|
||||
const plan = buildImportPlan(categoryNameIndex, brandNameIndex, slugIndex);
|
||||
const renamePlan = buildRenamePlan(categoryNameIndex);
|
||||
|
||||
// Validate that every parentSlug resolves to an existing category.
|
||||
const missingParents = new Set();
|
||||
@@ -51,6 +52,7 @@ try {
|
||||
}
|
||||
|
||||
console.log(`Planned: ${plan.categories.length} categories, ${plan.brands.length} brands`);
|
||||
console.log(`Planned renames: ${renamePlan.length}`);
|
||||
if (dryRun) {
|
||||
console.log('DRY RUN — nothing will be written.');
|
||||
for (const cat of plan.categories) {
|
||||
@@ -59,6 +61,9 @@ try {
|
||||
for (const brand of plan.brands) {
|
||||
console.log(` + brand ${brand.name} (slug=${brand.slug})`);
|
||||
}
|
||||
for (const rename of renamePlan) {
|
||||
console.log(` ~ rename ${rename.oldName} → ${rename.newName}`);
|
||||
}
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
@@ -82,7 +87,18 @@ try {
|
||||
console.log(` + brand ${brand.name} (slug=${brand.slug})`);
|
||||
}
|
||||
|
||||
console.log(`\nDone. Inserted ${plan.categories.length} categories and ${plan.brands.length} brands.`);
|
||||
for (const rename of renamePlan) {
|
||||
const result = await pool.query(
|
||||
`UPDATE categories_categories SET name = $1, updated_at = now()
|
||||
WHERE name = $2`,
|
||||
[rename.newName, rename.oldName],
|
||||
);
|
||||
if (result.rowCount > 0) {
|
||||
console.log(` ~ rename ${rename.oldName} → ${rename.newName}`);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`\nDone. Inserted ${plan.categories.length} categories and ${plan.brands.length} brands. Renamed ${renamePlan.length} categories.`);
|
||||
} catch (error) {
|
||||
console.error('Error:', error.message);
|
||||
process.exit(1);
|
||||
|
||||
Reference in New Issue
Block a user