Initial commit: Clean Mold Cost Calculator application

- Flask-based mold cost calculation application
- PostgreSQL database with SQLAlchemy ORM
- Docker/Podman containerization
- Comprehensive documentation in docs/
- Clean, organized codebase without obsolete files
- Production-ready deployment configuration
- Enhanced pytest configuration with coverage reporting
- Master/Development branch structure
This commit is contained in:
Gan, Jimmy
2025-06-24 02:30:09 +08:00
commit 6c0d5a4e63
108 changed files with 15911 additions and 0 deletions
+39
View File
@@ -0,0 +1,39 @@
#!/bin/bash
# Configuration
BACKUP_DIR="/backups"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
DB_HOST="mold_cost_db"
DB_NAME="mold_cost"
DB_USER="postgres"
DB_PASSWORD="postgres"
# Create backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"
# Set PostgreSQL password environment variable
export PGPASSWORD="$DB_PASSWORD"
# Create backup with error handling
echo "Creating database backup at $(date)..."
if pg_dump -h "$DB_HOST" -U "$DB_USER" -d "$DB_NAME" --no-password | gzip > "$BACKUP_DIR/backup_$TIMESTAMP.sql.gz"; then
echo "Backup completed successfully: backup_$TIMESTAMP.sql.gz"
# Keep only the last 7 backups
echo "Cleaning up old backups..."
ls -t "$BACKUP_DIR"/backup_*.sql.gz | tail -n +8 | xargs -r rm
# Verify backup file exists and has content
if [ -f "$BACKUP_DIR/backup_$TIMESTAMP.sql.gz" ] && [ -s "$BACKUP_DIR/backup_$TIMESTAMP.sql.gz" ]; then
echo "Backup verification successful: $(du -h "$BACKUP_DIR/backup_$TIMESTAMP.sql.gz" | cut -f1)"
else
echo "ERROR: Backup file is empty or missing!"
exit 1
fi
else
echo "ERROR: Backup failed!"
exit 1
fi
# Clear password from environment
unset PGPASSWORD
+164
View File
@@ -0,0 +1,164 @@
#!/bin/bash
# Backup management script for Mold Cost Calculator
# This script provides easy commands for backup operations
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_header() {
echo -e "${BLUE}=== $1 ===${NC}"
}
# Function to create manual backup
create_backup() {
print_header "Creating Manual Backup"
if podman-compose exec backup /backup.sh; then
print_status "Manual backup completed successfully!"
else
print_error "Manual backup failed!"
exit 1
fi
}
# Function to list backups
list_backups() {
print_header "Available Backups"
if [ -d "./backups" ]; then
if [ "$(ls -A ./backups)" ]; then
ls -lah ./backups/backup_*.sql.gz 2>/dev/null | while read -r line; do
echo " $line"
done
else
print_warning "No backup files found in ./backups directory"
fi
else
print_warning "Backups directory does not exist"
fi
}
# Function to restore from backup
restore_backup() {
local backup_file="$1"
if [ -z "$backup_file" ]; then
print_error "Please specify a backup file to restore from."
echo "Usage: $0 restore <backup_file>"
echo "Example: $0 restore ./backups/backup_20241201_120000.sql.gz"
exit 1
fi
if [ ! -f "$backup_file" ]; then
print_error "Backup file '$backup_file' not found!"
list_backups
exit 1
fi
print_header "Database Restore"
print_warning "This will overwrite the current database!"
echo "Backup file: $backup_file"
read -p "Are you sure you want to continue? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
print_status "Stopping application..."
podman-compose stop app
print_status "Restoring database..."
if podman-compose exec backup /restore.sh restore "/backups/$(basename "$backup_file")"; then
print_status "Database restored successfully!"
print_status "Starting application..."
podman-compose start app
print_status "Restore completed!"
else
print_error "Database restore failed!"
print_status "Starting application..."
podman-compose start app
exit 1
fi
else
print_status "Restore cancelled."
fi
}
# Function to show backup status
show_status() {
print_header "Backup Service Status"
# Check if backup container is running
if podman-compose ps backup | grep -q "Up"; then
print_status "Backup service is running"
else
print_error "Backup service is not running"
fi
# Show recent backup logs
print_header "Recent Backup Logs"
podman-compose logs --tail=10 backup
# List available backups
list_backups
}
# Function to show help
show_help() {
print_header "Backup Management Script"
echo "Usage: $0 [command] [options]"
echo ""
echo "Commands:"
echo " backup - Create a manual backup"
echo " list - List available backups"
echo " restore <backup_file> - Restore database from backup"
echo " status - Show backup service status"
echo " help - Show this help message"
echo ""
echo "Examples:"
echo " $0 backup"
echo " $0 list"
echo " $0 restore ./backups/backup_20241201_120000.sql.gz"
echo " $0 status"
}
# Main script logic
case "${1:-help}" in
"backup"|"create")
create_backup
;;
"list"|"ls")
list_backups
;;
"restore")
restore_backup "$2"
;;
"status")
show_status
;;
"help"|"--help"|"-h"|"")
show_help
;;
*)
print_error "Unknown command '$1'"
echo ""
show_help
exit 1
;;
esac
+44
View File
@@ -0,0 +1,44 @@
import os
import sys
import psycopg2
from pathlib import Path
def get_db_url():
"""Get database URL from environment variables."""
db_user = os.getenv('DB_USER', 'mold_user')
db_password = os.getenv('DB_PASSWORD', 'mold_password')
db_host = os.getenv('DB_HOST', 'localhost')
db_port = os.getenv('DB_PORT', '5434')
db_name = os.getenv('DB_NAME', 'mold_cost_calculator_dev')
return f"postgresql://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}"
def run_migration():
# Get database URL from environment
db_url = get_db_url()
try:
# Connect to the database
conn = psycopg2.connect(db_url)
conn.autocommit = True
cur = conn.cursor()
# Read and execute the migration SQL
migration_file = Path(__file__).parent / 'update_mold_types.sql'
with open(migration_file, 'r') as f:
sql = f.read()
cur.execute(sql)
print("Migration completed successfully!")
except Exception as e:
print(f"Error running migration: {str(e)}")
sys.exit(1)
finally:
if 'cur' in locals():
cur.close()
if 'conn' in locals():
conn.close()
if __name__ == '__main__':
run_migration()
+15
View File
@@ -0,0 +1,15 @@
#!/bin/bash
# Exit on error
set -e
# Change to the migrations directory
cd "$(dirname "$0")"
echo "Running database migration..."
python3 run_migration.py
echo "Testing migration..."
python3 test_mold_types.py
echo "Migration completed successfully!"
+82
View File
@@ -0,0 +1,82 @@
import os
import sys
import psycopg2
from pathlib import Path
def get_db_url():
"""Get database URL from environment variables."""
db_user = os.getenv('DB_USER', 'mold_user')
db_password = os.getenv('DB_PASSWORD', 'mold_password')
db_host = os.getenv('DB_HOST', 'localhost')
db_port = os.getenv('DB_PORT', '5434')
db_name = os.getenv('DB_NAME', 'mold_cost_calculator_dev')
return f"postgresql://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}"
def test_migration():
# Get database URL from environment
db_url = get_db_url()
try:
# Connect to the database
conn = psycopg2.connect(db_url)
cur = conn.cursor()
# Check if the column exists
cur.execute("""
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'mold_quotations'
AND column_name = 'mold_specific_type'
""")
if cur.fetchone():
print("✅ mold_specific_type column exists")
else:
print("❌ mold_specific_type column does not exist")
return False
# Check if the column is NOT NULL
cur.execute("""
SELECT is_nullable
FROM information_schema.columns
WHERE table_name = 'mold_quotations'
AND column_name = 'mold_specific_type'
""")
is_nullable = cur.fetchone()[0]
if is_nullable == 'NO':
print("✅ mold_specific_type column is NOT NULL")
else:
print("❌ mold_specific_type column is nullable")
return False
# Check if all records have a value
cur.execute("""
SELECT COUNT(*)
FROM mold_quotations
WHERE mold_specific_type IS NULL
""")
null_count = cur.fetchone()[0]
if null_count == 0:
print("✅ All records have a mold_specific_type value")
else:
print(f"❌ Found {null_count} records with NULL mold_specific_type")
return False
print("All tests passed!")
return True
except Exception as e:
print(f"Error testing migration: {str(e)}")
return False
finally:
if 'cur' in locals():
cur.close()
if 'conn' in locals():
conn.close()
if __name__ == '__main__':
success = test_migration()
sys.exit(0 if success else 1)
+92
View File
@@ -0,0 +1,92 @@
#!/bin/bash
# Configuration
BACKUP_DIR="/backups"
DB_HOST="mold_cost_db"
DB_NAME="mold_cost"
DB_USER="postgres"
DB_PASSWORD="postgres"
# Set PostgreSQL password environment variable
export PGPASSWORD="$DB_PASSWORD"
# Function to list available backups
list_backups() {
echo "Available backups:"
ls -la "$BACKUP_DIR"/backup_*.sql.gz 2>/dev/null | while read -r line; do
echo " $line"
done
}
# Function to restore from backup
restore_backup() {
local backup_file="$1"
if [ ! -f "$backup_file" ]; then
echo "ERROR: Backup file '$backup_file' not found!"
list_backups
exit 1
fi
echo "WARNING: This will overwrite the current database!"
echo "Backup file: $backup_file"
read -p "Are you sure you want to continue? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "Restoring database from backup..."
# Drop and recreate database
echo "Dropping existing database..."
dropdb -h "$DB_HOST" -U "$DB_USER" --no-password "$DB_NAME" 2>/dev/null || true
echo "Creating new database..."
createdb -h "$DB_HOST" -U "$DB_USER" --no-password "$DB_NAME"
echo "Restoring data..."
if gunzip -c "$backup_file" | psql -h "$DB_HOST" -U "$DB_USER" -d "$DB_NAME" --no-password; then
echo "Database restored successfully!"
else
echo "ERROR: Database restore failed!"
exit 1
fi
else
echo "Restore cancelled."
fi
}
# Main script logic
if [ $# -eq 0 ]; then
echo "Usage: $0 [list|restore <backup_file>]"
echo ""
echo "Commands:"
echo " list - List available backups"
echo " restore <backup_file> - Restore database from backup file"
echo ""
echo "Examples:"
echo " $0 list"
echo " $0 restore /backups/backup_20241201_120000.sql.gz"
exit 1
fi
case "$1" in
"list")
list_backups
;;
"restore")
if [ -z "$2" ]; then
echo "ERROR: Please specify a backup file to restore from."
echo "Usage: $0 restore <backup_file>"
exit 1
fi
restore_backup "$2"
;;
*)
echo "ERROR: Unknown command '$1'"
echo "Usage: $0 [list|restore <backup_file>]"
exit 1
;;
esac
# Clear password from environment
unset PGPASSWORD