From f53a136e75d71f41d8b1e3e0d2d223ebb8677cfe Mon Sep 17 00:00:00 2001 From: carmennt Date: Mon, 9 Nov 2026 15:05:45 +0100 Subject: [PATCH 1/6] fix: moving FillGenres to FillDB --- Database/FillDb.ts | 33 ++++++++++++++++++++++---- Database/FillGenres.ts | 54 ------------------------------------------ package.json | 4 +--- 3 files changed, 30 insertions(+), 61 deletions(-) delete mode 100644 Database/FillGenres.ts diff --git a/Database/FillDb.ts b/Database/FillDb.ts index bd181f9..7fb9429 100644 --- a/Database/FillDb.ts +++ b/Database/FillDb.ts @@ -7,13 +7,14 @@ const FILE = path.join(process.cwd(), 'Database', 'games.json') const CHUNK = 500 // games per INSERT statement async function FillDb() { + const client = await pool.connect() try { const games = JSON.parse(await readFile(FILE, 'utf8')) for (let i = 0; i < games.length; i += CHUNK) { - const chunk = games.slice(i, i + CHUNK) + const json = JSON.stringify(games.slice(i, i + CHUNK)) - await pool.query( + await client.query( `INSERT INTO games (game_id, game_name, current_price, initial_price, discount_percent, image_url, description) SELECT appid, @@ -33,16 +34,40 @@ async function FillDb() { discount_percent = EXCLUDED.discount_percent, image_url = EXCLUDED.image_url, description = EXCLUDED.description`, - [JSON.stringify(chunk)] + [json], ) + + await client.query( + `INSERT INTO genres (genre_name) + SELECT DISTINCT btrim(x) + FROM jsonb_to_recordset($1::jsonb) AS g(genre text), + unnest(string_to_array(g.genre, ',')) AS x + WHERE btrim(x) <> '' + ON CONFLICT (genre_name) DO NOTHING`, + [json], + ) + + await client.query( + `INSERT INTO genretogame (genre_id, game_id) + SELECT DISTINCT ge.genre_id, g.appid + FROM jsonb_to_recordset($1::jsonb) AS g(appid int, genre text) + CROSS JOIN LATERAL unnest(string_to_array(g.genre, ',')) AS x + JOIN genres ge ON ge.genre_name = btrim(x) + ON CONFLICT DO NOTHING`, + [json], + ) + + await client.query('COMMIT') console.log(`Inserted/updated ${Math.min(i + CHUNK, games.length)} of ${games.length}`) } } catch (error) { + await client.query('ROLLBACK') console.log(error) process.exitCode = 1 } finally { + client.release() await pool.end() } } -FillDb() +FillDb() \ No newline at end of file diff --git a/Database/FillGenres.ts b/Database/FillGenres.ts deleted file mode 100644 index 2c01337..0000000 --- a/Database/FillGenres.ts +++ /dev/null @@ -1,54 +0,0 @@ -import 'dotenv/config' -import { pool } from '../Backend/DatabaseConnection.ts' - -const FILL_GENRES = ` -INSERT INTO genres (genre_name) -SELECT DISTINCT btrim(g) -FROM games, unnest(string_to_array(genre, ',')) AS g -WHERE genre IS NOT NULL AND btrim(g) <> '' -ON CONFLICT (genre_name) DO NOTHING; - -INSERT INTO genretogame (genre_id, game_id) -SELECT DISTINCT ge.genre_id, ga.game_id -FROM games ga -CROSS JOIN LATERAL unnest(string_to_array(ga.genre, ',')) AS g -JOIN genres ge ON ge.genre_name = btrim(g) -WHERE ga.genre IS NOT NULL -ON CONFLICT DO NOTHING; -` - -async function FillGenres() { - const client = await pool.connect() - try { - await client.query('BEGIN') - await client.query(FILL_GENRES) - - const { rows } = await client.query( - `SELECT COUNT(*) AS missing - FROM games g - WHERE btrim(coalesce(g.genre, '')) <> '' - AND NOT EXISTS (SELECT 1 FROM genretogame gg WHERE gg.game_id = g.game_id)` - ) - if (Number(rows[0].missing) > 0) { - throw new Error(`${rows[0].missing} games would lose their genres, aborting`) - } - - await client.query('ALTER TABLE games DROP COLUMN genre') - await client.query('COMMIT') - - const genres = await pool.query('SELECT COUNT(*) AS count FROM genres') - const links = await pool.query('SELECT COUNT(*) AS count FROM genretogame') - console.log( - `Genres: ${genres.rows[0].count}, links: ${links.rows[0].count}. games.genre dropped.` - ) - } catch (error) { - await client.query('ROLLBACK') - console.log(error) - process.exitCode = 1 - } finally { - client.release() - await pool.end() - } -} - -FillGenres() diff --git a/package.json b/package.json index 066c2a0..16d3e32 100644 --- a/package.json +++ b/package.json @@ -16,11 +16,9 @@ "server": "node Backend/Server.ts", "db:create": "node Database/CreateTables.ts", "db:fill": "node Database/FillDb.ts", - "db:fill:genres":"node Database/FillGenres.ts", "server:local": "node --env-file=Frontend/.env.development Backend/Server.ts", "db:create:local": "node --env-file=Frontend/.env.development Database/CreateTables.ts", - "db:fill:local": "node --env-file=Frontend/.env.development Database/FillDb.ts", - "db:fill:genres:local":"node --env-file=Frontend/.env.development Database/FillGenres.ts" + "db:fill:local": "node --env-file=Frontend/.env.development Database/FillDb.ts" }, "dependencies": { "@apollo/server": "^5.5.1", From eac8eef7e9c3c2dbb6062eaf6590c83f0c93e58b Mon Sep 17 00:00:00 2001 From: carmennt Date: Tue, 10 Nov 2026 10:37:29 +0100 Subject: [PATCH 2/6] fix: Bring back games.genre --- Database/CreateTables.ts | 1 + Database/FillDb.ts | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Database/CreateTables.ts b/Database/CreateTables.ts index 894aa72..7737f3b 100644 --- a/Database/CreateTables.ts +++ b/Database/CreateTables.ts @@ -9,6 +9,7 @@ CREATE TABLE IF NOT EXISTS games ( initial_price numeric(10,2), discount_percent int, image_url varchar(255), + genre varchar(255), description text, CONSTRAINT games_not_hidden CHECK (game_id <> 999999) ); diff --git a/Database/FillDb.ts b/Database/FillDb.ts index bd181f9..8e65dbc 100644 --- a/Database/FillDb.ts +++ b/Database/FillDb.ts @@ -15,23 +15,25 @@ async function FillDb() { await pool.query( `INSERT INTO games (game_id, game_name, current_price, initial_price, - discount_percent, image_url, description) + discount_percent, image_url, genre, description) SELECT appid, name, CASE WHEN price = 'Free' THEN 0 ELSE price::numeric END, initialprice::numeric, discount::int, img, + genre, description FROM jsonb_to_recordset($1::jsonb) AS g(appid int, name text, price text, initialprice text, - discount text, img text, description text) + discount text, img text, genre text, description text) ON CONFLICT (game_id) DO UPDATE SET game_name = EXCLUDED.game_name, current_price = EXCLUDED.current_price, initial_price = EXCLUDED.initial_price, discount_percent = EXCLUDED.discount_percent, image_url = EXCLUDED.image_url, + genre = EXCLUDED.genre, description = EXCLUDED.description`, [JSON.stringify(chunk)] ) From 52f9243525a06c1b7c25d848768a0f3f0301a4de Mon Sep 17 00:00:00 2001 From: carmennt Date: Tue, 10 Nov 2026 10:38:59 +0100 Subject: [PATCH 3/6] fix: Bring back games.genre --- Database/CreateTables.ts | 1 + Database/FillDb.ts | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Database/CreateTables.ts b/Database/CreateTables.ts index 894aa72..7737f3b 100644 --- a/Database/CreateTables.ts +++ b/Database/CreateTables.ts @@ -9,6 +9,7 @@ CREATE TABLE IF NOT EXISTS games ( initial_price numeric(10,2), discount_percent int, image_url varchar(255), + genre varchar(255), description text, CONSTRAINT games_not_hidden CHECK (game_id <> 999999) ); diff --git a/Database/FillDb.ts b/Database/FillDb.ts index bd181f9..8e65dbc 100644 --- a/Database/FillDb.ts +++ b/Database/FillDb.ts @@ -15,23 +15,25 @@ async function FillDb() { await pool.query( `INSERT INTO games (game_id, game_name, current_price, initial_price, - discount_percent, image_url, description) + discount_percent, image_url, genre, description) SELECT appid, name, CASE WHEN price = 'Free' THEN 0 ELSE price::numeric END, initialprice::numeric, discount::int, img, + genre, description FROM jsonb_to_recordset($1::jsonb) AS g(appid int, name text, price text, initialprice text, - discount text, img text, description text) + discount text, img text, genre text, description text) ON CONFLICT (game_id) DO UPDATE SET game_name = EXCLUDED.game_name, current_price = EXCLUDED.current_price, initial_price = EXCLUDED.initial_price, discount_percent = EXCLUDED.discount_percent, image_url = EXCLUDED.image_url, + genre = EXCLUDED.genre, description = EXCLUDED.description`, [JSON.stringify(chunk)] ) From a3c1647bb26f87b430551525d013ae39e0b87d46 Mon Sep 17 00:00:00 2001 From: carmennt Date: Tue, 10 Nov 2026 10:50:06 +0100 Subject: [PATCH 4/6] fix: Bring back FillGenres --- Database/FillDb.ts | 33 ++++---------------------- Database/FillGenres.ts | 54 ++++++++++++++++++++++++++++++++++++++++++ package.json | 4 +++- 3 files changed, 61 insertions(+), 30 deletions(-) create mode 100644 Database/FillGenres.ts diff --git a/Database/FillDb.ts b/Database/FillDb.ts index ef7ff24..8e65dbc 100644 --- a/Database/FillDb.ts +++ b/Database/FillDb.ts @@ -7,14 +7,13 @@ const FILE = path.join(process.cwd(), 'Database', 'games.json') const CHUNK = 500 // games per INSERT statement async function FillDb() { - const client = await pool.connect() try { const games = JSON.parse(await readFile(FILE, 'utf8')) for (let i = 0; i < games.length; i += CHUNK) { - const json = JSON.stringify(games.slice(i, i + CHUNK)) + const chunk = games.slice(i, i + CHUNK) - await client.query( + await pool.query( `INSERT INTO games (game_id, game_name, current_price, initial_price, discount_percent, image_url, genre, description) SELECT appid, @@ -36,40 +35,16 @@ async function FillDb() { image_url = EXCLUDED.image_url, genre = EXCLUDED.genre, description = EXCLUDED.description`, - [json], + [JSON.stringify(chunk)] ) - - await client.query( - `INSERT INTO genres (genre_name) - SELECT DISTINCT btrim(x) - FROM jsonb_to_recordset($1::jsonb) AS g(genre text), - unnest(string_to_array(g.genre, ',')) AS x - WHERE btrim(x) <> '' - ON CONFLICT (genre_name) DO NOTHING`, - [json], - ) - - await client.query( - `INSERT INTO genretogame (genre_id, game_id) - SELECT DISTINCT ge.genre_id, g.appid - FROM jsonb_to_recordset($1::jsonb) AS g(appid int, genre text) - CROSS JOIN LATERAL unnest(string_to_array(g.genre, ',')) AS x - JOIN genres ge ON ge.genre_name = btrim(x) - ON CONFLICT DO NOTHING`, - [json], - ) - - await client.query('COMMIT') console.log(`Inserted/updated ${Math.min(i + CHUNK, games.length)} of ${games.length}`) } } catch (error) { - await client.query('ROLLBACK') console.log(error) process.exitCode = 1 } finally { - client.release() await pool.end() } } -FillDb() \ No newline at end of file +FillDb() diff --git a/Database/FillGenres.ts b/Database/FillGenres.ts new file mode 100644 index 0000000..2c01337 --- /dev/null +++ b/Database/FillGenres.ts @@ -0,0 +1,54 @@ +import 'dotenv/config' +import { pool } from '../Backend/DatabaseConnection.ts' + +const FILL_GENRES = ` +INSERT INTO genres (genre_name) +SELECT DISTINCT btrim(g) +FROM games, unnest(string_to_array(genre, ',')) AS g +WHERE genre IS NOT NULL AND btrim(g) <> '' +ON CONFLICT (genre_name) DO NOTHING; + +INSERT INTO genretogame (genre_id, game_id) +SELECT DISTINCT ge.genre_id, ga.game_id +FROM games ga +CROSS JOIN LATERAL unnest(string_to_array(ga.genre, ',')) AS g +JOIN genres ge ON ge.genre_name = btrim(g) +WHERE ga.genre IS NOT NULL +ON CONFLICT DO NOTHING; +` + +async function FillGenres() { + const client = await pool.connect() + try { + await client.query('BEGIN') + await client.query(FILL_GENRES) + + const { rows } = await client.query( + `SELECT COUNT(*) AS missing + FROM games g + WHERE btrim(coalesce(g.genre, '')) <> '' + AND NOT EXISTS (SELECT 1 FROM genretogame gg WHERE gg.game_id = g.game_id)` + ) + if (Number(rows[0].missing) > 0) { + throw new Error(`${rows[0].missing} games would lose their genres, aborting`) + } + + await client.query('ALTER TABLE games DROP COLUMN genre') + await client.query('COMMIT') + + const genres = await pool.query('SELECT COUNT(*) AS count FROM genres') + const links = await pool.query('SELECT COUNT(*) AS count FROM genretogame') + console.log( + `Genres: ${genres.rows[0].count}, links: ${links.rows[0].count}. games.genre dropped.` + ) + } catch (error) { + await client.query('ROLLBACK') + console.log(error) + process.exitCode = 1 + } finally { + client.release() + await pool.end() + } +} + +FillGenres() diff --git a/package.json b/package.json index 16d3e32..71f5c30 100644 --- a/package.json +++ b/package.json @@ -16,9 +16,11 @@ "server": "node Backend/Server.ts", "db:create": "node Database/CreateTables.ts", "db:fill": "node Database/FillDb.ts", + "db:fill:genres": "node Database/FillGenres.ts", "server:local": "node --env-file=Frontend/.env.development Backend/Server.ts", "db:create:local": "node --env-file=Frontend/.env.development Database/CreateTables.ts", - "db:fill:local": "node --env-file=Frontend/.env.development Database/FillDb.ts" + "db:fill:local": "node --env-file=Frontend/.env.development Database/FillDb.ts", + "db:fill:genres:local": "node --env-file=Frontend/.env.development Database/FillGenres.ts" }, "dependencies": { "@apollo/server": "^5.5.1", From 94e34f3c05fdb6de3223e2719840ab5008b62ada Mon Sep 17 00:00:00 2001 From: carmennt Date: Tue, 10 Nov 2026 11:20:27 +0100 Subject: [PATCH 5/6] fix: Adding ALTER TABLE for games.genre --- Database/CreateTables.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Database/CreateTables.ts b/Database/CreateTables.ts index 7737f3b..3a003c1 100644 --- a/Database/CreateTables.ts +++ b/Database/CreateTables.ts @@ -68,6 +68,9 @@ ALTER TABLE users ADD COLUMN IF NOT EXISTS description text, ADD COLUMN IF NOT EXISTS favourite_game int REFERENCES games (game_id) ON DELETE SET NULL, ADD COLUMN IF NOT EXISTS setup varchar(255); + +ALTER TABLE games + ADD COLUMN IF NOT EXISTS genre varchar(255),; ` async function createTables() { From 2d58c24cc2c9b20b05e8f1829afa0824d494577b Mon Sep 17 00:00:00 2001 From: carmennt Date: Tue, 10 Nov 2026 11:24:25 +0100 Subject: [PATCH 6/6] fix: ; removed --- Database/CreateTables.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Database/CreateTables.ts b/Database/CreateTables.ts index 3a003c1..cd7346a 100644 --- a/Database/CreateTables.ts +++ b/Database/CreateTables.ts @@ -70,7 +70,7 @@ ALTER TABLE users ADD COLUMN IF NOT EXISTS setup varchar(255); ALTER TABLE games - ADD COLUMN IF NOT EXISTS genre varchar(255),; + ADD COLUMN IF NOT EXISTS genre varchar(255); ` async function createTables() {