Skip to content

Commit 3bb2f87

Browse files
committed
Update RunMigrationsCommand.php
1 parent 79a94ab commit 3bb2f87

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

webfiori/framework/cli/commands/RunMigrationsCommand.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ public function __construct() {
4242
new Argument('--ini', 'Creates migrations table in database if not exist.', true),
4343
], 'Execute database migrations.');
4444
}
45+
private function createMigrationsTable() : bool {
46+
$this->println("Initializing migrations table...");
47+
try {
48+
$this->migrationsRunner->table('migrations')->createTable()->execute();
49+
} catch (DatabaseException $ex) {
50+
$this->error("Unable to create migrations table due to following:");
51+
$this->println($ex->getMessage());
52+
return false;
53+
}
54+
$this->success("Migrations table succesfully created.");
55+
return true;
56+
}
4557
/**
4658
* Execute the command.
4759
*
@@ -55,27 +67,15 @@ public function exec() : int {
5567
return 0;
5668
}
5769
if ($this->isConnectionSet()) {
58-
$this->migrationsRunner->setConnectionInfo($this->connectionInfo);
59-
$this->println("Initializing migrations table...");
60-
try {
61-
$this->migrationsRunner->table('migrations')->createTable()->execute();
62-
} catch (DatabaseException $ex) {
63-
$this->error("Unable to create migrations table due to following:");
64-
$this->println($ex->getMessage());
70+
$this->migrationsRunner = new MigrationsRunner(APP_PATH, '\\app', $this->connectionInfo);
71+
if(!$this->createMigrationsTable()) {
6572
return -1;
6673
}
67-
$this->success("Migrations table succesfully created.");
6874
} else {
6975
return -2;
7076
}
7177
} else if ($this->migrationsRunner !== null) {
72-
$this->println("Initializing migrations table...");
73-
try {
74-
$this->println($this->migrationsRunner->table('migrations')->createTable()->getQuery());
75-
$this->migrationsRunner->table('migrations')->createTable()->execute();
76-
} catch (DatabaseException $ex) {
77-
$this->error("Unable to create migrations table due to following:");
78-
$this->println($ex->getMessage());
78+
if (!$this->createMigrationsTable()) {
7979
return -1;
8080
}
8181
} else {
@@ -206,11 +206,13 @@ private function hasConnections() : bool {
206206
}
207207
private function hasMigrations(string $namespace) : bool {
208208
$this->migrationsRunner = new MigrationsRunner(ROOT_PATH.DS.str_replace('\\', DS, $namespace), $namespace, null);
209-
210-
if (count($this->migrationsRunner->getMigrations()) == 0) {
209+
$this->println("Checking namespace '$namespace' for migrations...");
210+
$count = count($this->migrationsRunner->getMigrations());
211+
if ($count == 0) {
211212
$this->info("No migrations were found in the namespace '$namespace'.");
212213
return false;
213214
}
215+
$this->println("Found $count migration(s).");
214216
return true;
215217
}
216218
}

0 commit comments

Comments
 (0)