@@ -96,6 +96,44 @@ public function actionIndex()
96
96
$ this ->stdout ("\n" );
97
97
}
98
98
99
+
100
+ public function actionWaitForConnection (
101
+ $ dsn = null ,
102
+ $ user = null ,
103
+ $ password = null
104
+ ) {
105
+ $ dsn = $ dsn ?: getenv ("DATABASE_DSN_BASE " );
106
+ $ user = $ user ?: getenv ("DB_ENV_MYSQL_ROOT_USER " );
107
+ $ password = $ password ?: getenv ("DB_ENV_MYSQL_ROOT_PASSWORD " );
108
+
109
+ if (empty ($ user ) || empty ($ password ) || empty ($ dsn )) {
110
+ $ this ->stderr ('Configuration failed, aborting. ' );
111
+ return ;
112
+ }
113
+
114
+ // trying to connect to database with PDO (20 times, interval 1 second)
115
+ $ this ->stdout (
116
+ "Checking database connection on DSN ' {$ dsn }' with user ' {$ user }' "
117
+ );
118
+
119
+ try {
120
+ // retry an operation up to 20 times
121
+ $ pdo = \igorw \retry (
122
+ $ this ->mysqlRetryMaxCount ,
123
+ function () use ($ dsn , $ user , $ password ) {
124
+ $ this ->stdout ('. ' );
125
+ sleep ($ this ->mysqlRetryTimeout );
126
+ return new \PDO ($ dsn , $ user , $ password );
127
+ }
128
+ );
129
+ } catch (FailingTooHardException $ e ) {
130
+ $ this ->stderr ("\n\nError: Unable to connect to database ' " .$ e ->getMessage ()."'' " );
131
+ \Yii::$ app ->end (1 );
132
+ }
133
+ $ this ->stdout (' [OK] ' .PHP_EOL );
134
+
135
+ }
136
+
99
137
/**
100
138
* Create MySQL database
101
139
*
@@ -120,63 +158,42 @@ public function actionCreate(
120
158
$ user = null ,
121
159
$ pass = null
122
160
) {
161
+
162
+ $ db = $ db ?: getenv ("DATABASE_DSN_DB " );
163
+ $ dsn = $ dsn ?: getenv ("DATABASE_DSN_BASE " );
164
+ $ root = $ root ?: getenv ("DB_ENV_MYSQL_ROOT_USER " );
165
+ $ rootPassword = $ rootPassword ?: getenv ("DB_ENV_MYSQL_ROOT_PASSWORD " );
166
+ $ user = $ user ?: getenv ("DB_ENV_MYSQL_USER " );
167
+ $ pass = $ pass ?: getenv ("DB_ENV_MYSQL_PASSWORD " );
168
+
123
169
// check dsn
124
- if ($ db === null ) {
125
- $ db = getenv ("DATABASE_DSN_DB " );
126
- }
127
170
if (empty ($ db )) {
128
171
$ this ->stderr ('No database configured, aborting. ' );
129
172
return ;
130
173
}
131
-
132
174
// check root user settings
133
- $ root = $ root ?: getenv ("DB_ENV_MYSQL_ROOT_USER " );
134
175
if (empty ($ root )) {
135
176
$ this ->stderr ('No root user configured, aborting. ' );
136
177
return ;
137
178
}
138
- $ rootPassword = $ rootPassword ?: getenv ("DB_ENV_MYSQL_ROOT_PASSWORD " );
139
179
if (empty ($ rootPassword )) {
140
180
$ this ->stderr ('No root password configured, aborting. ' );
141
181
return ;
142
182
}
143
-
144
- $ user = $ user ?: getenv ("DB_ENV_MYSQL_USER " );
145
- $ pass = $ pass ?: getenv ("DB_ENV_MYSQL_PASSWORD " );
146
- $ dsn = $ dsn ?: getenv ("DATABASE_DSN_BASE " );
147
-
148
183
if (empty ($ user ) || empty ($ pass ) || empty ($ dsn )) {
149
184
$ this ->stderr ('Configuration failed, aborting. ' );
150
185
return ;
151
186
}
152
187
153
- // trying to connect to database with PDO (20 times, interval 1 second)
154
- $ this ->stdout (
155
- "Checking database connection on DSN ' {$ dsn }' with user ' {$ root }' "
156
- );
157
-
158
- try {
159
- // retry an operation up to 20 times
160
- $ pdo = \igorw \retry (
161
- $ this ->mysqlRetryMaxCount ,
162
- function () use ($ dsn , $ root , $ rootPassword ) {
163
- $ this ->stdout ('. ' );
164
- sleep ($ this ->mysqlRetryTimeout );
165
- return new \PDO ($ dsn , $ root , $ rootPassword );
166
- }
167
- );
168
- } catch (FailingTooHardException $ e ) {
169
- $ this ->stderr ("\n\nError: Unable to connect to database ' " .$ e ->getMessage ()."'' " );
170
- \Yii::$ app ->end (1 );
171
- }
172
- $ this ->stdout (' [OK] ' );
173
-
188
+ // wait for database connection (BC)
189
+ $ this ->actionWaitForConnection ($ dsn , $ root , $ rootPassword );
174
190
175
191
// try to create a database for the user
176
192
$ this ->stdout (
177
193
"\nCreating database ' {$ db }' and granting permissions to user ' {$ user }' on DSN ' {$ dsn }' with user ' {$ root }' "
178
194
);
179
195
try {
196
+ $ pdo = new \PDO ($ dsn , $ root , $ rootPassword );
180
197
\igorw \retry (
181
198
$ this ->mysqlRetryMaxCount ,
182
199
function () use ($ dsn , $ root , $ rootPassword , $ pdo , $ user , $ pass , $ db ) {
0 commit comments