/** * Process all added schemas. */ public function processSchemas() { echo "[0;34m" . str_pad("Gathering operations for {$this->database}...", 100, ' ', STR_PAD_RIGHT); $sql = implode("\n", $this->schemas); list($sql, $drop) = $this->hoist('@^DROP .*?;$@ms', $sql); $operations = $drop; list($sql, $alter) = $this->hoist('@^ALTER TABLE .*?;$@ms', $sql); // Gather all conditionals and optionally wrap them in a "lambda". list($sql, $ifs) = $this->hoist('@^IF.*?^END IF;$@ms', $sql); foreach ($ifs as &$if) { $if = $this->wrapInProcedure($if); } $operations = array_merge($operations, $ifs); list($sql, $tables) = $this->hoist('@^CREATE([A-Z]|\\s)*(TABLE|SEQUENCE) .*?;$@ms', $sql); // Hoist all other recreatable objects. list($sql, $procedures) = $this->hoist(static::REGEX_PROCEDURES, $sql); list($sql, $triggers) = $this->hoist(static::REGEX_TRIGGERS, $sql); $hoists = array_merge($procedures, $triggers); list($sql, $views) = $this->hoist('@^CREATE VIEW.*?;$@ms', $sql); $operations = array_merge($operations, $this->dropRecreatables()); $tablenames = []; foreach ($tables as $table) { if (!preg_match('@^CREATE.*?TABLE (\\w+) ?\\(@', $table, $name)) { $operations[] = $table; continue; } $name = $name[1]; $tablenames[] = $name; // If the table doesn't exist yet, create it verbatim. if (!$this->tableExists($name)) { $operations[] = $table; } else { $existing = $this->getTableDefinition($name); $new = $this->parseTableDefinition($table); foreach ($existing as $col => $definition) { if (!isset($new[$col])) { $operations[] = sprintf("ALTER TABLE %s DROP COLUMN %s", $name, $col); } } foreach ($new as $col => $definition) { if (!isset($existing[$col])) { $operations[] = $this->addColumn($name, $definition); } else { $comp = $definition; unset($comp['key']); if ($comp != $existing[$col]) { $operations = array_merge($operations, $this->alterColumn($name, $definition)); } } if (isset($definition['key']) && $definition['key'] == 'PRI') { $operations[] = sprintf("ALTER TABLE %s ADD PRIMARY KEY(%s)", $name, $col); } } } } foreach ($hoists as $hoist) { preg_match('@^CREATE (\\w+) (\\w+)@', $hoist, $data); if ($data[1] == 'FUNCTION' && $this instanceof Pgsql) { $data[2] .= '()'; } $operations[] = "DROP {$data[1]} IF EXISTS {$data[2]}"; $operations[] = $hoist; } if (strlen(trim($sql))) { $operations[] = $sql; } // Recrate views $operations = array_merge($operations, $views); // Rerun ifs and alters $operations = array_merge($operations, $alter, $ifs); // Cleanup: remove tables that are not in the schema (any more) foreach ($this->getTables('BASE TABLE') as $table) { if (!in_array($table, $tablenames) && !$this->shouldIgnore($table)) { $operations[] = "DROP TABLE {$table} CASCADE"; } } // Perform the actual operations and display progress meter echo "[100D[1;34mPerforming operations for {$this->database}...\n"; echo "[0m"; $bar = new CliProgressBar(count($operations)); $bar->display(); $bar->setColorToRed(); $fails = []; while ($operation = array_shift($operations)) { try { $this->pdo->exec($operation); } catch (PDOException $e) { if (preg_match("@(ALTER|CREATE)@", $operation)) { $fails[$operation] = $e->getMessage(); } } $bar->progress(); if ($bar->getCurrentstep() >= $bar->getSteps() / 2) { $bar->setColorToYellow(); } } $bar->setColorToGreen(); $bar->display(); $bar->end(); echo "[0m"; if ($fails) { echo "The following operations raised an exception:\n"; echo "(This might not be a problem necessarily):\n"; foreach ($fails as $command => $reason) { echo "{$command}: {$reason}\n"; } } }
<?php require_once './../vendor/autoload.php'; use Dariuszp\CliProgressBar; $bar = new CliProgressBar(48); $bar->setBarLength(5); $bar->display(); $bar->setColorToRed(); while ($bar->getCurrentstep() < $bar->getSteps()) { usleep(50000); $bar->progress(); if ($bar->getCurrentstep() >= $bar->getSteps() / 2) { $bar->setColorToYellow(); } } $bar->setColorToGreen(); $bar->display(); $bar->end();