@@ -58,17 +58,34 @@ private function createMigrationsTable() : bool {
5858 $ this ->success ("Migrations table succesfully created. " );
5959 return true ;
6060 }
61- private function hasMigrationsTable (?MigrationsRunner $ runner ) : bool {
61+ /**
62+ * Checks if the argument '--ini' is provided or not and initialize
63+ * migrations table if provided.
64+ *
65+ * @param MigrationsRunner|null $runner An optional instance which will be
66+ * used to run the migrations. If provided, the table will be created based
67+ * on the connection of the runner.
68+ *
69+ * @return bool If the argument '--ini' is not provided, true is returned.
70+ * Other than that, an attempt to create the migrations table will be made.
71+ * If created, true is returned. Other than that, false is returned.
72+ */
73+ private function checkMigrationsTable (?MigrationsRunner $ runner ) {
6274 if (!$ this ->isArgProvided ('--ini ' )) {
63- return true ;
75+ return 0 ;
6476 }
6577 $ conn = $ this ->getDBConnection ($ runner );
6678 if ($ conn !== null ) {
6779 $ temp = $ runner !== null ? $ runner : new MigrationsRunner (APP_PATH , '\\' .APP_DIR , $ conn );
68- $ temp ->createMigrationsTable ();
69- return true ;
80+ try {
81+ $ temp ->createMigrationsTable ();
82+ } catch (Throwable $ ex ) {
83+ $ this ->error ('Unable to create migrations table: ' .$ ex ->getMessage ());
84+ return -1 ;
85+ }
86+ return 0 ;
7087 }
71- return false ;
88+ return 0 ;
7289 }
7390 private function getNS (?MigrationsRunner $ runner = null ) {
7491 if ($ this ->isArgProvided ('--ns ' )) {
@@ -89,14 +106,30 @@ private function getNS(?MigrationsRunner $runner = null) {
89106 public function exec () : int {
90107
91108 $ runner = $ this ->getRunnerArg ();
109+ if (!($ runner instanceof MigrationsRunner) && $ runner !== null ) {
110+ return -1 ;
111+ }
92112 $ ns = $ this ->getNS ($ runner );
93- $ this ->hasMigrationsTable ($ runner );
94-
113+ if ($ this ->checkMigrationsTable ($ runner ) == -1 ) {
114+ return -1 ;
115+ }
116+
95117 if (!$ this ->hasMigrations ($ ns )) {
96118 return 0 ;
97119 }
98120
99- $ this ->executeMigrations ($ runner );
121+ $ connection = $ this ->getDBConnection ($ runner );
122+
123+ if (!($ connection instanceof ConnectionInfo)) {
124+ return -1 ;
125+ }
126+ try {
127+ $ runner = new MigrationsRunner (ROOT_PATH .DS . str_replace ('\\' , DS , $ ns ), $ ns , $ connection );
128+ } catch (Throwable $ ex ) {
129+ $ this ->error ($ ex ->getMessage ());
130+ return -1 ;
131+ }
132+ return $ this ->executeMigrations ($ runner );
100133 }
101134 private function executeMigrations (MigrationsRunner $ runner ) {
102135 $ listOfApplied = [];
@@ -113,13 +146,31 @@ private function executeMigrations(MigrationsRunner $runner) {
113146 }
114147 return 0 ;
115148 }
116- private function getDBConnection (?MigrationsRunner $ runner = null ) : ?ConnectionInfo {
149+ /**
150+ * Returns the connection that will be used in running the migrations.
151+ *
152+ * The method will first check on the provided runner. If it has connection,
153+ * it will be returned. Then it will check if the argument '--connection' is
154+ * provided or not. If provided, the method will check if such connection
155+ * exist in application configuration. If no connection was found, null
156+ * is returned. If the argument '--connection' is not provided, the method will
157+ * ask the user to select a connection from the connections which
158+ * exist in application configuration.
159+ *
160+ * @param MigrationsRunner|null $runner If given and the connection is set
161+ * on the instance, it will be returned.
162+ *
163+ * @return ConnectionInfo|null
164+ */
165+ private function getDBConnection (?MigrationsRunner $ runner = null ) {
117166
118167 if ($ runner !== null ) {
119- return $ runner ->getConnectionInfo ();
168+ if ($ runner ->getConnectionInfo () !== null ) {
169+ return $ runner ->getConnectionInfo ();
170+ }
120171 }
121172 if (!$ this ->hasConnections ()) {
122- return null ;
173+ return - 1 ;
123174 }
124175 $ dbConnections = array_keys (App::getConfig ()->getDBConnections ());
125176
@@ -128,7 +179,7 @@ private function getDBConnection(?MigrationsRunner $runner = null) : ?Connection
128179
129180 if (!in_array ($ connection , $ dbConnections )) {
130181 $ this ->error ("No connection was found which has the name ' $ connection'. " );
131- return null ;
182+ return - 1 ;
132183 } else {
133184 return App::getConfig ()->getDBConnection ($ connection );
134185 }
@@ -155,7 +206,11 @@ private function applyNext(MigrationsRunner $runner, &$listOfApplied) : bool {
155206 return false ;
156207 }
157208 }
158- private function getRunnerArg () : ?MigrationsRunner {
209+ /**
210+ *
211+ * @return MigrationsRunner|int|null
212+ */
213+ private function getRunnerArg () {
159214 $ runner = $ this ->getArgValue ('--runner ' );
160215
161216 if ($ runner === null ) {
@@ -167,18 +222,18 @@ private function getRunnerArg() : ?MigrationsRunner {
167222 $ runnerInst = new $ runner ();
168223 } catch (Throwable $ exc ) {
169224 $ this ->error ('The argument --runner has invalid value: Exception: " ' .$ exc ->getMessage ().'". ' );
170- return null ;
225+ return - 1 ;
171226 }
172227
173228 if (!($ runnerInst instanceof MigrationsRunner)) {
174229 $ this ->error ('The argument --runner has invalid value: " ' .$ runner .'" is not an instance of "MigrationsRunner". ' );
175- return null ;
230+ return - 1 ;
176231 } else {
177232 return $ runnerInst ;
178233 }
179234 } else {
180235 $ this ->error ('The argument --runner has invalid value: Class " ' .$ runner .'" does not exist. ' );
181- return null ;
236+ return - 1 ;
182237 }
183238 }
184239 private function hasConnections () : bool {
0 commit comments