DataIntegrityTestInnoDB   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 78
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 51 4
A flushNow() 0 10 2
1
<?php
2
3
4
class DataIntegrityTestInnoDB extends BuildTask
5
{
6
7
    /**
8
     * standard SS variable
9
     * @var String
10
     */
11
    protected $title = "Convert all tables to InnoDB.";
12
13
    /**
14
     * standard SS variable
15
     * @var String
16
     */
17
    protected $description = "Converts table to innoDB. CAREFUL: replaces all tables in Database to innoDB - not just the Silverstripe ones.";
18
19
    public function run($request)
20
    {
21
        ini_set('max_execution_time', 3000);
22
        $tables = DB::query('SHOW TABLE STATUS WHERE ENGINE <>  \'InnoDB\'');
23
        foreach ($tables as $table) {
24
            $table = $table["Name"];
25
            DB::alteration_message("Updating $table to innoDB", "created");
26
            $this->flushNow();
27
            $indexRows = DB::query("SHOW INDEX FROM \"$table\" WHERE Index_type = 'FULLTEXT'");
28
            unset($done);
29
            $done = array();
30
            foreach ($indexRows as $indexRow) {
31
                $key = $indexRow["Key_name"];
32
                if (!isset($done[$key])) {
33
                    DB::alteration_message("Deleting INDEX $key in $table (FullText Index)", "deleted");
34
                    $this->flushNow();
35
                    DB::query("ALTER TABLE \"$table\" DROP INDEX $key;");
36
                    $done[$key] = $key;
37
                }
38
            }
39
            $sql = "ALTER TABLE \"$table\" ENGINE=INNODB";
40
            DB::query($sql);
41
        }
42
        //$rows = DB::query("SHOW GLOBAL STATUS LIKE  'Innodb_page_size'");
43
        $currentInnoDBSetting = DB::query("SELECT @@innodb_buffer_pool_size as V;")->Value();
44
        $innoDBBufferUsed = DB::query("
45
46
SELECT (PagesData*PageSize)/POWER(1024,3) DataGB FROM
47
(SELECT variable_value PagesData
48
FROM information_schema.global_status
49
WHERE variable_name='Innodb_buffer_pool_pages_data') A,
50
(SELECT variable_value PageSize
51
FROM information_schema.global_status
52
WHERE variable_name='Innodb_page_size') B;
53
54
		")->value();
55
        $innoBDBufferRecommended = DB::query(
56
            "
57
SELECT CEILING(Total_InnoDB_Bytes*1.6/POWER(1024,3)) RIBPS FROM
58
 (SELECT SUM(data_length+index_length) Total_InnoDB_Bytes
59
 FROM information_schema.tables WHERE engine='InnoDB') A;
60
"
61
        )->value();
62
        DB::alteration_message("<hr /><hr /><hr /><hr /><hr /><hr /><hr />COMPLETED
63
		<br />
64
		Please check your MYSQL innodb_buffer_pool_size setting.
65
		It is currently using ".round($innoDBBufferUsed, 3)."G,
66
		but it should be set to ".round($innoBDBufferRecommended, 3)."G.
67
		The current setting is: ".round($currentInnoDBSetting / (1042 * 1024* 1024))."G
68
		<hr /><hr /><hr /><hr /><hr /><hr /><hr />");
69
    }
70
71
    private function flushNow()
72
    {
73
        // check that buffer is actually set before flushing
74
        if (ob_get_length()) {
75
            @ob_flush();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
76
            @flush();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
77
            @ob_end_flush();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
78
        }
79
        @ob_start();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
80
    }
81
}
82