Skip to content

Commit 48c3dc0

Browse files
committed
Adds Item Rarity and Update Changes to Postgres DB
Ensures the inspect link is newer if overwriting, fixes up unique item index
1 parent 2b777e9 commit 48c3dc0

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

lib/postgres.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@ class Postgres {
4646
props integer NOT NULL,
4747
stickers jsonb,
4848
updated timestamp NOT NULL,
49-
PRIMARY KEY (a, ms, d)
49+
rarity smallint NOT NULL,
50+
PRIMARY KEY (a)
5051
)`);
5152

5253
await this.pool.query(`CREATE INDEX IF NOT EXISTS i_stickers ON items USING gin (stickers jsonb_path_ops)
5354
WHERE stickers IS NOT NULL`);
5455
await this.pool.query(`CREATE INDEX IF NOT EXISTS i_paintwear ON items (paintwear)`);
5556
await this.pool.query(`CREATE UNIQUE INDEX IF NOT EXISTS i_unique_item ON
56-
items (defindex, paintindex, paintseed, paintwear)`);
57+
items (defindex, paintindex, paintwear, paintseed)`);
5758
}
5859

5960
async insertItemData(item) {
@@ -104,12 +105,12 @@ class Postgres {
104105

105106
// We define unique items as those that have the same skin, wear, and paint seed
106107
// Duped items will be represented as one item in this case
107-
// If the item already exists, update it's link properties and stickers, these are the only attributes
108-
// that can change
109-
await this.pool.query(`INSERT INTO items VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11::jsonb, now())
110-
ON CONFLICT (paintwear, defindex, paintindex, paintseed) DO UPDATE SET ms=$12, a=$13, d=$14, stickers=$15`,
108+
// If the item already exists, update it's link properties and stickers only if it is more recent
109+
// (higher item id)
110+
await this.pool.query(`INSERT INTO items VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11::jsonb, now(), $12)
111+
ON CONFLICT (defindex, paintindex, paintwear, paintseed) DO UPDATE SET ms=$13, a=$14, d=$15, stickers=$16, updated=now() WHERE items.a < excluded.a`,
111112
[sm, item.a, item.d, item.paintseed, item.paintwear, item.defindex, item.paintindex, isStattrak,
112-
isSouvenir, props, JSON.stringify(stickers), sm, item.a, item.d, JSON.stringify(stickers)]);
113+
isSouvenir, props, JSON.stringify(stickers), item.rarity, sm, item.a, item.d, JSON.stringify(stickers)]);
113114
} catch (e) {
114115
winston.warn(e);
115116
}

0 commit comments

Comments
 (0)