✨ Features: - Modern tourism website with responsive design - AdminJS admin panel with image editor integration - PostgreSQL database with comprehensive schema - Docker containerization - Image upload and gallery management 🛠 Tech Stack: - Backend: Node.js + Express.js - Database: PostgreSQL 13+ - Frontend: HTML/CSS/JS with responsive design - Admin: AdminJS with custom components - Deployment: Docker + Docker Compose - Image Processing: Sharp with optimization 📱 Admin Features: - Routes/Tours management (city, mountain, fishing) - Guides profiles with specializations - Articles and blog system - Image editor with upload/gallery/URL options - User management and authentication - Responsive admin interface 🎨 Design: - Korean tourism focused branding - Mobile-first responsive design - Custom CSS with modern aesthetics - Image optimization and gallery - SEO-friendly structure 🔒 Security: - Helmet.js security headers - bcrypt password hashing - Input validation and sanitization - CORS protection - Environment variables
114 lines
4.2 KiB
JavaScript
114 lines
4.2 KiB
JavaScript
import fs from 'fs';
|
||
import path from 'path';
|
||
import { fileURLToPath } from 'url';
|
||
import { dirname } from 'path';
|
||
import db from '../src/config/database.js';
|
||
|
||
const __filename = fileURLToPath(import.meta.url);
|
||
const __dirname = dirname(__filename);
|
||
|
||
export async function initDatabase() {
|
||
try {
|
||
console.log('🚀 Starting complete database initialization...');
|
||
|
||
// Check if database is connected
|
||
await db.query('SELECT 1');
|
||
console.log('✅ Database connection successful');
|
||
|
||
// 1. Create schema
|
||
console.log('📋 Creating database schema...');
|
||
const schemaPath = path.join(__dirname, 'schema.sql');
|
||
const schema = fs.readFileSync(schemaPath, 'utf8');
|
||
await db.query(schema);
|
||
console.log('✅ Database schema created successfully');
|
||
|
||
// 2. Check if tables are empty (first run)
|
||
const checkResult = await db.query('SELECT COUNT(*) FROM admins');
|
||
const isEmpty = parseInt(checkResult.rows[0].count) === 0;
|
||
|
||
if (isEmpty) {
|
||
console.log('📝 Database is empty, inserting mock data...');
|
||
|
||
// Insert mock data
|
||
const mockDataPath = path.join(__dirname, 'mock-data.sql');
|
||
const mockData = fs.readFileSync(mockDataPath, 'utf8');
|
||
await db.query(mockData);
|
||
console.log('✅ Mock data inserted successfully');
|
||
} else {
|
||
console.log('ℹ️ Database already contains data, skipping mock data insertion');
|
||
}
|
||
|
||
// 3. Run any pending migrations
|
||
console.log('🔄 Checking for pending migrations...');
|
||
|
||
// Check if rating system exists
|
||
try {
|
||
await db.query('SELECT 1 FROM route_ratings LIMIT 1');
|
||
console.log('ℹ️ Rating system already exists');
|
||
} catch (error) {
|
||
console.log('📈 Installing rating system...');
|
||
const ratingMigrationPath = path.join(__dirname, 'rating-system-migration.sql');
|
||
if (fs.existsSync(ratingMigrationPath)) {
|
||
const ratingMigration = fs.readFileSync(ratingMigrationPath, 'utf8');
|
||
await db.query(ratingMigration);
|
||
console.log('✅ Rating system installed successfully');
|
||
}
|
||
}
|
||
|
||
// Check if style editor migration is applied
|
||
try {
|
||
const result = await db.query("SELECT 1 FROM site_settings WHERE setting_key = 'primary_color' LIMIT 1");
|
||
if (result.rows.length === 0) {
|
||
console.log('🎨 Installing style editor features...');
|
||
const styleMigrationPath = path.join(__dirname, 'style-editor-migration.sql');
|
||
if (fs.existsSync(styleMigrationPath)) {
|
||
const styleMigration = fs.readFileSync(styleMigrationPath, 'utf8');
|
||
await db.query(styleMigration);
|
||
console.log('✅ Style editor installed successfully');
|
||
}
|
||
} else {
|
||
console.log('ℹ️ Style editor already installed');
|
||
}
|
||
} catch (error) {
|
||
console.log('🎨 Installing style editor features...');
|
||
const styleMigrationPath = path.join(__dirname, 'style-editor-migration.sql');
|
||
if (fs.existsSync(styleMigrationPath)) {
|
||
const styleMigration = fs.readFileSync(styleMigrationPath, 'utf8');
|
||
await db.query(styleMigration);
|
||
console.log('✅ Style editor installed successfully');
|
||
}
|
||
}
|
||
|
||
// 4. Apply image triggers fix migration
|
||
try {
|
||
console.log('🖼️ Installing image triggers fix...');
|
||
const imageTriggersMigrationPath = path.join(__dirname, 'image-triggers-fix.sql');
|
||
if (fs.existsSync(imageTriggersMigrationPath)) {
|
||
const imageTriggersMigration = fs.readFileSync(imageTriggersMigrationPath, 'utf8');
|
||
await db.query(imageTriggersMigration);
|
||
console.log('✅ Image triggers fix applied successfully');
|
||
}
|
||
} catch (error) {
|
||
console.log('ℹ️ Image triggers fix - some changes may already be applied:', error.message);
|
||
}
|
||
|
||
console.log('✨ Database initialization completed successfully!');
|
||
|
||
} catch (error) {
|
||
console.error('❌ Database initialization failed:', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
// Run if called directly
|
||
if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
||
initDatabase()
|
||
.then(() => {
|
||
console.log('🎉 All done!');
|
||
process.exit(0);
|
||
})
|
||
.catch((error) => {
|
||
console.error('💥 Initialization failed:', error);
|
||
process.exit(1);
|
||
});
|
||
} |