diff --git a/scripts/sync-variants.ts b/scripts/sync-variants.ts new file mode 100644 index 0000000..53c3fbf --- /dev/null +++ b/scripts/sync-variants.ts @@ -0,0 +1,32 @@ +import 'dotenv/config'; +import { db, poolConnection } from '../src/db/index.ts'; +import { sql } from 'drizzle-orm' + +async function syncVariants() { + const updates = await db.execute(sql`update cards as c +join tcgcards t on c.productId = t.productId +join (select distinct productId, variant from skus) b on c.productId = b.productId and c.variant = b.variant +left join tcg_overrides o on c.productId = o.productId +set c.productName = coalesce(o.productName, regexp_replace(regexp_replace(regexp_replace(coalesce(nullif(t.productName, ''), t.productUrlName), ' \\[.*\\]', ''),' \\(.*\\)',''),' - .*$','')), +c.productLineName = coalesce(o.productLineName, t.productLineName), c.productUrlName = coalesce(o.productUrlName, t.productUrlName), c.rarityName = coalesce(o.rarityName, t.rarityName), +c.sealed = coalesce(o.sealed, t.sealed), c.setId = coalesce(o.setId, t.setId), c.cardType = coalesce(o.cardType, t.cardType), +c.energyType = coalesce(o.energyType, t.energyType), c.number = coalesce(o.number, t.number), c.Artist = coalesce(o.Artist, t.Artist)`); + console.log(`Updated ${updates[0].affectedRows} rows in cards table`); + + const inserts = await db.execute(sql`insert into cards (productId, variant, productName, productLineName, productUrlName, rarityName, sealed, setId, cardType, energyType, number, Artist) +select t.productId, b.variant, +coalesce(o.productName, regexp_replace(regexp_replace(regexp_replace(coalesce(nullif(t.productName, ''), t.productUrlName), ' \\[.*\\]', ''),' \\(.*\\)',''),' - .*$','')) as productName, +coalesce(o.productLineName, t.productLineName) as productLineName, coalesce(o.productUrlName, t.productUrlName) as productUrlName, coalesce(o.rarityName, t.rarityName) as rarityName, +coalesce(o.sealed, t.sealed) as sealed, coalesce(o.setId, t.setId) as setId, coalesce(o.cardType, t.cardType) as cardType, +coalesce(o.energyType, t.energyType) as energyType, coalesce(o.number, t.number) as number, coalesce(o.Artist, t.Artist) as Artist +from tcgcards t +join (select distinct productId, variant from skus) b on t.productId = b.productId +left join tcg_overrides o on t.productId = o.productId +where not exists (select 1 from cards where productId=t.productId and variant=b.variant) +`); + console.log(`Inserted ${inserts[0].affectedRows} rows into cards table`); + +} + +await syncVariants(); +await poolConnection.end(); diff --git a/src/db/index.ts b/src/db/index.ts index b787475..6b83b4f 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -4,7 +4,8 @@ import { relations } from './relations.ts'; import { drizzle } from 'drizzle-orm/mysql2'; import mysql from 'mysql2/promise'; -export const poolConnection = mysql.createPool(process.env.DATABASE_URL!); +//export const poolConnection = mysql.createPool({ uri: process.env.DATABASE_URL, client_found_rows: false }); +export const poolConnection = mysql.createPool({ uri: process.env.DATABASE_URL, flags: ["-FOUND_ROWS"] }); export const db = drizzle({ client: poolConnection, relations: relations});