Skip to content

Commit 7a3d8bd

Browse files
authored
Merge pull request #333 from bowphp/refactor/code-base
fix: add protected command and catch console error
2 parents a8b458f + b726967 commit 7a3d8bd

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

src/Console/Command/MigrationCommand.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44

55
namespace Bow\Console\Command;
66

7-
use Bow\Console\AbstractCommand;
7+
use Exception;
8+
use ErrorException;
9+
use Bow\Support\Str;
810
use Bow\Console\Color;
911
use Bow\Database\Database;
12+
use Bow\Database\QueryBuilder;
13+
use Bow\Console\AbstractCommand;
14+
use Bow\Database\Migration\Table;
15+
use Bow\Database\Exception\MigrationException;
1016
use Bow\Database\Exception\ConnectionException;
1117
use Bow\Database\Exception\QueryBuilderException;
12-
use Bow\Database\Migration\Table;
13-
use Bow\Database\QueryBuilder;
14-
use Bow\Support\Str;
15-
use ErrorException;
16-
use Exception;
1718

1819
class MigrationCommand extends AbstractCommand
1920
{
@@ -84,7 +85,13 @@ private function createMigrationTable(): void
8485
{
8586
$connection = $this->arg->getParameter("--connection", config("database.default"));
8687

87-
Database::connection($connection);
88+
try {
89+
Database::connection($connection);
90+
} catch (Exception $exception) {
91+
echo Color::red("▶ Please check your database configuration on .env.json file\n");
92+
throw new MigrationException($exception->getMessage(), (int)$exception->getCode());
93+
}
94+
8895
$adapter = Database::getConnectionAdapter();
8996

9097
$table = $adapter->getTablePrefix() . config('database.migration', 'migrations');
@@ -107,7 +114,12 @@ private function createMigrationTable(): void
107114
$generator->make()
108115
);
109116

110-
Database::statement($sql);
117+
try {
118+
Database::statement($sql);
119+
} catch (Exception $exception) {
120+
echo sprintf("%s %s\n", Color::red(""), $sql);
121+
throw new MigrationException($exception->getMessage(), (int)$exception->getCode());
122+
}
111123
}
112124

113125
/**

src/Console/Console.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ class Console
3939
'seed',
4040
'help',
4141
'clear',
42-
'flush'
42+
'flush',
43+
'launch',
44+
'serve',
4345
];
4446

4547
/**
@@ -256,7 +258,7 @@ public function call(?string $command): mixed
256258
try {
257259
return call_user_func_array([$this, $command], [$this->arg->getRawCommand()]);
258260
} catch (Exception $e) {
259-
echo $e->getMessage();
261+
echo Color::red(sprintf("$command command failed with: %s\n", $e->getMessage()));
260262
exit(1);
261263
}
262264
}

src/Database/Connection/Adapters/SqliteAdapter.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,11 @@ public function connection(): void
4545
// Build the PDO connection
4646
$this->pdo = new PDO('sqlite:' . $this->config['database']);
4747

48-
// Set the PDO attributes that we want
48+
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
49+
$this->pdo->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING);
4950
$this->pdo->setAttribute(
5051
PDO::ATTR_DEFAULT_FETCH_MODE,
5152
$this->config['fetch'] ?? $this->fetch
5253
);
53-
54-
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
55-
$this->pdo->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING);
5654
}
5755
}

0 commit comments

Comments
 (0)