import 'dotenv/config'; import { db, ClosePool } from '../src/db/index.ts'; import { sql } from 'drizzle-orm' async function syncVariants() { const updates = await db.execute(sql`update cards as c set product_name = a.product_name, product_line_name = a.product_line_name, product_url_name = a.product_url_name, rarity_name = a.rarity_name, sealed = a.sealed, set_id = a.set_id, card_type = a.card_type, energy_type = a.energy_type, number = a.number, artist = a.artist from ( select t.product_id, b.variant, coalesce(o.product_name, regexp_replace(regexp_replace(coalesce(nullif(t.product_name, ''), t.product_url_name),' \\\\(.*\\\\)',''),' - .*$','')) as product_name, coalesce(o.product_line_name, t.product_line_name) as product_line_name, coalesce(o.product_url_name, t.product_url_name) as product_url_name, coalesce(o.rarity_name, t.rarity_name) as rarity_name, coalesce(o.sealed, t.sealed) as sealed, coalesce(o.set_id, t.set_id) as set_id, coalesce(o.card_type, t.card_type) as card_type, coalesce(o.energy_type, t.energy_type) as energy_type, coalesce(o.number, t.number) as number, coalesce(o.artist, t.artist) as artist from tcg_cards t join (select distinct product_id, variant from skus) b on t.product_id = b.product_id left join tcg_overrides o on t.product_id = o.product_id ) a where c.product_id = a.product_id and c.variant = a.variant and ( c.product_name is distinct from a.product_name or c.product_line_name is distinct from a.product_line_name or c.product_url_name is distinct from a.product_url_name or c.rarity_name is distinct from a.rarity_name or c.sealed is distinct from a.sealed or c.set_id is distinct from a.set_id or c.card_type is distinct from a.card_type or c.energy_type is distinct from a.energy_type or c."number" is distinct from a."number" or c.artist is distinct from a.artist ) `); console.log(`Updated ${updates.rowCount} rows in cards table`); const inserts = await db.execute(sql`insert into cards (product_id, variant, product_name, product_line_name, product_url_name, rarity_name, sealed, set_id, card_type, energy_type, "number", artist) select t.product_id, b.variant, coalesce(o.product_name, regexp_replace(regexp_replace(coalesce(nullif(t.product_name, ''), t.product_url_name),' \\\\(.*\\\\)',''),' - .*$','')) as product_name, coalesce(o.product_line_name, t.product_line_name) as product_line_name, coalesce(o.product_url_name, t.product_url_name) as product_url_name, coalesce(o.rarity_name, t.rarity_name) as rarity_name, coalesce(o.sealed, t.sealed) as sealed, coalesce(o.set_id, t.set_id) as set_id, coalesce(o.card_type, t.card_type) as card_type, coalesce(o.energy_type, t.energy_type) as energy_type, coalesce(o.number, t.number) as number, coalesce(o.artist, t.artist) as artist from tcg_cards t join (select distinct product_id, variant from skus) b on t.product_id = b.product_id left join tcg_overrides o on t.product_id = o.product_id where not exists (select 1 from cards where product_id=t.product_id and variant=b.variant) `); console.log(`Inserted ${inserts.rowCount} rows into cards table`); } await syncVariants(); await ClosePool();