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.
- 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
- PHP 8.1 or higher
- Laravel 9.x or later
- Composer
composer require dibakar/db-sync
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.
Laravel | PHP | Package |
---|---|---|
11.x | 8.2+ | ^1.0 |
10.x | 8.2+ | ^1.0 |
9.x | 8.1+ | ^1.0 |
Run database synchronization with default settings:
php artisan db:sync
# 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
php artisan db:sync [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 |
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 |
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
],
];
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";
}
- 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
If you discover any security related issues, please email dibakarmitra07@gmail.com instead of using the issue tracker.
Thank you for considering contributing to Laravel DB Sync! The contribution guide can be found in the CONTRIBUTING.md file.
In order to ensure that the Laravel community is welcoming to all, please review and abide by the Code of Conduct.
composer test
Laravel DB Sync is open-sourced software licensed under the MIT license.