From 3ae3458dfc3c5ba1f1229cf3a3d5af3a32e3fc19 Mon Sep 17 00:00:00 2001 From: jason Date: Mon, 9 Mar 2026 01:59:52 -0500 Subject: [PATCH] Clean: Fresh database init with parents table - no migrations --- server/db/init.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/server/db/init.js b/server/db/init.js index b6aefb6..7b74afc 100644 --- a/server/db/init.js +++ b/server/db/init.js @@ -16,7 +16,7 @@ function initDatabase(dbPath) { console.log('Initializing database schema...'); - // Dogs table - Core registry + // Dogs table - NO sire/dam columns, only litter_id db.exec(` CREATE TABLE IF NOT EXISTS dogs ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -29,9 +29,11 @@ function initDatabase(dbPath) { microchip TEXT, photo_urls TEXT, -- JSON array of photo URLs notes TEXT, + litter_id INTEGER, is_active INTEGER DEFAULT 1, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, - updated_at DATETIME DEFAULT CURRENT_TIMESTAMP + updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (litter_id) REFERENCES litters(id) ON DELETE SET NULL ) `); @@ -42,7 +44,7 @@ function initDatabase(dbPath) { WHERE microchip IS NOT NULL `); - // Parents table - Relationship mapping + // Parents table - Stores sire/dam relationships db.exec(` CREATE TABLE IF NOT EXISTS parents ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -51,7 +53,7 @@ function initDatabase(dbPath) { parent_type TEXT NOT NULL CHECK(parent_type IN ('sire', 'dam')), FOREIGN KEY (dog_id) REFERENCES dogs(id) ON DELETE CASCADE, FOREIGN KEY (parent_id) REFERENCES dogs(id) ON DELETE CASCADE, - UNIQUE(dog_id, parent_id, parent_type) + UNIQUE(dog_id, parent_type) ) `); @@ -122,6 +124,7 @@ function initDatabase(dbPath) { db.exec(` CREATE INDEX IF NOT EXISTS idx_dogs_name ON dogs(name); CREATE INDEX IF NOT EXISTS idx_dogs_registration ON dogs(registration_number); + CREATE INDEX IF NOT EXISTS idx_dogs_litter ON dogs(litter_id); CREATE INDEX IF NOT EXISTS idx_parents_dog ON parents(dog_id); CREATE INDEX IF NOT EXISTS idx_parents_parent ON parents(parent_id); CREATE INDEX IF NOT EXISTS idx_litters_sire ON litters(sire_id); @@ -141,7 +144,10 @@ function initDatabase(dbPath) { END; `); - console.log('Database schema initialized successfully!'); + console.log('✓ Database schema initialized successfully!'); + console.log('✓ Dogs table: NO sire/dam columns, uses parents table'); + console.log('✓ Parents table: Stores sire/dam relationships'); + console.log('✓ Litters table: Links puppies via litter_id'); db.close(); return true; @@ -159,5 +165,11 @@ module.exports = { initDatabase, getDatabase }; // Run initialization if called directly if (require.main === module) { const dbPath = process.env.DB_PATH || path.join(__dirname, '../../data/breedr.db'); + console.log('\n=========================================='); + console.log('BREEDR Database Initialization'); + console.log('=========================================='); + console.log(`Database: ${dbPath}`); + console.log('==========================================\n'); initDatabase(dbPath); -} \ No newline at end of file + console.log('\n✓ Database ready!\n'); +}