dibakar / db-sync
A Laravel package for database synchronization with support for migrations and SQL imports
Requires
- php: ^8.1
- illuminate/console: ^9.0 || ^10.0 || ^11.0 || ^12.0
- illuminate/database: ^9.0 || ^10.0 || ^11.0 || ^12.0
- illuminate/support: ^9.0 || ^10.0 || ^11.0 || ^12.0
Requires (Dev)
- orchestra/testbench: ^7.0 || ^8.0 || ^9.0 || ^10.0
README
A powerful Laravel package for database synchronization, migration, and SQL file import operations. This package provides a unified interface to manage your database schema and data across different environments with ease and confidence.
✨ Features
- Flexible Database Operations: Supports various database operations including migrations, fresh migrations, and refresh operations
- SQL File Import: Import SQL files or directories of SQL files directly
- Multiple Database Connections: Work with different database connections easily
- Progress Tracking: Option to show progress for long-running operations
- Production Safety: Includes safety checks for production environments
- Seeding Support: Run seeders after migration/import operations
- Configurable: Customize behavior through configuration file
- Logging: Detailed logging of all operations
🚀 Installation
Requirements
- PHP 8.1 or higher
- Laravel 9.x or later
- Composer
Install via Composer
composer require dibakar/db-sync
Configuration (Optional)
Publish the configuration file to customize the package behavior:
php artisan vendor:publish --provider="Dibakar\DbSync\DbSyncServiceProvider" --tag="db-sync-config"
This will create a db-sync.php
file in your config
directory with sensible defaults.
📦 Version Compatibility
Laravel | PHP | Package |
---|---|---|
11.x | 8.2+ | ^1.0 |
10.x | 8.2+ | ^1.0 |
9.x | 8.1+ | ^1.0 |
🚀 Quick Start
Basic Usage
Run database synchronization with default settings:
php artisan db:sync
Common Examples
# Run with a specific database connection # Import SQL files php artisan db:sync --mode=import --path=/path/to/file.sql # Import directory of SQL files php artisan db:sync --mode=import --path=/path/to/sql/files # Import with fresh database (drop all tables first) php artisan db:sync --mode=import-fresh --path=/path/to/schema.sql # Run migrations and seeders php artisan db:sync --mode=migrate --seed # Perform a fresh migration (drop all tables and re-run migrations) php artisan db:sync --mode=migrate-fresh --seed # Refresh migrations (rollback and re-run) php artisan db:sync --mode=migrate-refresh --seed # Append SQL to existing database php artisan db:sync --mode=import-append --path=/path/to/data.sql # Force operations in production (use with caution!) php artisan db:sync --mode=migrate-fresh --force
📚 Documentation
Available Commands
Basic Command Structure
php artisan db:sync [options]
Command Options
Option | Description | Type | Default |
---|---|---|---|
--path |
Path to a migration file, SQL file, or directory | string |
null |
--mode |
Operation mode (see Modes section below) | string |
import |
--database |
Database connection to use | string |
config('database.default') |
--force |
Skip production confirmation | flag |
false |
--step |
Show progress for each operation | flag |
false |
--seed |
Run seeders after migration/import | flag |
false |
--no-interaction |
Disable interactive prompts | flag |
false |
Operation Modes
Mode | Description | Use Case |
---|---|---|
import |
Import SQL files without running migrations | Initial data import, data migration |
import-fresh |
Drop all tables and import SQL files | Fresh installation, complete database reset |
import-append |
Append SQL to existing database | Data updates, incremental imports |
migrate |
Run database migrations | Standard migration updates |
migrate-fresh |
Drop all tables and run migrations | Complete database refresh |
migrate-refresh |
Rollback and re-run all migrations | Development environment updates |
Configuration
After publishing the configuration file, you can customize the package behavior:
return [ /* |-------------------------------------------------------------------------- | Default Database Connection |-------------------------------------------------------------------------- | | This option controls the default database connection that will be used | by the package when none is specified via the --database option. | */ 'default_connection' => env('DB_CONNECTION', 'mysql'), /* |-------------------------------------------------------------------------- | SQL Import Settings |-------------------------------------------------------------------------- | | These options control how SQL files are processed during import. | */ 'sql' => [ 'max_execution_time' => 0, // 0 = no limit 'query_logging' => true, 'transaction' => true, // Wrap imports in a transaction ], ];
🛠 Advanced Usage
Using in Code
You can also use the package directly in your code:
use Dibakar\DbSync\DbSync; // Initialize with default options $dbSync = app(DbSync::class); // Run synchronization programmatically $result = $dbSync->sync([ 'mode' => 'migrate', 'database' => 'mysql', 'seed' => true, 'force' => false, ]); // Handle the result if ($result['success']) { echo "Database synchronized successfully!\n"; } else { echo "Error: " . $result['message'] . "\n"; }
🔒 Security
Best Practices
- Always test database operations in a development environment first
- Always take backups before running destructive operations
- Use the
--force
flag with extreme caution in production - For large databases, use
--step
to monitor progress - Review SQL files from untrusted sources before importing
- Use database transactions when possible
Reporting Security Issues
If you discover any security related issues, please email dibakarmitra07@gmail.com instead of using the issue tracker.
🤝 Contributing
Thank you for considering contributing to Laravel DB Sync! The contribution guide can be found in the CONTRIBUTING.md file.
Code of Conduct
In order to ensure that the Laravel community is welcoming to all, please review and abide by the Code of Conduct.
Testing
composer test
📄 License
Laravel DB Sync is open-sourced software licensed under the MIT license.