File size: 3,811 Bytes
bf8c0ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Tests\Integration\Db\Schema;

use Piwik\Config\DatabaseConfig;
use Piwik\Db;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

class MysqlTest extends IntegrationTestCase
{
    /**
     * @dataProvider getIsOptimizeInnoDBTestData
     */
    public function testIsOptimizeInnoDBSupportedReturnsCorrectResult($version, $expectedResult)
    {
        $schema = $this->getMockBuilder(Db\Schema\Mysql::class)->onlyMethods(['getVersion'])->getMock();
        $schema->method('getVersion')->willReturn($version);
        $this->assertEquals($expectedResult, $schema->isOptimizeInnoDBSupported());
    }

    public function getIsOptimizeInnoDBTestData()
    {
        return array(
            array("10.0.17-MariaDB-1~trusty", false),
            array("10.1.1-MariaDB-1~trusty", true),
            array("10.2.0-MariaDB-1~trusty", true),
            array("10.6.19-0ubuntu0.14.04.1", false),
            array("8.0.11-TiDB-v8.1.0", false),
            array("", false),
            array("0", false),
            array("slkdf(@*#lkesjfMariaDB", false),
            array("slkdfjq3rujlkv", false),
        );
    }

    public function testOptimize()
    {
        if (DatabaseConfig::getConfigValue('schema') !== 'Mysql') {
            self::markTestSkipped('Mysql is not available');
        }

        // create two myisam tables
        Db::exec("CREATE TABLE table1 (a INT) ENGINE=MYISAM");
        Db::exec("CREATE TABLE table2 (b INT) ENGINE=MYISAM");

        // create two innodb tables
        Db::exec("CREATE TABLE table3 (c INT) ENGINE=InnoDB");
        Db::exec("CREATE TABLE table4 (d INT) ENGINE=InnoDB");

        $schema = Db\Schema::getInstance();

        // make sure optimizing myisam tables works
        $this->assertTrue($schema->optimizeTables(['table1', 'table2']));

        // make sure optimizing both myisam & innodb results in optimizations
        $this->assertTrue($schema->optimizeTables(['table1', 'table2', 'table3', 'table4']));

        // innodb should be skipped by default
        $this->assertFalse($schema->optimizeTables(['table3', 'table4']));

        // should optimize when forced
        $this->assertTrue($schema->optimizeTables(['table3', 'table4'], true));
    }

    /**
     * @dataProvider getTableCreateOptionsTestData
     */
    public function testTableCreateOptions(array $optionOverrides, string $expected): void
    {
        if (DatabaseConfig::getConfigValue('schema') !== 'Mysql') {
            self::markTestSkipped('Mysql is not available');
        }

        foreach ($optionOverrides as $name => $value) {
            DatabaseConfig::setConfigValue($name, $value);
        }

        $schema = Db\Schema::getInstance();

        self::assertSame($expected, $schema->getTableCreateOptions());
    }

    public function getTableCreateOptionsTestData(): iterable
    {
        yield 'default charset, empty collation' => [
            ['collation' => ''],
            'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC',
        ];

        yield 'override charset, empty collation' => [
            ['charset' => 'utf8mb3', 'collation' => ''],
            'ENGINE=InnoDB DEFAULT CHARSET=utf8mb3',
        ];

        yield 'default charset, override collation' => [
            ['collation' => 'utf8mb4_swedish_ci'],
            'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_swedish_ci ROW_FORMAT=DYNAMIC',
        ];

        yield 'override charset and collation' => [
            ['charset' => 'utf8mb3', 'collation' => 'utf8mb3_general_ci'],
            'ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci',
        ];
    }
}