public function getSqlDiff()
{
$tmp = $this->getJarves()->getCacheDir() . '/';
if (!file_exists($tmp . 'propel/propel.yml')) {
self::writeConfig();
self::writeBuildProperties();
self::collectSchemas();
}
//remove all migration files
if (is_dir($tmp . 'propel/build/')) {
$files = Finder::create()->in($tmp . 'propel/build/')->depth(0)->name('PropelMigration_*.php');
foreach ($files as $file) {
unlink($file->getPathname());
}
}
$platform = $this->getJarves()->getSystemConfig()->getDatabase()->getMainConnection()->getType();
$platform = ucfirst($platform) . 'Platform';
$input = new ArrayInput(array('--config-dir' => $tmp . 'propel/', '--schema-dir' => $tmp . 'propel/', '--output-dir' => $tmp . 'propel/build/', '--platform' => $platform, '--verbose' => 'vvv'));
$command = new MigrationDiffCommand();
$command->getDefinition()->addOption(new InputOption('--verbose', '-v|vv|vvv', InputOption::VALUE_NONE, ''));
$output = new StreamOutput(fopen('php://memory', 'rw'));
$command->run($input, $output);
if (is_dir($tmp . 'propel/build/')) {
$files = Finder::create()->in($tmp . 'propel/build/')->depth(0)->name('PropelMigration_*.php');
foreach ($files as $file) {
$lastMigrationFile = $file->getPathname();
break;
}
}
if (!isset($lastMigrationFile) || !$lastMigrationFile) {
return '';
}
preg_match('/(.*)\\/PropelMigration_([0-9]*)\\.php/', $lastMigrationFile, $matches);
$clazz = 'PropelMigration_' . $matches[2];
$uid = str_replace('.', '_', uniqid('', true));
$newClazz = 'PropelMigration__' . $uid;
$content = file_get_contents($lastMigrationFile);
$content = str_replace('class ' . $clazz, 'class PropelMigration__' . $uid, $content);
file_put_contents($lastMigrationFile, $content);
include $lastMigrationFile;
$obj = new $newClazz();
$sql = $obj->getUpSQL();
$sql = $sql['default'];
// unlink($lastMigrationFile);
// todo
// if (is_array($protectTables = $this->getJarves()->getSystemConfig()->getDatabase()->getProtectTables())) {
// foreach ($protectTables as $table) {
// $table = str_replace('%pfx%', pfx, $table);
// $sql = preg_replace('/^DROP TABLE (IF EXISTS|) ' . $table . '(\n|\s)(.*)\n+/im', '', $sql);
// }
// }
$sql = preg_replace('/^#.*$/im', '', $sql);
return trim($sql);
}