-
Notifications
You must be signed in to change notification settings - Fork 248
Expand file tree
/
Copy pathCreateTestDBs.php
More file actions
66 lines (55 loc) · 1.74 KB
/
CreateTestDBs.php
File metadata and controls
66 lines (55 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace ProcessMaker\Console\Commands;
use Exception;
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
class CreateTestDBs extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'processmaker:create-test-dbs';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Clone the test database for parallel test runs';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$processes = env('PARALLEL_TEST_PROCESSES');
if (!$processes) {
throw new Exception('PARALLEL_TEST_PROCESSES not set');
}
$dbConnectionArgs = '-h ' . env('DB_HOSTNAME') . ' -P ' . env('DB_PORT') . ' -u ' . env('DB_USERNAME') . " -p'" . env('DB_PASSWORD') . "'";
$file = tempnam(sys_get_temp_dir(), 'dump');
$cmd = "mysqldump $dbConnectionArgs " . env('DB_DATABASE') . " > $file";
(new Process($cmd))->mustRun();
foreach (range(1, $processes) as $process) {
$database = "test_$process";
$this->info("Creating database $database");
$cmd = "mysql $dbConnectionArgs -e 'DROP DATABASE IF EXISTS $database'";
(new Process($cmd))->mustRun();
$cmd = "mysql $dbConnectionArgs -e 'CREATE DATABASE $database'";
(new Process($cmd))->mustRun();
$cmd = "mysql $dbConnectionArgs $database < $file";
(new Process($cmd))->mustRun();
}
}
}