-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathFileMigration.php
executable file
·83 lines (67 loc) · 2.22 KB
/
FileMigration.php
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
* @link http://www.diemeisterei.de/
* @copyright Copyright (c) 2014 diemeisterei GmbH, Stuttgart
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace dmstr\db\mysql;
use dmstr\db\helper\CliHelper;
use mikehaertl\shellcommand\Command;
use yii\base\Exception;
use yii\db\Migration;
/**
* Class FileMigration
* @package common\components
* @author Tobias Munk <[email protected]>
*/
class FileMigration extends Migration
{
public $file = null;
public $mysqlExecutable = 'mysql';
public $mysqlOptions = [];
public function init()
{
parent::init();
if ($this->file === null) {
$reflection = new \ReflectionClass($this);
$this->file = str_replace('.php', '.sql', $reflection->getFileName());
} else {
$reflection = new \ReflectionClass($this);
$this->file = dirname($reflection->getFileName()).DIRECTORY_SEPARATOR.$this->file;
}
if (!is_file($this->file)) {
throw new Exception("File {$this->file} not found");
}
}
public function up()
{
$dsnOpts = CliHelper::getMysqlOptsFromDsn($this->db);
$command = new Command($this->mysqlExecutable);
$command->addArg('-h', $dsnOpts['host']);
$command->addArg('-P', $dsnOpts['port']);
$command->addArg('-u', $this->db->username);
$command->addArg('--password=', $this->db->password);
if ($this->db->charset) {
$command->addArg('--default-character-set=', $this->db->charset);
}
foreach (CliHelper::getMysqlCliArgsFromPdo($this->db) as $opt => $value) {
$command->addArg($opt, $value);
}
$cmd = $command->getExecCommand()." \"{$dsnOpts['db']}\" < \"{$this->file}\"";
#echo " ".$cmd . "\n"; // TODO echo only with --verbose
exec($cmd, $output, $return);
if ($return !== 0) {
//var_dump($output, $return);
return false;
} else {
return true;
}
}
public function down()
{
echo $this::className() . " cannot be reverted.\n";
return false;
}
}