← back to Kravet Sheet Sync 2026 04 20

delete_collision_pilots.py

55 lines

#!/usr/bin/env python3
"""Delete the 5 DWKK-collision pilot products + clean PG rows."""
import json, urllib.request, subprocess

TOKEN=os.environ.get('SHOPIFY_ADMIN_TOKEN','')
URL='https://designer-laboratory-sandbox.myshopify.com/admin/api/2024-10/graphql.json'

# Collision pilots — the NEW product IDs (keep the OLD existing ones untouched)
COLLISIONS = [
    {"dw_sku": "DWKK-140010", "pid": "gid://shopify/Product/7821709836339"},
    {"dw_sku": "DWKK-140056", "pid": "gid://shopify/Product/7821709869107"},
    {"dw_sku": "DWKK-140544", "pid": "gid://shopify/Product/7821709901875"},
    {"dw_sku": "DWKK-140783", "pid": "gid://shopify/Product/7821709934643"},
    {"dw_sku": "DWKK-142909", "pid": "gid://shopify/Product/7821710032947"},
]

DEL = """mutation($in: ProductDeleteInput!){ productDelete(input:$in){ deletedProductId userErrors{field message} } }"""

def gql(q,v=None):
    body=json.dumps({"query":q,"variables":v or {}}).encode()
    r=urllib.request.urlopen(urllib.request.Request(URL,data=body,method="POST",
      headers={"Content-Type":"application/json","X-Shopify-Access-Token":TOKEN}),timeout=30)
    return json.loads(r.read())

for c in COLLISIONS:
    # 1) Delete on Shopify
    d = gql(DEL, {"in": {"id": c["pid"]}})
    errs = d.get("data", {}).get("productDelete", {}).get("userErrors", [])
    del_id = d.get("data", {}).get("productDelete", {}).get("deletedProductId")
    if errs:
        print(f"  {c['dw_sku']}: delete errors: {errs}")
    else:
        print(f"  {c['dw_sku']}: deleted {del_id}")

    # 2) Clean PG (but only the rows I INSERTED — match by the NEW shopify_id numeric)
    pid_num = c["pid"].split("/")[-1]
    dwsku = c["dw_sku"]
    for q in [
        # shopify_products row I inserted (not the pre-existing one; match by numeric shopify_id)
        f"DELETE FROM shopify_products WHERE shopify_id = '{pid_num}' AND dw_sku = '{dwsku}'",
        # kravet_catalog row I inserted (match by full gid I stored)
        f"DELETE FROM kravet_catalog WHERE shopify_product_id = '{c['pid']}' AND dw_sku = '{dwsku}'",
        # dw_sku_registry (my insert — may or may not have existed before, delete if mfr_sku is my new one)
    ]:
        subprocess.run(["ssh","root@45.61.58.125",
          f'PGPASSWORD=DW2024! psql -h 127.0.0.1 -U dw_admin -d dw_unified -c "{q}"'],
          capture_output=True, text=True, check=True)
    # Registry: preserve if it existed before; only delete if created today
    subprocess.run(["ssh","root@45.61.58.125",
        f'PGPASSWORD=DW2024! psql -h 127.0.0.1 -U dw_admin -d dw_unified -c '
        f'"DELETE FROM dw_sku_registry WHERE dw_sku = \'{dwsku}\' AND created_at > NOW() - INTERVAL \'1 day\'"'],
        capture_output=True, text=True, check=True)

print("done.")