Skip to content

Commit e4b3e46

Browse files
authored
Merge pull request #27 from xepozz/file-over-connection
Added ability to execute raw SQL over already opened database connection
2 parents 0fc2562 + f7af4f5 commit e4b3e46

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

db/mysql/FileMigration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
/**
18-
* Class MysqlFileMigration
18+
* Class FileMigration
1919
* @package common\components
2020
* @author Tobias Munk <[email protected]>
2121
*/
@@ -79,4 +79,4 @@ public function down()
7979
return false;
8080
}
8181

82-
}
82+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* @link http://www.diemeisterei.de/
4+
* @copyright Copyright (c) 2014 diemeisterei GmbH, Stuttgart
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace dmstr\db\mysql;
11+
12+
use mikehaertl\shellcommand\Command;
13+
use yii\base\Exception;
14+
use yii\db\Migration;
15+
16+
17+
/**
18+
* Class FileOverConnectionMigration
19+
* @package common\components
20+
* @author Dmitry Derepko <[email protected]>
21+
*/
22+
class FileOverConnectionMigration extends Migration
23+
{
24+
public $file = null;
25+
public $mysqlExecutable = 'mysql';
26+
27+
public function init()
28+
{
29+
parent::init();
30+
31+
if ($this->file === null) {
32+
$reflection = new \ReflectionClass($this);
33+
$this->file = str_replace('.php', '.sql', $reflection->getFileName());
34+
} else {
35+
$reflection = new \ReflectionClass($this);
36+
$this->file = dirname($reflection->getFileName()).DIRECTORY_SEPARATOR.$this->file;
37+
}
38+
39+
if (!is_file($this->file)) {
40+
throw new Exception("File {$this->file} not found");
41+
}
42+
}
43+
44+
public function up()
45+
{
46+
$sql = file_get_contents($this->file);
47+
48+
$this->db->createCommand($sql)->execute();
49+
}
50+
51+
public function down()
52+
{
53+
echo get_class($this) . " cannot be reverted.\n";
54+
return false;
55+
}
56+
}

0 commit comments

Comments
 (0)