Skip to content

Instantly share code, notes, and snippets.

@BenMorel
Created November 23, 2020 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenMorel/bb77b169aa5e904ba86fc32bf0d3eb87 to your computer and use it in GitHub Desktop.
Save BenMorel/bb77b169aa5e904ba86fc32bf0d3eb87 to your computer and use it in GitHub Desktop.
Example of using the benmorel/smartdump PHP API instead of the CLI
<?php
/**
* Example valid with benmorel/smartdump~0.2.0
*
* https://github.com/BenMorel/smartdump
*/
use BenMorel\SmartDump\Configuration\DumpConfiguration;
use BenMorel\SmartDump\Configuration\TargetTable;
use BenMorel\SmartDump\Driver\MySQLDriver;
use BenMorel\SmartDump\Dumper;
use BenMorel\SmartDump\Object\Table;
require __DIR__ . '/vendor/autoload.php';
$pdo = new PDO('mysql:host=localhost;charset=utf8mb4', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$driver = new MySQLDriver($pdo);
$dumper = new Dumper($pdo, $driver);
$configuration = new DumpConfiguration();
$configuration->targetTables = [];
$configuration->targetTables[] = createTargetTable('mydb', 'table1');
$configuration->targetTables[] = createTargetTable('mydb', 'table2', 'LIMIT 100');
$configuration->addDropTable = false;
$configuration->addCreateTable = true;
$configuration->includeSchemaNameInOutput = true;
$configuration->merge = false;
foreach ($dumper->dump($configuration) as $statement) {
echo $statement, PHP_EOL;
}
function createTargetTable(string $database, string $table, ?string $conditions = null): TargetTable
{
$targetTable = new TargetTable();
$targetTable->table = new Table($database, $table);
$targetTable->conditions = $conditions;
return $targetTable;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment