Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Db/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,7 @@ public function getColumnTypes(): array
'double',
'datetime',
'timestamp',
'timestampfractional',
'time',
'date',
'blob',
Expand Down
19 changes: 19 additions & 0 deletions src/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,25 @@ public function getColumnTypes(): array
return $types;
}

/**
* Get the default encoding for the current database.
*
* @return string The default encoding
*/
public function getDefaultCollation(): string
{
$encodingRequest = 'SELECT DEFAULT_COLLATION_NAME
FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = :dbname';

$connection = $this->getConnection();
$connectionConfig = $connection->config();

$statement = $connection->execute($encodingRequest, ['dbname' => $connectionConfig['database']]);
$row = $statement->fetch('assoc');

return $row['DEFAULT_COLLATION_NAME'] ?? '';
}

/**
* Whether the server has a native uuid type.
* (MariaDB 10.7.0+)
Expand Down
18 changes: 5 additions & 13 deletions src/Db/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Migrations\Db\Action\RenameColumn;
use Migrations\Db\Action\RenameTable;
use Migrations\Db\Adapter\AdapterInterface;
use Migrations\Db\Adapter\MysqlAdapter;
use Migrations\Db\Plan\Intent;
use Migrations\Db\Plan\Plan;
use Migrations\Db\Table\Column;
Expand Down Expand Up @@ -637,19 +638,10 @@ public function create(): void
}

$adapter = $this->getAdapter();
if ($adapter->getAdapterType() === 'mysql' && empty($options['collation'])) {
// TODO this should be a method on the MySQL adapter.
// It could be a hook method on the adapter?
$encodingRequest = 'SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME
FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = :dbname';

$connection = $adapter->getConnection();
$connectionConfig = $connection->config();

$statement = $connection->execute($encodingRequest, ['dbname' => $connectionConfig['database']]);
$defaultEncoding = $statement->fetch('assoc');
if (!empty($defaultEncoding['DEFAULT_COLLATION_NAME'])) {
$options['collation'] = $defaultEncoding['DEFAULT_COLLATION_NAME'];
if ($adapter instanceof MysqlAdapter && empty($options['collation'])) {
$collation = $adapter->getDefaultCollation();
if ($collation) {
$options['collation'] = $collation;
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/Util/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ public static function getVersionFromFileName(string $fileName): int
*/
public static function mapClassNameToFileName(string $className): string
{
// TODO it would be nice to replace this with Inflector::underscore
// but it will break compatibility for little end user gain.
$snake = function ($matches) {
return '_' . strtolower($matches[0]);
};
Expand Down
9 changes: 2 additions & 7 deletions src/View/Helper/MigrationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,6 @@ public function column(TableSchemaInterface $tableSchema, string $column): array
{
$columnType = $tableSchema->getColumnType($column);

// TODO Remove this when we align with cakephp/database more.
// Phinx doesn't understand timestampfractional or datetimefractional types
if ($columnType === 'timestampfractional' || $columnType === 'datetimefractional') {
$columnType = 'timestamp';
}

return [
'columnType' => $columnType,
'options' => $this->attributes($tableSchema, $column),
Expand Down Expand Up @@ -414,12 +408,13 @@ public function getColumnOption(array $options): array
}

if (($isMysql || $isSqlserver) && !empty($columnOptions['collate'])) {
// TODO fix this when migrations is aligned with cakephp/database
// TODO deprecate this in 5.x
// Change keys due to Phinx using different naming for the collation
$columnOptions['collation'] = $columnOptions['collate'];
unset($columnOptions['collate']);
}

// TODO deprecate precision/scale and align with cakephp/database in 5.x
// TODO this can be cleaned up when we stop using phinx data structures for column definitions
if (!isset($columnOptions['precision']) || $columnOptions['precision'] == null) {
unset($columnOptions['precision']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function testSnapshotGenerateOnly()
}

/**
* Test baking a snapshot with the phinx auto-id feature disabled
* Test baking a snapshot with the auto-id feature disabled
*
* @return void
*/
Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1819,8 +1819,8 @@ public function testDumpCreateTable()

/**
* Creates the table "table1".
* Then sets phinx to dry run mode and inserts a record.
* Asserts that phinx outputs the insert statement and doesn't insert a record.
* Then enables dry run mode and inserts a record.
* Asserts that the insert statement is output and doesn't insert a record.
*/
public function testDumpInsert()
{
Expand Down Expand Up @@ -1863,8 +1863,8 @@ public function testDumpInsert()

/**
* Creates the table "table1".
* Then sets phinx to dry run mode and inserts some records.
* Asserts that phinx outputs the insert statement and doesn't insert any record.
* Then enables dry run mode and inserts some records.
* Asserts that output contains the insert statement and doesn't insert any record.
*/
public function testDumpBulkinsert()
{
Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase/Db/Adapter/PostgresAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2504,8 +2504,8 @@ public function testDumpCreateTableWithSchema()

/**
* Creates the table "table1".
* Then sets phinx to dry run mode and inserts a record.
* Asserts that phinx outputs the insert statement and doesn't insert a record.
* Then enables dry run mode and inserts a record.
* Asserts that output contains the insert statement and doesn't insert a record.
*/
public function testDumpInsert()
{
Expand Down Expand Up @@ -2559,8 +2559,8 @@ public function testDumpInsert()

/**
* Creates the table "table1".
* Then sets phinx to dry run mode and inserts some records.
* Asserts that phinx outputs the insert statement and doesn't insert any record.
* Then enables dry run mode and inserts some records.
* Asserts that output contains the insert statement and doesn't insert any record.
*/
public function testDumpBulkinsert()
{
Expand Down
6 changes: 6 additions & 0 deletions tests/TestCase/View/Helper/MigrationHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ public function setUp(): void
'comment' => null,
'precision' => 6,
];
$this->types = [
'timestamp' => 'timestampfractional',
];
}

if (getenv('DB') === 'sqlserver') {
Expand All @@ -120,6 +123,9 @@ public function setUp(): void
'comment' => null,
'precision' => 7,
];
$this->types = [
'timestamp' => 'datetimefractional',
];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ public function up(): void
'limit' => null,
'null' => true,
])
->addColumn('created', 'timestamp', [
->addColumn('created', 'timestampfractional', [
'default' => null,
'limit' => null,
'null' => true,
'precision' => 6,
'scale' => 6,
])
->addColumn('modified', 'timestamp', [
->addColumn('modified', 'timestampfractional', [
'default' => null,
'limit' => null,
'null' => true,
Expand Down Expand Up @@ -99,14 +99,14 @@ public function up(): void
'limit' => 100,
'null' => true,
])
->addColumn('created', 'timestamp', [
->addColumn('created', 'timestampfractional', [
'default' => null,
'limit' => null,
'null' => true,
'precision' => 6,
'scale' => 6,
])
->addColumn('modified', 'timestamp', [
->addColumn('modified', 'timestampfractional', [
'default' => null,
'limit' => null,
'null' => true,
Expand Down Expand Up @@ -229,14 +229,14 @@ public function up(): void
'limit' => 10,
'null' => true,
])
->addColumn('created', 'timestamp', [
->addColumn('created', 'timestampfractional', [
'default' => null,
'limit' => null,
'null' => true,
'precision' => 6,
'scale' => 6,
])
->addColumn('modified', 'timestamp', [
->addColumn('modified', 'timestampfractional', [
'default' => null,
'limit' => null,
'null' => true,
Expand Down Expand Up @@ -304,7 +304,7 @@ public function up(): void
'limit' => null,
'null' => true,
])
->addColumn('highlighted_time', 'timestamp', [
->addColumn('highlighted_time', 'timestampfractional', [
'default' => null,
'limit' => null,
'null' => true,
Expand Down Expand Up @@ -349,14 +349,14 @@ public function up(): void
'limit' => 256,
'null' => true,
])
->addColumn('created', 'timestamp', [
->addColumn('created', 'timestampfractional', [
'default' => null,
'limit' => null,
'null' => true,
'precision' => 6,
'scale' => 6,
])
->addColumn('updated', 'timestamp', [
->addColumn('updated', 'timestampfractional', [
'default' => null,
'limit' => null,
'null' => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ public function up(): void
'limit' => null,
'null' => true,
])
->addColumn('created', 'timestamp', [
->addColumn('created', 'timestampfractional', [
'default' => null,
'limit' => null,
'null' => true,
'precision' => 6,
'scale' => 6,
])
->addColumn('modified', 'timestamp', [
->addColumn('modified', 'timestampfractional', [
'default' => null,
'limit' => null,
'null' => true,
Expand Down Expand Up @@ -83,14 +83,14 @@ public function up(): void
'limit' => 100,
'null' => true,
])
->addColumn('created', 'timestamp', [
->addColumn('created', 'timestampfractional', [
'default' => null,
'limit' => null,
'null' => true,
'precision' => 6,
'scale' => 6,
])
->addColumn('modified', 'timestamp', [
->addColumn('modified', 'timestampfractional', [
'default' => null,
'limit' => null,
'null' => true,
Expand Down Expand Up @@ -184,14 +184,14 @@ public function up(): void
'limit' => 10,
'null' => true,
])
->addColumn('created', 'timestamp', [
->addColumn('created', 'timestampfractional', [
'default' => null,
'limit' => null,
'null' => true,
'precision' => 6,
'scale' => 6,
])
->addColumn('modified', 'timestamp', [
->addColumn('modified', 'timestampfractional', [
'default' => null,
'limit' => null,
'null' => true,
Expand Down Expand Up @@ -251,7 +251,7 @@ public function up(): void
'limit' => null,
'null' => true,
])
->addColumn('highlighted_time', 'timestamp', [
->addColumn('highlighted_time', 'timestampfractional', [
'default' => null,
'limit' => null,
'null' => true,
Expand Down Expand Up @@ -289,14 +289,14 @@ public function up(): void
'limit' => 256,
'null' => true,
])
->addColumn('created', 'timestamp', [
->addColumn('created', 'timestampfractional', [
'default' => null,
'limit' => null,
'null' => true,
'precision' => 6,
'scale' => 6,
])
->addColumn('updated', 'timestamp', [
->addColumn('updated', 'timestampfractional', [
'default' => null,
'limit' => null,
'null' => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ public function up(): void
'limit' => null,
'null' => true,
])
->addColumn('created', 'timestamp', [
->addColumn('created', 'timestampfractional', [
'default' => null,
'limit' => null,
'null' => true,
'precision' => 6,
'scale' => 6,
])
->addColumn('modified', 'timestamp', [
->addColumn('modified', 'timestampfractional', [
'default' => null,
'limit' => null,
'null' => true,
Expand Down Expand Up @@ -83,14 +83,14 @@ public function up(): void
'limit' => 100,
'null' => true,
])
->addColumn('created', 'timestamp', [
->addColumn('created', 'timestampfractional', [
'default' => null,
'limit' => null,
'null' => true,
'precision' => 6,
'scale' => 6,
])
->addColumn('modified', 'timestamp', [
->addColumn('modified', 'timestampfractional', [
'default' => null,
'limit' => null,
'null' => true,
Expand Down
Loading
Loading