Completed
Push — master ( f5a493...f6c233 )
by Joao
03:20
created

MySqlCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 1 Features 2
Metric Value
wmc 5
c 3
b 1
f 2
lcom 1
cbo 3
dl 0
loc 38
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareEnvironment() 0 8 1
A createDatabase() 0 7 1
A dropDatabase() 0 6 1
A createVersion() 0 5 1
A executeSql() 0 4 1
1
<?php
2
3
namespace ByJG\DbMigration\Commands;
4
5
use ByJG\AnyDataset\ConnectionManagement;
6
use ByJG\AnyDataset\Repository\DBDataset;
7
8
class MySqlCommand extends AbstractCommand
9
{
10
11
    public static function prepareEnvironment(ConnectionManagement $connection)
12
    {
13
        $database = $connection->getDatabase();
14
15
        $newConnection = new ConnectionManagement(str_replace("/$database", "/", $connection->getDbConnectionString()));
16
        $dbDataset = new DBDataset($newConnection->getDbConnectionString());
17
        $dbDataset->execSQL("CREATE SCHEMA IF NOT EXISTS `$database` DEFAULT CHARACTER SET utf8 ;");
18
    }
19
20
    public function createDatabase()
21
    {
22
        $database = $this->getDbDataset()->getConnectionManagement()->getDatabase();
23
24
        $this->getDbDataset()->execSQL("CREATE SCHEMA IF NOT EXISTS `$database` DEFAULT CHARACTER SET utf8 ;");
25
        $this->getDbDataset()->execSQL("USE `$database`");
26
    }
27
28
    public function dropDatabase()
29
    {
30
        $database = $this->getDbDataset()->getConnectionManagement()->getDatabase();
31
32
        $this->getDbDataset()->execSQL("drop database `$database`");
33
    }
34
35
    public function createVersion()
36
    {
37
        $this->getDbDataset()->execSQL('CREATE TABLE IF NOT EXISTS migration_version (version int)');
38
        $this->checkExistsVersion();
39
    }
40
41
    public function executeSql($sql)
42
    {
43
        $this->getDbDataset()->execSQL($sql);
44
    }
45
}
46