Skip to content

Commit 8059a0b

Browse files
connection spinner
1 parent 3454b44 commit 8059a0b

File tree

6 files changed

+59
-14
lines changed

6 files changed

+59
-14
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "javanile/mysql-import",
33
"description": "Import SQL file from command-line",
4-
"version": "0.0.8",
4+
"version": "0.0.9",
55
"license": "MIT",
66
"type": "project",
77
"authors": [

develop.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
set -e
3+
4+
[[ -f mysql-import.log ]] && rm mysql-import.log
5+
6+
docker-compose down -v
7+
docker-compose up -d mysql
8+
docker-compose run --rm -e MYSQL_HOST=ciao -e MYSQL_USER=root -e MYSQL_PASSWORD=secret php ./mysql-import ./tests/fixtures/database.sql
9+
10+
if [[ -f mysql-import.log ]]; then
11+
echo "---[ mysql-import.log ]---"
12+
cat mysql-import.log
13+
fi

docker-compose.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ services:
1717

1818
mysql:
1919
image: mysql:5.5
20-
environment: ['MYSQL_ROOT_PASSWORD=root']
20+
environment: ['MYSQL_ROOT_PASSWORD=secret']
21+
volumes: ['mysql:/var/lib/mysql']
22+
23+
volumes:
24+
mysql:

mysql-import

100644100755
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111
* @license https://goo.gl/KPZ2qI MIT License
1212
* @copyright 2015-2018 Javanile
1313
*/
14-
require_once __DIR__.'/../../autoload.php';
14+
15+
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
16+
require_once __DIR__ . '/vendor/autoload.php';
17+
} elseif (file_exists(__DIR__ . '/../autoload.php')) {
18+
require_once __DIR__ . '/../autoload.php';
19+
} else {
20+
require_once __DIR__ . '/../../autoload.php';
21+
}
1522

1623
use Javanile\MysqlImport\MysqlImport;
1724

src/MysqlImport.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,10 @@ protected function tryUserAndPassword()
168168

169169
// first attempt avoid database delay
170170
if (!$this->connect($this->user, $this->password)) {
171-
sleep(10);
171+
for ($i = 0; $i < 10; $i++) {
172+
sleep(1);
173+
$this->progressBar($i, 10, "connecting...");
174+
}
172175
}
173176

174177
// second attempt real check
@@ -203,7 +206,11 @@ protected function tryRootPassword()
203206

204207
// first attempt avoid database delay
205208
if (!$this->connect('root', $this->rootPassword)) {
206-
sleep(10);
209+
for ($i = 0; $i < 10; $i++) {
210+
sleep(1);
211+
$this->progressBar($i, 10, "connecting...");
212+
}
213+
207214
}
208215

209216
// second attempt real check
@@ -236,7 +243,7 @@ protected function tryRootPassword()
236243
protected function connect($user, $password)
237244
{
238245
try {
239-
$this->link = mysqli_connect($this->host, $user, $password, '', $this->port);
246+
$this->link = @mysqli_connect($this->host, $user, $password, '', $this->port);
240247
} catch (\Throwable $e) {
241248
$log = 'ERROR: '.$e->getMessage()."\n".$e->getTraceAsString()."\n";
242249
file_put_contents('mysql-import.log', $log, FILE_APPEND);
@@ -392,4 +399,18 @@ public function getInfo()
392399
'database' => $this->database
393400
];
394401
}
402+
403+
/**
404+
* @param $done
405+
* @param $total
406+
* @param string $info
407+
* @param int $width
408+
* @return string
409+
*/
410+
public function progressBar($done, $total, $info="", $width=10)
411+
{
412+
$perc = round(($done * 100) / $total);
413+
$bar = round(($width * $perc) / 100);
414+
echo sprintf("%s%%[%s>%s] %s\r", $perc, str_repeat("=", $bar), str_repeat(" ", $width-$bar), $info);
415+
}
395416
}

tests/MysqlImportTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function testImportByDefault()
1212
$sqlFile = __DIR__.'/fixtures/database.sql';
1313
$message = "[mysql-import] database named 'database' successfully imported.";
1414

15-
$app = new MysqlImport(['MYSQL_ROOT_PASSWORD' => 'root'], [$sqlFile]);
15+
$app = new MysqlImport(['MYSQL_ROOT_PASSWORD' => 'secret'], [$sqlFile]);
1616
$this->assertEquals($message, $app->run());
1717
$app->drop('yes');
1818

@@ -26,7 +26,7 @@ public function testImportWithUserAndPassword()
2626
$sqlFile = __DIR__.'/fixtures/database.sql';
2727
$message = "[mysql-import] database named 'database' successfully imported.";
2828

29-
$app = new MysqlImport(['MYSQL_USER' => 'root', 'MYSQL_PASSWORD' => 'root'], [$sqlFile]);
29+
$app = new MysqlImport(['MYSQL_USER' => 'root', 'MYSQL_PASSWORD' => 'secret'], [$sqlFile]);
3030
$this->assertEquals($message, $app->run());
3131
$app->drop('yes');
3232

@@ -40,7 +40,7 @@ public function testDropAndCreateDatabase()
4040
$sqlFile = __DIR__.'/fixtures/database.sql';
4141
$message = "[mysql-import] database named 'database' successfully imported.";
4242

43-
$app = new MysqlImport(['MYSQL_USER' => 'root', 'MYSQL_PASSWORD' => 'root'], [$sqlFile]);
43+
$app = new MysqlImport(['MYSQL_USER' => 'root', 'MYSQL_PASSWORD' => 'secret'], [$sqlFile]);
4444

4545
$app->run();
4646
$app->drop();
@@ -55,13 +55,13 @@ public function testNotBlankDatabase()
5555
$sqlFile = __DIR__.'/fixtures/database.sql';
5656
$message = "[mysql-import] required blank database for import.";
5757

58-
$app = new MysqlImport(['MYSQL_ROOT_PASSWORD' => 'root'], [$sqlFile]);
58+
$app = new MysqlImport(['MYSQL_ROOT_PASSWORD' => 'secret'], [$sqlFile]);
5959
$app->run();
6060

6161
$app = new MysqlImport([], ['-proot', $sqlFile]);
6262
$this->assertEquals($message, $app->run());
6363

64-
$app = new MysqlImport(['DB_USER' => 'root', 'DB_PASSWORD' => 'root'], [$sqlFile]);
64+
$app = new MysqlImport(['DB_USER' => 'root', 'DB_PASSWORD' => 'secret'], [$sqlFile]);
6565
$this->assertEquals($message, $app->run());
6666

6767
$app->drop('yes');
@@ -96,7 +96,7 @@ public function testConnectionProblemWrongHost()
9696

9797
public function testMissingSqlFile()
9898
{
99-
$app = new MysqlImport(['MYSQL_ROOT_PASSWORD' => 'root'], []);
99+
$app = new MysqlImport(['MYSQL_ROOT_PASSWORD' => 'secret'], []);
100100
$this->assertEquals('[mysql-import] required sql file to import.', $app->run());
101101

102102
$sqlFile = __DIR__.'/fixtures/not_exists.sql';
@@ -129,7 +129,7 @@ public function testDefaultDatabaseName()
129129
{
130130
$sqlFile = __DIR__.'/fixtures/database.sql';
131131

132-
$app = new MysqlImport(['WORDPRESS_DB_PASSWORD' => 'root'], [$sqlFile]);
132+
$app = new MysqlImport(['WORDPRESS_DB_PASSWORD' => 'secret'], [$sqlFile]);
133133
$this->assertEquals([
134134
'state' => 'ready',
135135
'host' => 'mysql',
@@ -143,7 +143,7 @@ public function testSyntaxError()
143143
$sqlFile = __DIR__.'/fixtures/syntax_error.sql';
144144
$message = "[mysql-import] You have an error in your SQL syntax; check the manual that corresponds to";
145145

146-
$app = new MysqlImport(['MYSQL_ROOT_PASSWORD' => 'root'], [$sqlFile]);
146+
$app = new MysqlImport(['MYSQL_ROOT_PASSWORD' => 'secret'], [$sqlFile]);
147147
$this->assertStringStartsWith($message, $app->run());
148148
}
149149
}

0 commit comments

Comments
 (0)