diff --git a/src/Db/Adapter/AbstractAdapter.php b/src/Db/Adapter/AbstractAdapter.php index 8a7815269..2a78430ac 100644 --- a/src/Db/Adapter/AbstractAdapter.php +++ b/src/Db/Adapter/AbstractAdapter.php @@ -982,6 +982,7 @@ public function getColumnTypes(): array 'double', 'datetime', 'timestamp', + 'timestampfractional', 'time', 'date', 'blob', diff --git a/src/Db/Adapter/MysqlAdapter.php b/src/Db/Adapter/MysqlAdapter.php index bd3de2d4d..9516eca56 100644 --- a/src/Db/Adapter/MysqlAdapter.php +++ b/src/Db/Adapter/MysqlAdapter.php @@ -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+) diff --git a/src/Db/Table.php b/src/Db/Table.php index 358688c03..324eb96c6 100644 --- a/src/Db/Table.php +++ b/src/Db/Table.php @@ -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; @@ -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; } } diff --git a/src/Util/Util.php b/src/Util/Util.php index a0435b41e..6b87a4fb0 100644 --- a/src/Util/Util.php +++ b/src/Util/Util.php @@ -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]); }; diff --git a/src/View/Helper/MigrationHelper.php b/src/View/Helper/MigrationHelper.php index 128c14fae..0ab2b4f44 100644 --- a/src/View/Helper/MigrationHelper.php +++ b/src/View/Helper/MigrationHelper.php @@ -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), @@ -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']); diff --git a/tests/TestCase/Command/BakeMigrationSnapshotCommandTest.php b/tests/TestCase/Command/BakeMigrationSnapshotCommandTest.php index a7bafae74..016255a90 100644 --- a/tests/TestCase/Command/BakeMigrationSnapshotCommandTest.php +++ b/tests/TestCase/Command/BakeMigrationSnapshotCommandTest.php @@ -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 */ diff --git a/tests/TestCase/Db/Adapter/MysqlAdapterTest.php b/tests/TestCase/Db/Adapter/MysqlAdapterTest.php index 531b3763a..7ed97f6e4 100644 --- a/tests/TestCase/Db/Adapter/MysqlAdapterTest.php +++ b/tests/TestCase/Db/Adapter/MysqlAdapterTest.php @@ -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() { @@ -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() { diff --git a/tests/TestCase/Db/Adapter/PostgresAdapterTest.php b/tests/TestCase/Db/Adapter/PostgresAdapterTest.php index 3ca14c553..1fc8f3d17 100644 --- a/tests/TestCase/Db/Adapter/PostgresAdapterTest.php +++ b/tests/TestCase/Db/Adapter/PostgresAdapterTest.php @@ -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() { @@ -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() { diff --git a/tests/TestCase/View/Helper/MigrationHelperTest.php b/tests/TestCase/View/Helper/MigrationHelperTest.php index 79aa3554e..0b26b9347 100644 --- a/tests/TestCase/View/Helper/MigrationHelperTest.php +++ b/tests/TestCase/View/Helper/MigrationHelperTest.php @@ -110,6 +110,9 @@ public function setUp(): void 'comment' => null, 'precision' => 6, ]; + $this->types = [ + 'timestamp' => 'timestampfractional', + ]; } if (getenv('DB') === 'sqlserver') { @@ -120,6 +123,9 @@ public function setUp(): void 'comment' => null, 'precision' => 7, ]; + $this->types = [ + 'timestamp' => 'datetimefractional', + ]; } } diff --git a/tests/comparisons/Migration/pgsql/test_snapshot_auto_id_disabled_pgsql.php b/tests/comparisons/Migration/pgsql/test_snapshot_auto_id_disabled_pgsql.php index 6ee447f1c..eca100cc3 100644 --- a/tests/comparisons/Migration/pgsql/test_snapshot_auto_id_disabled_pgsql.php +++ b/tests/comparisons/Migration/pgsql/test_snapshot_auto_id_disabled_pgsql.php @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, diff --git a/tests/comparisons/Migration/pgsql/test_snapshot_not_empty_pgsql.php b/tests/comparisons/Migration/pgsql/test_snapshot_not_empty_pgsql.php index 5d0d5f81a..e297c18ce 100644 --- a/tests/comparisons/Migration/pgsql/test_snapshot_not_empty_pgsql.php +++ b/tests/comparisons/Migration/pgsql/test_snapshot_not_empty_pgsql.php @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, diff --git a/tests/comparisons/Migration/pgsql/test_snapshot_plugin_blog_pgsql.php b/tests/comparisons/Migration/pgsql/test_snapshot_plugin_blog_pgsql.php index 271e40fba..7b850c910 100644 --- a/tests/comparisons/Migration/pgsql/test_snapshot_plugin_blog_pgsql.php +++ b/tests/comparisons/Migration/pgsql/test_snapshot_plugin_blog_pgsql.php @@ -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, @@ -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, diff --git a/tests/comparisons/Migration/sqlserver/test_snapshot_auto_id_disabled_sqlserver.php b/tests/comparisons/Migration/sqlserver/test_snapshot_auto_id_disabled_sqlserver.php index 3957bd4a5..347b5cdd5 100644 --- a/tests/comparisons/Migration/sqlserver/test_snapshot_auto_id_disabled_sqlserver.php +++ b/tests/comparisons/Migration/sqlserver/test_snapshot_auto_id_disabled_sqlserver.php @@ -58,14 +58,14 @@ public function up(): void 'limit' => null, 'null' => true, ]) - ->addColumn('created', 'timestamp', [ + ->addColumn('created', 'datetimefractional', [ 'default' => null, 'limit' => null, 'null' => true, 'precision' => 7, 'scale' => 7, ]) - ->addColumn('modified', 'timestamp', [ + ->addColumn('modified', 'datetimefractional', [ 'default' => null, 'limit' => null, 'null' => true, @@ -103,14 +103,14 @@ public function up(): void 'limit' => 100, 'null' => true, ]) - ->addColumn('created', 'timestamp', [ + ->addColumn('created', 'datetimefractional', [ 'default' => null, 'limit' => null, 'null' => true, 'precision' => 7, 'scale' => 7, ]) - ->addColumn('modified', 'timestamp', [ + ->addColumn('modified', 'datetimefractional', [ 'default' => null, 'limit' => null, 'null' => true, @@ -240,14 +240,14 @@ public function up(): void 'limit' => 10, 'null' => true, ]) - ->addColumn('created', 'timestamp', [ + ->addColumn('created', 'datetimefractional', [ 'default' => null, 'limit' => null, 'null' => true, 'precision' => 7, 'scale' => 7, ]) - ->addColumn('modified', 'timestamp', [ + ->addColumn('modified', 'datetimefractional', [ 'default' => null, 'limit' => null, 'null' => true, @@ -316,7 +316,7 @@ public function up(): void 'limit' => null, 'null' => true, ]) - ->addColumn('highlighted_time', 'timestamp', [ + ->addColumn('highlighted_time', 'datetimefractional', [ 'default' => null, 'limit' => null, 'null' => true, @@ -365,14 +365,14 @@ public function up(): void 'limit' => 256, 'null' => true, ]) - ->addColumn('created', 'timestamp', [ + ->addColumn('created', 'datetimefractional', [ 'default' => null, 'limit' => null, 'null' => true, 'precision' => 7, 'scale' => 7, ]) - ->addColumn('updated', 'timestamp', [ + ->addColumn('updated', 'datetimefractional', [ 'default' => null, 'limit' => null, 'null' => true, diff --git a/tests/comparisons/Migration/sqlserver/test_snapshot_not_empty_sqlserver.php b/tests/comparisons/Migration/sqlserver/test_snapshot_not_empty_sqlserver.php index 43f2a5482..e0aad74e8 100644 --- a/tests/comparisons/Migration/sqlserver/test_snapshot_not_empty_sqlserver.php +++ b/tests/comparisons/Migration/sqlserver/test_snapshot_not_empty_sqlserver.php @@ -49,14 +49,14 @@ public function up(): void 'limit' => null, 'null' => true, ]) - ->addColumn('created', 'timestamp', [ + ->addColumn('created', 'datetimefractional', [ 'default' => null, 'limit' => null, 'null' => true, 'precision' => 7, 'scale' => 7, ]) - ->addColumn('modified', 'timestamp', [ + ->addColumn('modified', 'datetimefractional', [ 'default' => null, 'limit' => null, 'null' => true, @@ -87,14 +87,14 @@ public function up(): void 'limit' => 100, 'null' => true, ]) - ->addColumn('created', 'timestamp', [ + ->addColumn('created', 'datetimefractional', [ 'default' => null, 'limit' => null, 'null' => true, 'precision' => 7, 'scale' => 7, ]) - ->addColumn('modified', 'timestamp', [ + ->addColumn('modified', 'datetimefractional', [ 'default' => null, 'limit' => null, 'null' => true, @@ -195,14 +195,14 @@ public function up(): void 'limit' => 10, 'null' => true, ]) - ->addColumn('created', 'timestamp', [ + ->addColumn('created', 'datetimefractional', [ 'default' => null, 'limit' => null, 'null' => true, 'precision' => 7, 'scale' => 7, ]) - ->addColumn('modified', 'timestamp', [ + ->addColumn('modified', 'datetimefractional', [ 'default' => null, 'limit' => null, 'null' => true, @@ -263,7 +263,7 @@ public function up(): void 'limit' => null, 'null' => true, ]) - ->addColumn('highlighted_time', 'timestamp', [ + ->addColumn('highlighted_time', 'datetimefractional', [ 'default' => null, 'limit' => null, 'null' => true, @@ -305,14 +305,14 @@ public function up(): void 'limit' => 256, 'null' => true, ]) - ->addColumn('created', 'timestamp', [ + ->addColumn('created', 'datetimefractional', [ 'default' => null, 'limit' => null, 'null' => true, 'precision' => 7, 'scale' => 7, ]) - ->addColumn('updated', 'timestamp', [ + ->addColumn('updated', 'datetimefractional', [ 'default' => null, 'limit' => null, 'null' => true, diff --git a/tests/comparisons/Migration/sqlserver/test_snapshot_plugin_blog_sqlserver.php b/tests/comparisons/Migration/sqlserver/test_snapshot_plugin_blog_sqlserver.php index 1cbdfcfe3..f13b45b19 100644 --- a/tests/comparisons/Migration/sqlserver/test_snapshot_plugin_blog_sqlserver.php +++ b/tests/comparisons/Migration/sqlserver/test_snapshot_plugin_blog_sqlserver.php @@ -49,14 +49,14 @@ public function up(): void 'limit' => null, 'null' => true, ]) - ->addColumn('created', 'timestamp', [ + ->addColumn('created', 'datetimefractional', [ 'default' => null, 'limit' => null, 'null' => true, 'precision' => 7, 'scale' => 7, ]) - ->addColumn('modified', 'timestamp', [ + ->addColumn('modified', 'datetimefractional', [ 'default' => null, 'limit' => null, 'null' => true, @@ -87,14 +87,14 @@ public function up(): void 'limit' => 100, 'null' => true, ]) - ->addColumn('created', 'timestamp', [ + ->addColumn('created', 'datetimefractional', [ 'default' => null, 'limit' => null, 'null' => true, 'precision' => 7, 'scale' => 7, ]) - ->addColumn('modified', 'timestamp', [ + ->addColumn('modified', 'datetimefractional', [ 'default' => null, 'limit' => null, 'null' => true,