From a246e5f84f283e5e2914b44196dc8878652ff281 Mon Sep 17 00:00:00 2001 From: jason Date: Mon, 9 Mar 2026 00:06:41 -0500 Subject: [PATCH] Add migration to add litter_id column to dogs table --- server/db/migrate_litter_id.js | 52 ++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 server/db/migrate_litter_id.js diff --git a/server/db/migrate_litter_id.js b/server/db/migrate_litter_id.js new file mode 100644 index 0000000..0fa4dcf --- /dev/null +++ b/server/db/migrate_litter_id.js @@ -0,0 +1,52 @@ +const Database = require('better-sqlite3'); +const path = require('path'); + +function migrateLitterId(dbPath) { + console.log('Running litter_id migration...'); + + const db = new Database(dbPath); + db.pragma('foreign_keys = ON'); + + try { + // Check if litter_id column already exists + const tableInfo = db.prepare("PRAGMA table_info(dogs)").all(); + const hasLitterId = tableInfo.some(col => col.name === 'litter_id'); + + if (hasLitterId) { + console.log('litter_id column already exists. Skipping migration.'); + db.close(); + return; + } + + // Add litter_id column to dogs table + db.exec(` + ALTER TABLE dogs ADD COLUMN litter_id INTEGER; + `); + + // Create index for litter_id + db.exec(` + CREATE INDEX IF NOT EXISTS idx_dogs_litter ON dogs(litter_id); + `); + + // Add foreign key relationship (SQLite doesn't support ALTER TABLE ADD CONSTRAINT) + // So we'll rely on application-level constraint checking + + console.log('✓ Added litter_id column to dogs table'); + console.log('✓ Created index on litter_id'); + console.log('Migration completed successfully!'); + + db.close(); + } catch (error) { + console.error('Migration failed:', error.message); + db.close(); + throw error; + } +} + +module.exports = { migrateLitterId }; + +// Run migration if called directly +if (require.main === module) { + const dbPath = process.env.DB_PATH || path.join(__dirname, '../../data/breedr.db'); + migrateLitterId(dbPath); +}