feat: enhance product analysis results with additional fields and update handling logic
This commit is contained in:
@@ -49,7 +49,7 @@ function createProductAnalysisResultsTable(database: Database): void {
|
||||
confidence REAL NOT NULL,
|
||||
reasoning TEXT,
|
||||
fetched_at TEXT NOT NULL,
|
||||
UNIQUE(run_id, asin),
|
||||
UNIQUE(asin),
|
||||
FOREIGN KEY (run_id) REFERENCES category_analysis_runs(id)
|
||||
);
|
||||
`);
|
||||
@@ -70,12 +70,38 @@ function ensureProductAnalysisResultsTable(database: Database): void {
|
||||
(col) => col.name === "asin" && col.pk === 1,
|
||||
);
|
||||
|
||||
if (!hasIdColumn || hasAsinPrimaryKey) {
|
||||
const indexList = database
|
||||
.query("PRAGMA index_list(product_analysis_results)")
|
||||
.all() as Array<{ name: string; unique: number }>;
|
||||
const hasUniqueAsinConstraint = indexList.some((idx) => {
|
||||
if (idx.unique !== 1) return false;
|
||||
const columns = database
|
||||
.query(`PRAGMA index_info(${JSON.stringify(idx.name)})`)
|
||||
.all() as Array<{ name: string }>;
|
||||
return columns.length === 1 && columns[0]?.name === "asin";
|
||||
});
|
||||
|
||||
if (!hasIdColumn || hasAsinPrimaryKey || !hasUniqueAsinConstraint) {
|
||||
database.run(
|
||||
"ALTER TABLE product_analysis_results RENAME TO product_analysis_results_legacy",
|
||||
);
|
||||
createProductAnalysisResultsTable(database);
|
||||
database.run(`
|
||||
WITH ranked AS (
|
||||
SELECT
|
||||
asin, run_id, name, brand, category, unit_cost,
|
||||
current_price, avg_price_90d, avg_price_90d_sheet,
|
||||
selling_price_sheet, sales_rank, sales_rank_avg_90d,
|
||||
seller_count, monthly_sold, rank_drops_30d, rank_drops_90d,
|
||||
fba_fee, fbm_fee, referral_percent, can_sell,
|
||||
sellability_status, sellability_reason,
|
||||
verdict, confidence, reasoning, fetched_at,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY asin
|
||||
ORDER BY datetime(fetched_at) DESC, run_id DESC, id DESC
|
||||
) AS row_num
|
||||
FROM product_analysis_results_legacy
|
||||
)
|
||||
INSERT INTO product_analysis_results (
|
||||
asin, run_id, name, brand, category, unit_cost,
|
||||
current_price, avg_price_90d, avg_price_90d_sheet,
|
||||
@@ -93,7 +119,8 @@ function ensureProductAnalysisResultsTable(database: Database): void {
|
||||
fba_fee, fbm_fee, referral_percent, can_sell,
|
||||
sellability_status, sellability_reason,
|
||||
verdict, confidence, reasoning, fetched_at
|
||||
FROM product_analysis_results_legacy
|
||||
FROM ranked
|
||||
WHERE row_num = 1
|
||||
`);
|
||||
database.run("DROP TABLE product_analysis_results_legacy");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user