Clean: Fresh database init with parents table - no migrations

This commit is contained in:
2026-03-09 01:59:52 -05:00
parent eec18daeea
commit 3ae3458dfc

View File

@@ -16,7 +16,7 @@ function initDatabase(dbPath) {
console.log('Initializing database schema...'); console.log('Initializing database schema...');
// Dogs table - Core registry // Dogs table - NO sire/dam columns, only litter_id
db.exec(` db.exec(`
CREATE TABLE IF NOT EXISTS dogs ( CREATE TABLE IF NOT EXISTS dogs (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -29,9 +29,11 @@ function initDatabase(dbPath) {
microchip TEXT, microchip TEXT,
photo_urls TEXT, -- JSON array of photo URLs photo_urls TEXT, -- JSON array of photo URLs
notes TEXT, notes TEXT,
litter_id INTEGER,
is_active INTEGER DEFAULT 1, is_active INTEGER DEFAULT 1,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP, 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 WHERE microchip IS NOT NULL
`); `);
// Parents table - Relationship mapping // Parents table - Stores sire/dam relationships
db.exec(` db.exec(`
CREATE TABLE IF NOT EXISTS parents ( CREATE TABLE IF NOT EXISTS parents (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -51,7 +53,7 @@ function initDatabase(dbPath) {
parent_type TEXT NOT NULL CHECK(parent_type IN ('sire', 'dam')), parent_type TEXT NOT NULL CHECK(parent_type IN ('sire', 'dam')),
FOREIGN KEY (dog_id) REFERENCES dogs(id) ON DELETE CASCADE, FOREIGN KEY (dog_id) REFERENCES dogs(id) ON DELETE CASCADE,
FOREIGN KEY (parent_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(` db.exec(`
CREATE INDEX IF NOT EXISTS idx_dogs_name ON dogs(name); 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_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_dog ON parents(dog_id);
CREATE INDEX IF NOT EXISTS idx_parents_parent ON parents(parent_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); CREATE INDEX IF NOT EXISTS idx_litters_sire ON litters(sire_id);
@@ -141,7 +144,10 @@ function initDatabase(dbPath) {
END; 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(); db.close();
return true; return true;
@@ -159,5 +165,11 @@ module.exports = { initDatabase, getDatabase };
// Run initialization if called directly // Run initialization if called directly
if (require.main === module) { if (require.main === module) {
const dbPath = process.env.DB_PATH || path.join(__dirname, '../../data/breedr.db'); 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); initDatabase(dbPath);
console.log('\n✓ Database ready!\n');
} }