Passed
Push — master ( c5d372...0a085e )
by Alexander
01:31
created

SchemaTest::constraintsProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 17
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql\Tests;
6
7
use Yiisoft\Db\Expressions\Expression;
8
use Yiisoft\Db\Mysql\ColumnSchema;
9
use Yiisoft\Db\Mysql\Schema;
10
use Yiisoft\Db\Tests\SchemaTest as AbstractSchemaTest;
11
12
class SchemaTest extends AbstractSchemaTest
13
{
14
    protected ?string $driverName = 'mysql';
15
16
    public function testLoadDefaultDatetimeColumn()
17
    {
18
        if (!version_compare($this->getConnection()->getPDO()->getAttribute(\PDO::ATTR_SERVER_VERSION), '5.6', '>=')) {
19
            $this->markTestSkipped('Default datetime columns are supported since MySQL 5.6.');
20
        }
21
        $sql = <<<SQL
22
CREATE TABLE  IF NOT EXISTS `datetime_test`  (
23
  `id` int(11) NOT NULL AUTO_INCREMENT,
24
  `dt` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
25
  `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
26
  PRIMARY KEY (`id`)
27
) ENGINE=InnoDB DEFAULT CHARSET=utf8
28
SQL;
29
30
        $this->getConnection()->createCommand($sql)->execute();
31
32
        $schema = $this->getConnection()->getTableSchema('datetime_test');
33
34
        $dt = $schema->columns['dt'];
35
36
        $this->assertInstanceOf(Expression::class, $dt->defaultValue);
37
        $this->assertEquals('CURRENT_TIMESTAMP', (string)$dt->defaultValue);
38
    }
39
40
    public function testDefaultDatetimeColumnWithMicrosecs(): void
41
    {
42
        if (!version_compare($this->getConnection()->getPDO()->getAttribute(\PDO::ATTR_SERVER_VERSION), '5.6.4', '>=')) {
43
            $this->markTestSkipped('CURRENT_TIMESTAMP with microseconds as default column value is supported since MySQL 5.6.4.');
44
        }
45
        $sql = <<<SQL
46
CREATE TABLE  IF NOT EXISTS `current_timestamp_test`  (
47
  `dt` datetime(2) NOT NULL DEFAULT CURRENT_TIMESTAMP(2),
48
  `ts` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3)
49
) ENGINE=InnoDB DEFAULT CHARSET=utf8
50
SQL;
51
52
        $this->getConnection()->createCommand($sql)->execute();
53
54
        $schema = $this->getConnection()->getTableSchema('current_timestamp_test');
55
56
        $dt = $schema->columns['dt'];
57
        $this->assertInstanceOf(Expression::class, $dt->defaultValue);
58
        $this->assertEquals('CURRENT_TIMESTAMP(2)', (string)$dt->defaultValue);
59
60
        $ts = $schema->columns['ts'];
61
        $this->assertInstanceOf(Expression::class, $ts->defaultValue);
62
        $this->assertEquals('CURRENT_TIMESTAMP(3)', (string)$ts->defaultValue);
63
    }
64
65
    public function testGetSchemaNames()
66
    {
67
        $this->markTestSkipped('Schemas are not supported in MySQL.');
68
    }
69
70
    public function constraintsProvider()
71
    {
72
        $result = parent::constraintsProvider();
73
74
        $result['1: check'][2] = false;
75
76
        $result['2: primary key'][2]->setName(null);
77
        $result['2: check'][2] = false;
78
79
        // Work aroung bug in MySQL 5.1 - it creates only this table in lowercase. O_o
80
        //$result['3: foreign key'][2][0]->setForeignTableName(new AnyCaseValue('T_constraints_2'));
81
82
        $result['3: check'][2] = false;
83
84
        $result['4: check'][2] = false;
85
86
        return $result;
87
    }
88
89
    /**
90
     * When displayed in the INFORMATION_SCHEMA.COLUMNS table, a default CURRENT TIMESTAMP is displayed
91
     * as CURRENT_TIMESTAMP up until MariaDB 10.2.2, and as current_timestamp() from MariaDB 10.2.3.
92
     *
93
     * @see https://mariadb.com/kb/en/library/now/#description
94
     * @see https://github.com/yiisoft/yii2/issues/15167
95
     */
96
    public function testAlternativeDisplayOfDefaultCurrentTimestampInMariaDB(): void
97
    {
98
        /**
99
         * We do not have a real database MariaDB >= 10.2.3 for tests, so we emulate the information that database
100
         * returns in response to the query `SHOW FULL COLUMNS FROM ...`
101
         */
102
        $schema = new Schema($this->getConnection());
103
104
        $column = $this->invokeMethod($schema, 'loadColumnSchema', [[
105
            'field' => 'emulated_MariaDB_field',
106
            'type' => 'timestamp',
107
            'collation' => null,
108
            'null' => 'NO',
109
            'key' => '',
110
            'default' => 'current_timestamp()',
111
            'extra' => '',
112
            'privileges' => 'select,insert,update,references',
113
            'comment' => '',
114
        ]]);
115
116
        $this->assertInstanceOf(ColumnSchema::class, $column);
117
        $this->assertInstanceOf(Expression::class, $column->defaultValue);
118
        $this->assertEquals('CURRENT_TIMESTAMP', $column->defaultValue);
119
    }
120
}
121