[object Object]

← back to Crezana Scraper

Auto-assign DWCZ- series SKU to new rows on scrape (idempotent, continues from max)

f721c896be5aa1fc4a34e52e5f01df5d2005549c · 2026-07-14 07:56:07 -0700 · Steve Abrams

Files touched

Diff

commit f721c896be5aa1fc4a34e52e5f01df5d2005549c
Author: Steve Abrams <steve@designerwallcoverings.com>
Date:   Tue Jul 14 07:56:07 2026 -0700

    Auto-assign DWCZ- series SKU to new rows on scrape (idempotent, continues from max)
---
 scrape.mjs | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/scrape.mjs b/scrape.mjs
index c67b338..0b23c19 100644
--- a/scrape.mjs
+++ b/scrape.mjs
@@ -135,6 +135,25 @@ async function main() {
     );
     if (r.rows[0].inserted) ins++; else upd++;
   }
+
+  // Assign DW series SKUs (DWCZ-) to any rows still missing one — continue from the
+  // current max so the series never regresses to NULL and never collides. Idempotent:
+  // a normal re-run touches 0 rows since every existing row already carries a dw_sku.
+  const dw = await client.query(`
+    WITH mx AS (
+      SELECT COALESCE(MAX((regexp_replace(dw_sku,'^DWCZ-',''))::int), 890999) AS n
+      FROM crezana_catalog WHERE dw_sku ~ '^DWCZ-[0-9]+$'
+    ),
+    todo AS (
+      SELECT id, (SELECT n FROM mx) + row_number() OVER (ORDER BY id) AS seq
+      FROM crezana_catalog WHERE dw_sku IS NULL
+    )
+    UPDATE crezana_catalog c
+    SET dw_sku = 'DWCZ-' || todo.seq, updated_at = now()
+    FROM todo WHERE c.id = todo.id
+    RETURNING c.id`);
+  if (dw.rowCount) console.log(`Assigned ${dw.rowCount} new DWCZ- series SKU(s).`);
+
   await client.end();
   console.log(`Upserted: ${ins} inserted, ${upd} updated. Cost: $0 (local)`);
 }

← 078064e initial scaffold: crezana-scraper (Crezana onboard)  ·  back to Crezana Scraper  ·  Auto-assign collection-encoded DWCZ-<coll>-NNN to new rows ( 9917ba6 →