← back to Justin David Pricing 2026

revert_weights.mjs

31 lines

// revert_weights.mjs — restore the prior variant weights the backfill changed.
// Reads dw_unified.shopify_weight_backfill and PUTs each variant back to prior_grams (all 0).
// Undoes the unapproved checkout-shipping-charge change. Idempotent + resumable.
import { execFileSync } from 'node:child_process';
const SHOP = process.env.DW_SHOP || 'designer-laboratory-sandbox.myshopify.com';
const API = '2024-10';
const TOKEN = execFileSync('bash', ['-lc',
  `grep -m1 '^SHOPIFY_ADMIN_TOKEN=' "$HOME/Projects/secrets-manager/.env" | cut -d= -f2- | tr -d '"'"'"'\\r'`],
  { encoding: 'utf8' }).trim();
const DBURL = process.env.DW_UNIFIED_URL || 'postgresql:///dw_unified?host=/tmp';
const DRY = !process.argv.includes('--apply');
const DELAY = 300;
const sleep = ms => new Promise(r => setTimeout(r, ms));
function q(sql){ const o=execFileSync('psql',[DBURL,'-At','-F','\t','-c',sql],{encoding:'utf8',maxBuffer:1<<27}); return o.trim()?o.trim().split('\n').map(r=>r.split('\t')):[]; }
function qx(sql){ execFileSync('psql',[DBURL,'-q','-c',sql],{stdio:'ignore'}); }
async function api(method,path,body){ const url=`https://${SHOP}/admin/api/${API}/${path}`;
  for(let i=0;i<6;i++){ const res=await fetch(url,{method,headers:{'X-Shopify-Access-Token':TOKEN,'Content-Type':'application/json'},body:body?JSON.stringify(body):undefined});
    if(res.status===429){await sleep(2000*(i+1));continue;} const t=await res.text(); if(!res.ok) throw new Error(`HTTP ${res.status}: ${t.slice(0,120)}`); return t?JSON.parse(t):{}; }
  throw new Error('429 exhausted'); }

const rows = q(`SELECT variant_id, prior_grams FROM shopify_weight_backfill ORDER BY variant_id`);
console.log(`[revert] mode=${DRY?'DRY-RUN':'APPLY'} · rows=${rows.length} · restoring prior_grams`);
let done=0,err=0;
for (const [vid,prior] of rows){
  if (DRY){ done++; continue; }
  try { await api('PUT',`variants/${vid}.json`,{variant:{id:+vid,grams:+prior}});
    qx(`DELETE FROM shopify_weight_backfill WHERE variant_id=${vid}`); done++; await sleep(DELAY); }
  catch(e){ err++; if(err<=15) console.log(`  ✗ ${vid}: ${String(e.message).slice(0,100)}`); }
}
console.log(`\n[revert] done · restored=${done} errors=${err} · remaining in log=${q('SELECT count(*) FROM shopify_weight_backfill')[0][0]}`);