Files
quality_app-v2/documentation/INIT_DB_UPGRADE_COMPLETE.md
Quality App Developer b15cc93b9d FG Scan form validation improvements with warehouse module updates
- Fixed 3 JavaScript syntax errors in fg_scan.html (lines 951, 840-950, 1175-1215)
- Restored form field validation with proper null safety checks
- Re-enabled auto-advance between form fields
- Re-enabled CP code auto-complete with hyphen detection
- Updated validation error messages with clear format specifications and examples
- Added autocomplete='off' to all input fields
- Removed auto-prefix correction feature
- Updated warehouse routes and modules for box assignment workflow
- Added/improved database initialization scripts
- Updated requirements.txt dependencies

Format specifications implemented:
- Operator Code: OP + 2 digits (example: OP01, OP99)
- CP Code: CP + 8 digits + hyphen + 4 digits (example: CP00000000-0001)
- OC1/OC2 Codes: OC + 2 digits (example: OC01, OC99)
- Defect Code: 3 digits only
2026-01-30 10:50:06 +02:00

6.4 KiB

init_db.py Upgrade Complete

Date: January 28, 2026
Status: Complete


🎯 Summary

The init_db.py file has been completely upgraded to match initialize_db.py for redundancy and robustness. Both files now have identical functionality.


📋 What Changed

Before (init_db.py)

❌ 9 tables only (basic setup)
❌ No scanfg_orders table
❌ No box tracking tables (boxes_crates, box_contents, cp_location_history)
❌ No warehouse support
❌ No schema verification
❌ No auto-repair capabilities
❌ Limited default data insertion

After (init_db.py)

✅ 18+ tables (complete setup)
✅ scanfg_orders WITH location_id and box_id columns
✅ All box tracking tables
✅ Full warehouse support
✅ Schema verification via SchemaVerifier
✅ Automatic database repair for missing tables/columns
✅ Comprehensive default data insertion
✅ Database validation verification

New Features in init_db.py

1. Step 0: Database Verification & Repair

check_and_repair_database()
  • Detects if database exists
  • Runs SchemaVerifier on existing databases
  • Automatically repairs missing tables/columns/data
  • Safe for upgrades

2. 18+ Complete Tables

Category Tables
Users & Auth users, user_credentials, roles
Permissions user_modules, user_permissions
Quality quality_inspections, scanfg_orders
Warehouse warehouse_locations, boxes_crates, box_contents
Audit cp_location_history
API qz_pairing_keys, api_keys
System application_settings, backup_schedules
Hierarchy worker_manager_bindings

3. Scanned Goods Box Support

✅ boxes_crates - Box creation and tracking
✅ box_contents - CP codes to boxes mapping
✅ scanfg_orders - FG scans WITH location_id and box_id
✅ cp_location_history - Box movement audit trail

4. Comprehensive Default Data

  • 6 default roles (superadmin, admin, manager, warehouse_manager, worker, warehouse_worker)
  • Admin user with password hashing
  • Warehouse locations (FG_INCOMING, TRUCK_LOADING)
  • Application settings (app_name, version, timeouts, backup settings)

5. Database Validation

verify_database()
  • Confirms all tables exist
  • Counts roles, users, and credentials
  • Validates database integrity

🔄 Key Improvements

Feature init_db.py (Before) init_db.py (After) initialize_db.py
Tables 9 18+ 18+
Location_id in scanfg NO YES YES
Box_id in scanfg NO YES YES
Schema Verification NO YES YES
Auto-Repair NO YES YES
Warehouse Tables NO YES YES
Foreign Keys Partial Complete Complete
Indexes Minimal Complete Complete
Lines of Code 294 640 631

🚀 Execution Flow

init_db.py (or initialize_db.py) runs
        ↓
Step 0: Check & Repair Database
        ├─ Database exists? → Run verification
        └─ Database new? → Skip verification
        ↓
Step 1: Create Database
        ↓
Step 2: Create Tables (18+)
        ├─ Users & Auth
        ├─ Permissions & Access
        ├─ Quality & Scanning
        ├─ Warehouse & Boxes
        ├─ API & System
        └─ With all FK constraints & indexes
        ↓
Step 3: Insert Default Data
        ├─ Roles (6 types)
        ├─ Admin user
        ├─ Warehouse locations
        └─ Application settings
        ↓
Step 4: Verify Database
        ├─ Check all tables exist
        ├─ Count records
        └─ Report status
        ↓
✅ Database Ready for Application

📝 Configuration

Both init_db.py and initialize_db.py now use:

Priority 1: From app.config.Config

from app.config import Config
DB_HOST = Config.DB_HOST
DB_PORT = Config.DB_PORT
DB_USER = Config.DB_USER
DB_PASSWORD = Config.DB_PASSWORD
DB_NAME = Config.DB_NAME

Priority 2: Fallback to Environment Variables

DB_HOST = os.getenv('DB_HOST', 'mariadb')
DB_PORT = int(os.getenv('DB_PORT', '3306'))
DB_USER = os.getenv('DB_USER', 'quality_user')
DB_PASSWORD = os.getenv('DB_PASSWORD', 'quality_pass')
DB_NAME = os.getenv('DB_NAME', 'quality_db')

Redundancy & Robustness Benefits

Redundancy

  • Both files are now identical in functionality
  • Either can be used for initialization
  • No single point of failure for database setup
  • Easy to maintain consistency

Robustness

  • Schema verification detects errors
  • Auto-repair fixes missing elements
  • Foreign key constraints enforced
  • Comprehensive logging for debugging
  • Verification step confirms success

Upgrade Safety

  • Existing databases detected and verified
  • Missing tables automatically created
  • Missing columns automatically added
  • Data preserved during upgrades
  • Schema evolution supported

🔧 Files Modified

File: init_db.py

  • Before: 294 lines (basic initialization)
  • After: 640 lines (comprehensive initialization)
  • Status: Ready for production

File: initialize_db.py

  • Status: Unchanged (remains reference)

🧪 Testing Recommendations

# Test fresh database
rm -rf data/db/*
python3 init_db.py

# Test schema repair (existing database)
# Simulate missing column:
# ALTER TABLE scanfg_orders DROP COLUMN location_id;

# Run again:
python3 init_db.py
# Should detect and repair missing column

# Verify tables
docker exec quality_app_mariadb mariadb -u root quality_db -e "SHOW TABLES;"

📚 Documentation


Conclusion

init_db.py is now production-ready with:

  • Complete feature parity with initialize_db.py
  • 18+ tables including all box tracking tables
  • location_id and box_id in scanfg_orders
  • Automatic schema verification and repair
  • Comprehensive logging and validation
  • Upgrade-safe database initialization

Both init_db.py and initialize_db.py can now be used interchangeably for redundancy and robustness.