image_tagger/scripts/setup-database.js
2025-11-03 13:22:29 +05:30

105 lines
3.2 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
/**
* Database Setup Script
* Creates the database and imports the schema
*/
require('dotenv').config();
const mysql = require('mysql2/promise');
const fs = require('fs');
const path = require('path');
async function setupDatabase() {
console.log('================================================');
console.log('🗄️ Database Setup');
console.log('================================================\n');
const config = {
host: process.env.DB_HOST || 'localhost',
port: process.env.DB_PORT || 3306,
user: process.env.DB_USER || 'root',
password: process.env.DB_PASSWORD || '',
};
const dbName = process.env.DB_NAME || 'property_tagging';
let connection;
try {
// Connect to MySQL (without selecting a database)
console.log(`📡 Connecting to MySQL at ${config.host}:${config.port}...`);
connection = await mysql.createConnection(config);
console.log('✅ Connected to MySQL\n');
// Create database if it doesn't exist
console.log(`📦 Creating database '${dbName}' if it doesn't exist...`);
await connection.query(`CREATE DATABASE IF NOT EXISTS \`${dbName}\``);
console.log('✅ Database created/verified\n');
// Switch to the database
await connection.query(`USE \`${dbName}\``);
// Check if tables already exist
const [tables] = await connection.query('SHOW TABLES');
if (tables.length > 0) {
console.log(' Tables already exist:');
tables.forEach(table => {
console.log(` - ${Object.values(table)[0]}`);
});
console.log('\n✅ Database is already set up!\n');
} else {
// Import schema
console.log('📄 Importing schema from 001_initial_schema.sql...');
const schemaPath = path.join(__dirname, '..', '001_initial_schema.sql');
if (!fs.existsSync(schemaPath)) {
throw new Error(`Schema file not found: ${schemaPath}`);
}
const schema = fs.readFileSync(schemaPath, 'utf8');
// Split by semicolon and execute each statement
const statements = schema
.split(';')
.map(s => s.trim())
.filter(s => s.length > 0 && !s.startsWith('--'));
for (const statement of statements) {
await connection.query(statement);
}
console.log('✅ Schema imported successfully\n');
// Verify tables were created
const [newTables] = await connection.query('SHOW TABLES');
console.log('📊 Created tables:');
newTables.forEach(table => {
console.log(` - ${Object.values(table)[0]}`);
});
console.log('');
}
console.log('================================================');
console.log('✅ Database setup complete!');
console.log('================================================\n');
} catch (error) {
console.error('❌ Database setup failed:');
console.error(error.message);
console.error('\nPlease check:');
console.error(' 1. MySQL is running: systemctl status mysql');
console.error(' 2. Credentials in .env file are correct');
console.error(' 3. MySQL user has CREATE DATABASE privileges\n');
process.exit(1);
} finally {
if (connection) {
await connection.end();
}
}
}
// Run setup
setupDatabase();