/** * @param DibiConnection * @param string table name * @param string file path * * @return int * @throws DibiException * @throws Nette\IOException * @throws Nette\UnexpectedValueException */ function dumpStructure(DibiConnection $db, $table, $file) { // Return code // 0: Not changed // 1: Created // 2: Updated $result = 0; $row = $db->query("SHOW CREATE TABLE %n", $table)->fetch(); if (!$row) { throw new Nette\UnexpectedValueException("Cannot gather create table syntax for: {$table}"); } $createSyntax = $row->{'Create Table'}; $createSyntax = preg_replace("/\\sAUTO_INCREMENT=([0-9]+)\\s/", " ", $createSyntax); // Check for existing SQL script if (file_exists($file)) { $lines = file($file); if ($lines === FALSE) { throw new Nette\IOException("Cannot read file {$file}"); } $oldCreateSyntax = ""; foreach ($lines as $line) { if (!Nette\Utils\Strings::startsWith($line, '--')) { $oldCreateSyntax .= "{$line}\n"; } } $new = rtrim(trim(preg_replace('/[\\s\\r\\n]+/', ' ', $createSyntax)), ';'); $old = rtrim(trim(preg_replace('/[\\s\\r\\n]+/', ' ', $oldCreateSyntax)), ';'); $result = $old != $new ? 2 : 0; // No existing SQL script } else { $result = 1; } // ---- // If we need to create new dump if ($result) { $createSyntax = "" . "--\n" . "-- Create table: {$table}\n" . "-- Generated: " . date("Y-m-d H:i:s") . "\n" . "--\n" . $createSyntax . ";"; if (file_put_contents($file, $createSyntax) === FALSE) { throw new Nette\IOException("Cannot write to file '{$file}'"); } } return $result; }
exit(1); } } // ----------------------------------------------------------------------------- // DB // ----------------------------------------------------------------------------- $existingTables = $db->getDatabaseInfo()->getTableNames(); foreach ($tables as $table => $script) { echo "[1;36m{$table}:[0m "; if (in_array($table, $existingTables)) { echo "exists"; // TODO: some table syntax check /* $r = $db->query("SHOW CREATE TABLE %n", $table)->fetch(); if($r) { $createSyntax = $r->{'Create Table'}; d($createSyntax); } else throw new Nette\InvalidStateException("Cannot gather create table syntax for: $table"); */ } else { echo "creating from [0;36m"; if (Nette\Utils\Strings::startsWith($script, __DIR__ . '/../')) { echo mb_substr($script, mb_strlen(__DIR__ . '/../')); } else { echo $script; } echo "[0m"; $db->loadFile($script); } echo "\n"; } echo "\nDone :-)\n\n";
<?php require __DIR__ . '/../vendor/autoload.php'; $configurator = new Nette\Configurator(); $environment = $configurator->setDebugMode('x.x.x.x'); $configurator->enableDebugger(__DIR__ . '/../log'); $configurator->setTempDirectory(__DIR__ . '/../temp'); $configurator->createRobotLoader()->addDirectory(__DIR__)->register(); $environment = (Nette\Configurator::detectDebugMode('127.0.0.1') or PHP_SAPI == 'cli' && Nette\Utils\Strings::startsWith(getHostByName(getHostName()), "192.168.")) ? 'development' : 'production'; $configurator->addConfig(__DIR__ . '/config/config.neon'); $configurator->addConfig(__DIR__ . "/config/config.{$environment}.neon"); $container = $configurator->createContainer(); $container->getService('application')->errorPresenter = 'Front:Error'; return $container;