'use strict'; /** * 修正 products 表列名,使其与外部商品结构及 Product 模型保持一致: * code→item_code、name→item_name、sku_code→goods_code、type→item_type、 * category→category_name、spec→sku_property、unit→stock_unit、price→retail_price * * 背景:迁移 20260910000500 曾把上述列改名后又改回旧名,导致 Product 模型/控制器按 * item_code / item_name / item_type 等查询时报 "Unknown column 'item_type' in 'field list'"。 * 本迁移幂等:仅当旧列存在、新列不存在时才重命名,可重复执行。 */ module.exports = { async up(queryInterface) { const cols = await queryInterface.describeTable('products'); const has = (n) => Object.prototype.hasOwnProperty.call(cols, n); const rename = async (from, to) => { if (has(from) && !has(to)) { await queryInterface.renameColumn('products', from, to); } }; await rename('code', 'item_code'); await rename('name', 'item_name'); await rename('sku_code', 'goods_code'); await rename('type', 'item_type'); await rename('category', 'category_name'); await rename('spec', 'sku_property'); await rename('unit', 'stock_unit'); await rename('price', 'retail_price'); }, async down(queryInterface) { const cols = await queryInterface.describeTable('products'); const has = (n) => Object.prototype.hasOwnProperty.call(cols, n); const rename = async (from, to) => { if (has(from) && !has(to)) { await queryInterface.renameColumn('products', from, to); } }; await rename('item_code', 'code'); await rename('item_name', 'name'); await rename('goods_code', 'sku_code'); await rename('item_type', 'type'); await rename('category_name', 'category'); await rename('sku_property', 'spec'); await rename('stock_unit', 'unit'); await rename('retail_price', 'price'); }, };