Skip to content

ndestates/laravel-schema-checker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Schema Checker

A standalone tool for comparing Laravel Eloquent models with database schema and generating migrations.

Features

  • 🔍 Model-Database Comparison: Compare your Laravel models with the actual database schema
  • 🔧 Automatic Fixes: Automatically update model $fillable properties
  • 📋 Migration Generation: Generate Laravel migrations for missing tables and columns
  • 💾 Database Backup: Create automatic backups before running migrations
  • 🌍 Environment Aware: Works in DDEV, Docker, and local PHP environments
  • 📊 Multiple Output Formats: JSON output support for CI/CD integration

Installation

Option 1: Download and Place in Your Laravel Project

  1. Download or clone this repository
  2. Copy the entire laravel-schema-checker folder into your Laravel project root
  3. Make the scripts executable:
    chmod +x laravel-schema-checker/check.php
    chmod +x laravel-schema-checker/run-checker.sh

Option 2: Clone Directly into Your Project

cd your-laravel-project
git clone https://github.com/your-repo/laravel-schema-checker.git
cd laravel-schema-checker
chmod +x check.php run-checker.sh

Usage

Basic Commands

# Compare models with database schema
php laravel-schema-checker/check.php

# Fix model fillable properties automatically
php laravel-schema-checker/check.php --fix

# Preview changes without applying them
php laravel-schema-checker/check.php --dry-run

# Generate Laravel migrations for missing tables/columns
php laravel-schema-checker/check.php --generate-migrations

# Create database backup and run migrations
php laravel-schema-checker/check.php --generate-migrations --backup-db --run-migrations

# Environment-aware execution (auto-detects DDEV/Docker/local)
./laravel-schema-checker/run-checker.sh --generate-migrations --backup-db --run-migrations

Command Line Options

Option Description
--fix Fix model $fillable properties automatically
--dry-run Show what would be changed without applying
--generate-migrations Generate Laravel migrations
--run-migrations Run migrations after generating them
--backup-db Create database backup before running migrations
--backup Show database backup recommendations
--json Output results in JSON format
--help, -h Show help message

Environment Support

The tool automatically detects and adapts to different environments:

  • DDEV Projects: Uses ddev exec php commands
  • Docker Containers: Runs commands directly in containers
  • Local PHP: Uses local PHP installation

Examples

Complete Workflow: Check, Generate, Backup, and Migrate

# Step 1: Check for issues
php laravel-schema-checker/check.php

# Step 2: Generate migrations for missing tables
php laravel-schema-checker/check.php --generate-migrations

# Step 3: Create backup and run migrations automatically
php laravel-schema-checker/check.php --run-migrations --backup-db

# Or do it all in one command
php laravel-schema-checker/check.php --generate-migrations --run-migrations --backup-db

CI/CD Integration

# JSON output for automated processing
php laravel-schema-checker/check.php --json > schema_check.json

# Dry run to check for issues without making changes
php laravel-schema-checker/check.php --dry-run

Environment-Specific Usage

# In DDEV projects
ddev exec php laravel-schema-checker/check.php --fix

# Using the auto-detecting wrapper
./laravel-schema-checker/run-checker.sh --generate-migrations --backup-db --run-migrations

Configuration

The tool uses sensible defaults but can be configured by modifying the src/config/CheckConfig.php file:

  • Models Directory: Path to your Laravel models
  • Migrations Directory: Where to generate migrations
  • Excluded Fields: Fields to ignore in comparisons (id, created_at, updated_at)
  • Log File Location: Where to write execution logs

Safety Features

  • Database Backup: Automatic backup creation before migrations
  • Dry Run Mode: Preview changes without applying them
  • Error Handling: Comprehensive error reporting and rollback suggestions
  • Environment Detection: Prevents incorrect command execution

Output

The tool generates:

  • Console Output: Real-time progress and results
  • Log Files: Detailed execution logs with timestamps
  • Migration Files: Laravel migration files for schema changes
  • Database Backups: SQL dump files before migrations

Troubleshooting

Common Issues

  1. "Could not find Laravel project root"

    • Ensure you're running the tool from within a Laravel project directory
    • The tool looks for the artisan file to detect Laravel projects
  2. Database Connection Issues

    • Check your Laravel database configuration
    • Ensure database credentials are correct
  3. Permission Issues

    • Make sure the tool can write to the migrations directory
    • Check file permissions for log files and backups

Getting Help

php laravel-schema-checker/check.php --help

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Changelog

Version 2.0.0 - Standalone Release

  • Converted to standalone tool that can be copied to any Laravel project
  • Added environment-aware command execution
  • Implemented automatic database backup functionality
  • Enhanced migration generation with relationship support
  • Added JSON output support for CI/CD integration

Version 1.1.0

  • Fixed excluded fields issue in model comparisons
  • Improved field filtering logic

Version 1.0.0

  • Initial release with basic model-database comparison
  • Support for fillable property fixes
  • Migration and SQL file generation

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors