Skip to content

Instantly share code, notes, and snippets.

@Gyvastis
Created March 24, 2020 07:10
Show Gist options
  • Save Gyvastis/f07f6c31c2090288ce3f0e6858e8c4d7 to your computer and use it in GitHub Desktop.
Save Gyvastis/f07f6c31c2090288ce3f0e6858e8c4d7 to your computer and use it in GitHub Desktop.
Generate initial Doctrine Migration from SQL dump
<?php
$sqlDump = file_get_contents(dirname(__FILE__) . '/dump.sql');
$sqlStatements = explode(";\n", $sqlDump);
$sqlStatements = array_filter($sqlStatements, function($statement) {
$statement = trim($statement);
return strpos($statement, '#') !== 0 && strpos($statement, '/*') !== 0;
});
$sqlStatements = array_map(function($statement){
$statement = trim($statement);
return !empty($statement) ? sprintf("%s;", $statement) : null;
}, $sqlStatements);
$sqlStatements = array_filter($sqlStatements);
$sampleMigrationFileName = 'sample_Version20200207124939.php';
$sampleMigrationFile = file_get_contents(dirname(__FILE__) . '/' . $sampleMigrationFileName);
$sqlStatementsMerged = '';
foreach($sqlStatements as $statement) {
$sqlStatementsMerged .= sprintf("\t\t\$this->addSql(\"%s\");\n", $statement);
}
$realMigrationFileName = str_replace('sample_', '', $sampleMigrationFileName);
file_put_contents(
dirname(__FILE__) . '/../../src/DoctrineMigrations/2020/' . $realMigrationFileName,
str_replace('/* COMSAVE */', $sqlStatementsMerged, $sampleMigrationFile)
);
<?php
declare(strict_types=1);
namespace Comsave\Website\Migrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20200207124939 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
/* COMSAVE */
}
public function down(Schema $schema): void
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment