Skip to content

Commit

Permalink
Test fixes for mariadb
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosp committed Dec 11, 2024
1 parent 2577870 commit 0c70293
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
22 changes: 17 additions & 5 deletions tests/TestCase/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ public function testCreateTableAndInheritDefaultCollation()
->save();
$this->assertTrue($adapter->hasTable('table_with_default_collation'));
$row = $adapter->fetchRow(sprintf("SHOW TABLE STATUS WHERE Name = '%s'", 'table_with_default_collation'));
$this->assertEquals('utf8mb4_0900_ai_ci', $row['Collation']);
$this->assertContains($row['Collation'], ['utf8mb4_0900_ai_ci', 'utf8mb4_unicode_520_ci']);
}

public function testCreateTableWithLatin1Collate()
Expand Down Expand Up @@ -2181,8 +2181,14 @@ public function testDumpCreateTable()
->addColumn('column3', 'string', ['default' => 'test', 'null' => false])
->save();

$expectedOutput = <<<'OUTPUT'
CREATE TABLE `table1` (`id` INT(11) unsigned NOT NULL AUTO_INCREMENT, `column1` VARCHAR(255) NOT NULL, `column2` INT(11) NULL, `column3` VARCHAR(255) NOT NULL DEFAULT 'test', PRIMARY KEY (`id`)) ENGINE = InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
if ($this->usingMariaDbWithUuid()) {
$collation = 'utf8mb4_unicode_520_ci';
} else {
$collation = 'utf8mb4_0900_ai_ci';
}

$expectedOutput = <<<OUTPUT
CREATE TABLE `table1` (`id` INT(11) unsigned NOT NULL AUTO_INCREMENT, `column1` VARCHAR(255) NOT NULL, `column2` INT(11) NULL, `column3` VARCHAR(255) NOT NULL DEFAULT 'test', PRIMARY KEY (`id`)) ENGINE = InnoDB CHARACTER SET utf8mb4 COLLATE {$collation};
OUTPUT;
$actualOutput = join("\n", $this->out->messages());
$this->assertStringContainsString($expectedOutput, $actualOutput, 'Passing the --dry-run option does not dump create table query to the output');
Expand Down Expand Up @@ -2286,8 +2292,14 @@ public function testDumpCreateTableAndThenInsert()
'column2' => 1,
])->save();

$expectedOutput = <<<'OUTPUT'
CREATE TABLE `table1` (`column1` VARCHAR(255) NOT NULL, `column2` INT(11) NULL, PRIMARY KEY (`column1`)) ENGINE = InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
if ($this->usingMariaDbWithUuid()) {
$collation = 'utf8mb4_unicode_520_ci';
} else {
$collation = 'utf8mb4_0900_ai_ci';
}

$expectedOutput = <<<OUTPUT
CREATE TABLE `table1` (`column1` VARCHAR(255) NOT NULL, `column2` INT(11) NULL, PRIMARY KEY (`column1`)) ENGINE = InnoDB CHARACTER SET utf8mb4 COLLATE {$collation};
INSERT INTO `table1` (`column1`, `column2`) VALUES ('id1', 1);
OUTPUT;
$actualOutput = join("\n", $this->out->messages());
Expand Down
6 changes: 5 additions & 1 deletion tests/TestCase/MigrationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Database\Driver\Mysql;
use Cake\Database\Driver\Sqlserver;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;
Expand Down Expand Up @@ -232,7 +233,10 @@ public function testMigrateAndRollback($backend)
$this->assertEquals($expected, $columns);
$createdColumn = $storesTable->getSchema()->getColumn('created');
$expected = 'CURRENT_TIMESTAMP';
if ($this->Connection->getDriver() instanceof Sqlserver) {
$driver = $this->Connection->getDriver();
if ($driver instanceof Mysql && $driver->isMariadb()) {
$expected = 'current_timestamp()';
} elseif ($driver instanceof Sqlserver) {
$expected = 'getdate()';
}
$this->assertEquals($expected, $createdColumn['default']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

class TheDiffAddRemoveMysql extends BaseMigration
{
private function isMariaDB(): bool
{
$version = $this->adapter->getConnection()->getDriver()->version();

return version_compare($version, '10.0', '>=');
}

/**
* Up Method.
*
Expand All @@ -29,7 +36,7 @@ public function up(): void
$this->table('articles')
->addColumn('the_text', 'text', [
'after' => 'title',
'collation' => 'utf8mb4_0900_ai_ci',
'collation' => $this->isMariaDB() ? 'utf8mb4_unicode_520_ci' : 'utf8mb4_0900_ai_ci',
'default' => null,
'length' => null,
'null' => false,
Expand Down

0 comments on commit 0c70293

Please sign in to comment.