PHP code example of dibakar / db-sync

1. Go to this page and download the library: Download dibakar/db-sync library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dibakar / db-sync example snippets


# 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

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
    ],
];

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";
}
bash
php artisan vendor:publish --provider="Dibakar\DbSync\DbSyncServiceProvider" --tag="db-sync-config"
bash
php artisan db:sync
bash
php artisan db:sync [options]