1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\DbMigration\Database; |
4
|
|
|
|
5
|
|
|
use ByJG\AnyDataset\Db\Factory; |
6
|
|
|
use ByJG\Util\Uri; |
7
|
|
|
use Psr\Http\Message\UriInterface; |
8
|
|
|
|
9
|
|
|
class MySqlDatabase extends AbstractDatabase |
10
|
|
|
{ |
11
|
|
|
public static function schema() |
12
|
|
|
{ |
13
|
|
|
return ['mysql', 'mariadb']; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public static function prepareEnvironment(UriInterface $uri) |
17
|
|
|
{ |
18
|
|
|
$database = preg_replace('~^/~', '', $uri->getPath()); |
19
|
|
|
|
20
|
|
|
$customUri = new Uri($uri->__toString()); |
21
|
|
|
|
22
|
|
|
$dbDriver = Factory::getDbRelationalInstance($customUri->withPath('/')->__toString()); |
23
|
|
|
$dbDriver->execute("CREATE SCHEMA IF NOT EXISTS `$database` DEFAULT CHARACTER SET utf8 ;"); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function createDatabase() |
27
|
|
|
{ |
28
|
|
|
$database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath()); |
29
|
|
|
|
30
|
|
|
$this->getDbDriver()->execute("CREATE SCHEMA IF NOT EXISTS `$database` DEFAULT CHARACTER SET utf8 ;"); |
31
|
|
|
$this->getDbDriver()->execute("USE `$database`"); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function dropDatabase() |
35
|
|
|
{ |
36
|
|
|
$database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath()); |
37
|
|
|
|
38
|
|
|
$this->getDbDriver()->execute("drop database `$database`"); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException |
43
|
|
|
* @throws \ByJG\DbMigration\Exception\OldVersionSchemaException |
44
|
|
|
*/ |
45
|
|
|
public function createVersion() |
46
|
|
|
{ |
47
|
|
|
$this->getDbDriver()->execute('CREATE TABLE IF NOT EXISTS ' . $this->getMigrationTable() . ' (version int, status varchar(20), PRIMARY KEY (version))'); |
48
|
|
|
$this->checkExistsVersion(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function executeSql($sql) |
52
|
|
|
{ |
53
|
|
|
$this->getDbDriver()->execute($sql); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|