This commit is contained in:
2025-12-23 17:41:30 -08:00
parent d6ac68f7d3
commit ff534eb448
18 changed files with 3989 additions and 461 deletions

220
backend/migrate.js Normal file
View File

@@ -0,0 +1,220 @@
import 'dotenv/config';
import sqlite3 from 'sqlite3';
import path from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const dbPath = process.env.DATABASE_PATH || path.join(__dirname, 'awards.db');
// Migration commands
const command = process.argv[2];
const db = new sqlite3.Database(dbPath, (err) => {
if (err) {
console.error('Failed to connect to database:', err);
process.exit(1);
}
});
// Create tables
function createTables() {
return new Promise((resolve, reject) => {
db.serialize(() => {
db.run(`CREATE TABLE IF NOT EXISTS awards (
id INTEGER PRIMARY KEY AUTOINCREMENT,
category TEXT NOT NULL,
address TEXT NOT NULL,
submitted_by VARCHAR(255),
emoji_tally TEXT DEFAULT '{}',
submitted_date DATETIME DEFAULT CURRENT_TIMESTAMP,
approved_date DATETIME,
lat REAL,
lng REAL
)`, (err) => {
if (err) {
console.error('Error creating awards table:', err);
reject(err);
} else {
console.log('✓ Created awards table');
resolve();
}
});
});
});
}
// Seed sample data
async function seed() {
const seedData = [
{
category: 'Best Pizza',
address: '1188 Garnet Ave, San Diego, CA 92109',
submitted_by: 'admin',
lat: 32.7977,
lng: -117.2514,
approved: true
},
{
category: 'Best Coffee Shop',
address: '4876 Santa Monica Ave, San Diego, CA 92109',
submitted_by: 'admin',
lat: 32.7959,
lng: -117.2346,
approved: true
},
{
category: 'Best Taco Shop',
address: '1830 Garnet Ave, San Diego, CA 92109',
submitted_by: 'admin',
lat: 32.7980,
lng: -117.2420,
approved: true
},
{
category: 'Best Sunset View',
address: 'Crystal Pier, Pacific Beach, San Diego, CA 92109',
submitted_by: 'admin',
lat: 32.7965,
lng: -117.2547,
approved: true
},
{
category: 'Best Ice Cream',
address: '1025 Garnet Ave, San Diego, CA 92109',
submitted_by: 'admin',
lat: 32.7975,
lng: -117.2470,
approved: true
},
{
category: 'Best Breakfast Spot',
address: '4970 Cass St, San Diego, CA 92109',
submitted_by: 'local_foodie',
lat: 32.7962,
lng: -117.2580,
approved: false
}
];
console.log('Seeding database...');
for (const item of seedData) {
await new Promise((resolve, reject) => {
const approvedDate = item.approved ? new Date().toISOString() : null;
db.run(
'INSERT INTO awards (category, address, submitted_by, emoji_tally, lat, lng, approved_date) VALUES (?, ?, ?, ?, ?, ?, ?)',
[item.category, item.address, item.submitted_by, '{}', item.lat, item.lng, approvedDate],
function (err) {
if (err) {
console.error(`✗ Failed to seed "${item.category}":`, err.message);
reject(err);
} else {
console.log(`✓ Seeded: ${item.category}`);
resolve();
}
}
);
});
}
console.log('✓ Database seeded successfully');
}
// Reset database (drop all tables)
function reset() {
return new Promise((resolve, reject) => {
db.run('DROP TABLE IF EXISTS awards', (err) => {
if (err) {
console.error('Error dropping awards table:', err);
reject(err);
} else {
console.log('✓ Dropped awards table');
resolve();
}
});
});
}
// Backup database
function backup() {
const backupDir = path.join(__dirname, 'backups');
if (!fs.existsSync(backupDir)) {
fs.mkdirSync(backupDir);
}
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const backupPath = path.join(backupDir, `awards-${timestamp}.db`);
fs.copyFileSync(dbPath, backupPath);
console.log(`✓ Database backed up to: ${backupPath}`);
}
// Main execution
async function main() {
try {
switch (command) {
case 'create':
console.log('Creating tables...');
await createTables();
console.log('✓ Migration complete');
break;
case 'seed':
console.log('Seeding database...');
await seed();
console.log('✓ Seed complete');
break;
case 'reset':
console.log('Resetting database...');
await reset();
await createTables();
console.log('✓ Database reset complete');
break;
case 'reset:seed':
console.log('Resetting and seeding database...');
await reset();
await createTables();
await seed();
console.log('✓ Reset and seed complete');
break;
case 'backup':
console.log('Backing up database...');
backup();
break;
default:
console.log(`
Usage: node migrate.js <command>
Commands:
create Create database tables
seed Seed database with sample data
reset Drop all tables and recreate them
reset:seed Reset database and seed with sample data
backup Create a backup of the database
Examples:
node migrate.js create
node migrate.js seed
node migrate.js reset:seed
node migrate.js backup
`);
process.exit(0);
}
db.close();
process.exit(0);
} catch (error) {
console.error('Migration failed:', error);
db.close();
process.exit(1);
}
}
main();