-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall-database.sh
More file actions
executable file
·176 lines (149 loc) · 5.61 KB
/
install-database.sh
File metadata and controls
executable file
·176 lines (149 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
# Nextlog Database Installation Script
# This script sets up the complete Nextlog database schema
set -e # Exit on any error
echo "🚀 Nextlog Database Installation Script"
echo "======================================"
# Configuration
DB_NAME="nextlog"
DB_USER="nextlog"
DB_PASSWORD="password"
DB_HOST="localhost"
DB_PORT="5432"
# Check if PostgreSQL is available
if ! command -v psql &> /dev/null; then
echo "❌ PostgreSQL client (psql) is not installed or not in PATH"
echo "Please install PostgreSQL client tools first"
exit 1
fi
# Function to check if database exists
check_database() {
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -lqt | cut -d \| -f 1 | grep -qw "$DB_NAME"
}
# Function to create database if it doesn't exist
create_database() {
echo "📋 Creating database '$DB_NAME'..."
createdb -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" "$DB_NAME"
echo "✅ Database '$DB_NAME' created successfully"
}
# Function to install schema
install_schema() {
echo "📦 Installing database schema..."
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -f install-database.sql
echo "✅ Database schema installed successfully"
}
# Function to load DXCC entities data
load_dxcc_data() {
if [ -f "scripts/dxcc_entities.sql" ]; then
echo "🌍 Loading DXCC entities data..."
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -f scripts/dxcc_entities.sql
echo "✅ DXCC entities data loaded successfully"
# Count loaded entities
local count=$(psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -t -c "SELECT COUNT(*) FROM dxcc_entities;")
echo " 📊 Loaded $count DXCC entities"
else
echo "❌ DXCC entities data file not found (scripts/dxcc_entities.sql)"
echo " This file is required for proper operation"
exit 1
fi
}
# Function to load states/provinces data
load_states_data() {
if [ -f "scripts/states_provinces_import.sql" ]; then
echo "🗺️ Loading states/provinces data..."
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -f scripts/states_provinces_import.sql
echo "✅ States/provinces data loaded successfully"
# Count loaded states/provinces
local count=$(psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -t -c "SELECT COUNT(*) FROM states_provinces;")
echo " 📊 Loaded $count states/provinces"
else
echo "❌ States/provinces data file not found (scripts/states_provinces_import.sql)"
echo " This file is required for proper operation"
exit 1
fi
}
# Function to verify installation
verify_installation() {
echo "🔍 Verifying installation..."
# Check if all tables exist
TABLES=(users stations contacts dxcc_entities states_provinces)
for table in "${TABLES[@]}"; do
if psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -c "\\dt $table" | grep -q "$table"; then
echo " ✅ Table '$table' exists"
else
echo " ❌ Table '$table' missing"
exit 1
fi
done
# Check if functions exist
FUNCTIONS=(update_updated_at_column ensure_single_default_station set_default_station_for_contact)
for func in "${FUNCTIONS[@]}"; do
if psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -c "\\df $func" | grep -q "$func"; then
echo " ✅ Function '$func' exists"
else
echo " ❌ Function '$func' missing"
exit 1
fi
done
# Verify data was loaded
local dxcc_count=$(psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -t -c "SELECT COUNT(*) FROM dxcc_entities;")
local states_count=$(psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -t -c "SELECT COUNT(*) FROM states_provinces;")
if [ "$dxcc_count" -gt 0 ]; then
echo " ✅ DXCC entities data loaded ($dxcc_count records)"
else
echo " ❌ No DXCC entities data found"
exit 1
fi
if [ "$states_count" -gt 0 ]; then
echo " ✅ States/provinces data loaded ($states_count records)"
else
echo " ❌ No states/provinces data found"
exit 1
fi
echo "✅ All components verified successfully"
}
# Main installation process
main() {
echo "🔧 Starting Nextlog database installation..."
echo " Database: $DB_NAME"
echo " User: $DB_USER"
echo " Host: $DB_HOST:$DB_PORT"
echo ""
# Set PostgreSQL password
export PGPASSWORD="$DB_PASSWORD"
# Check if database exists
if check_database; then
echo "⚠️ Database '$DB_NAME' already exists!"
read -p "Do you want to drop and recreate it? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "🗑️ Dropping existing database..."
dropdb -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" "$DB_NAME"
create_database
else
echo "❌ Installation cancelled"
exit 1
fi
else
create_database
fi
# Install schema
install_schema
# Load reference data
load_dxcc_data
load_states_data
# Verify installation
verify_installation
echo ""
echo "🎉 Nextlog database installation completed successfully!"
echo ""
echo "Next steps:"
echo "1. Start your Nextlog application"
echo "2. Create your first user account"
echo "3. Set up your station information"
echo "4. Start logging contacts!"
echo ""
echo "Happy logging! 73 de Nextlog"
}
# Run the main function
main "$@"