Skip to content
23 changes: 16 additions & 7 deletions commands/MigrateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace uhi67\umvc\commands;

use Codeception\Util\Debug;
use Exception;
use Throwable;
use uhi67\umvc\AppHelper;
Expand Down Expand Up @@ -103,7 +102,9 @@ public function actionUp() {
// Execute new migrations
$n = 0;
foreach($new as $file) {
$this->connection->pdo->beginTransaction();
if(!$this->connection->pdo->beginTransaction()) {
echo "Error starting transaction\n";
}
$name = pathinfo($file, PATHINFO_FILENAME);
$ext = pathinfo($file, PATHINFO_EXTENSION);
$filename = $this->migrationPath.'/'.$file;
Expand Down Expand Up @@ -167,15 +168,23 @@ public function actionUp() {
break;
}
}
// Note: MySQL auto-commits transactions on DDL statements. Therefore we may find our transaction already gone
if($this->connection->pdo->inTransaction()) {
$this->connection->pdo->commit();
$this->connection->pdo->commit();
}
else {
if($this->verbose > 1) echo "Transaction lost...\n";
}
}
catch(Throwable $e) {
if($this->connection->pdo->inTransaction()) $this->connection->pdo->rollBack();
if(ENV_DEV) Debug::debug(sprintf("Exception in migration: '%s' in file '%s' at line '%d'", $e->getMessage(), $e->getFile(), $e->getLine()));
printf("Exception: %s in file %s at line %d\n", $e->getMessage(), $e->getFile(), $e->getLine());
echo "Applying migration '". $name."' caused an exception\n";
printf("'%s' in file %s at line %d\n", $e->getMessage(), $e->getFile(), $e->getLine());

if($this->connection->pdo->inTransaction()) {
$this->connection->pdo->rollBack();
}
else {
if($this->verbose > 1) echo "Transaction lost...\n";
}
throw new Exception("Applying migration '". $name."' caused an exception", 500, $e);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Ansi.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Ansi {
* @param bool $close -- restore color after the message
* @return string -- string with pre- and appended ansi color commands
*/
public static function color($string, $fg, $bg='null', $close=true) {
public static function color($string, $fg, $bg='', $close=true) {
if(!$fg) $fg = $bg=='white' ? 'black' : 'white';
if(!is_string($fg)) return $string.'*'; //throw new InternalException('fg must be string');
$color = self::$colors[trim(strtolower($fg))] ?? '0';
Expand Down
Loading