From bcaebf0494a44645e8f968d67e0a867280411581 Mon Sep 17 00:00:00 2001 From: Indigo Tang Date: Sun, 6 Jul 2025 13:37:24 +0000 Subject: [PATCH] delete current toyshare.db and regenerate example data --- src/lib/db.ts | 78 +++++++++++++++++++++--------------------------- toyshare.db-shm | Bin 32768 -> 32768 bytes toyshare.db-wal | Bin 90672 -> 259592 bytes 3 files changed, 34 insertions(+), 44 deletions(-) diff --git a/src/lib/db.ts b/src/lib/db.ts index 1274739..2373437 100644 --- a/src/lib/db.ts +++ b/src/lib/db.ts @@ -15,11 +15,17 @@ db.pragma('journal_mode = WAL'); db.pragma('foreign_keys = ON'); function initDb() { - console.log("Initializing database schema if needed..."); + console.log("Resetting database and re-seeding data..."); - // Create Users Table if it doesn't exist + // Drop existing tables to ensure a clean slate on every run. + // This is useful for development to easily reset the data. + db.exec('DROP TABLE IF EXISTS toys'); + db.exec('DROP TABLE IF EXISTS users'); + + + // Create Users Table db.exec(` - CREATE TABLE IF NOT EXISTS users ( + CREATE TABLE users ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, nickname TEXT, @@ -29,22 +35,10 @@ function initDb() { bio TEXT ); `); - - // Migration: Add nickname column to users table if it doesn't exist - try { - const columns = db.prepare("PRAGMA table_info(users)").all(); - if (!columns.some((col: any) => col.name === 'nickname')) { - console.log("Adding 'nickname' column to 'users' table..."); - db.exec('ALTER TABLE users ADD COLUMN nickname TEXT'); - } - } catch (error) { - console.error("Error during 'users' table migration:", error); - } - - // Create Toys Table if it doesn't exist + // Create Toys Table db.exec(` - CREATE TABLE IF NOT EXISTS toys ( + CREATE TABLE toys ( id TEXT PRIMARY KEY, name TEXT NOT NULL, description TEXT NOT NULL, @@ -58,46 +52,42 @@ function initDb() { ); `); - console.log("Seeding initial data if missing..."); + console.log("Seeding initial data..."); - // Use INSERT OR IGNORE to only add data if the primary key doesn't exist. - // This prevents errors on subsequent runs and adds missing users/toys without overwriting. + // Prepare insert statements const insertUser = db.prepare(` - INSERT OR IGNORE INTO users (id, name, nickname, email, role, avatarUrl, bio) + INSERT INTO users (id, name, nickname, email, role, avatarUrl, bio) VALUES (@id, @name, @nickname, @email, @role, @avatarUrl, @bio) `); const insertToy = db.prepare(` - INSERT OR IGNORE INTO toys (id, name, description, category, images, unavailableRanges, ownerId, pricePerDay, location) + INSERT INTO toys (id, name, description, category, images, unavailableRanges, ownerId, pricePerDay, location) VALUES (@id, @name, @description, @category, @images, @unavailableRanges, @ownerId, @pricePerDay, @location) `); - const insertManyUsers = db.transaction((users: User[]) => { - for (const user of users) { - insertUser.run({ - id: user.id, - name: user.name, - nickname: user.nickname ?? null, - email: user.email, - role: user.role ?? 'User', - avatarUrl: user.avatarUrl ?? null, - bio: user.bio ?? null - }); + // Use a transaction for efficiency + const seedData = db.transaction(() => { + for (const user of rawUsers) { + insertUser.run({ + id: user.id, + name: user.name, + nickname: user.nickname ?? null, + email: user.email, + role: user.role ?? 'User', + avatarUrl: user.avatarUrl ?? null, + bio: user.bio ?? null + }); } - }); - - const insertManyToys = db.transaction((toys) => { - for (const toy of toys) { - insertToy.run({ - ...toy, - images: JSON.stringify(toy.images), - unavailableRanges: JSON.stringify(toy.unavailableRanges), - }); + for (const toy of rawToys) { + insertToy.run({ + ...toy, + images: JSON.stringify(toy.images), + unavailableRanges: JSON.stringify(toy.unavailableRanges), + }); } }); - insertManyUsers(rawUsers); - insertManyToys(rawToys); + seedData(); console.log("Database initialization and seeding complete."); } diff --git a/toyshare.db-shm b/toyshare.db-shm index f791555dee24fde1c1accfcce48b50859d4ced65..8b4efd5736298dc815236e380a541638b791a72e 100644 GIT binary patch delta 366 zcmZo@U}|V!s+V}A%K!qxK+MR%AYc!q*?`!`K5?#`Y`9?P%e^ZuRdY1A_r0%D{8G$7 zs(PSNU|F(7zGRyG$CZa$nn8g$*?>4yoj*}-aE}8t#iEVQe;}53Ee_VNhj89Cg%s_XEZf<1! b$u#+gH$;q?je#2|@|%g7oq-3);M4;EyKzEW diff --git a/toyshare.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