@@ -156,70 +156,53 @@ public function testColumnSchemaWithTypeBit(array $columns): void
156156 */
157157 public function testDefaultValueDatetimeColumn (): void
158158 {
159+ $ tableName = '{{%datetime_test}} ' ;
159160 $ db = $ this ->getConnection ();
160161
161- if ($ db ->getTableSchema ('{{%datetime_test}} ' , true ) !== null ) {
162- $ db ->createCommand ('DROP TABLE `datetime_test` ' )->execute ();
163- }
164-
165- $ command = $ db ->createCommand ();
166- $ schema = $ db ->getSchema ();
167-
168162 $ oldMySQL = !(
169163 version_compare ($ db ->getServerVersion (), '8.0.0 ' , '> ' ) &&
170164 !str_contains ($ db ->getServerVersion (), 'MariaDB ' )
171165 );
172166
173- if ($ oldMySQL ) {
174- $ sql = <<<SQL
175- CREATE TABLE IF NOT EXISTS `datetime_test` (
176- `id` int(11) NOT NULL AUTO_INCREMENT,
177- `dt` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
178- `simple_col` varchar(40) DEFAULT 'uuid()',
179- `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
180- PRIMARY KEY (`id`)
181- ) ENGINE=InnoDB DEFAULT CHARSET=utf8
182- SQL ;
183- } else {
184- $ sql = <<<SQL
185- CREATE TABLE IF NOT EXISTS `datetime_test` (
186- `id` int(11) NOT NULL AUTO_INCREMENT,
187- `dt` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
188- `simple_col` varchar(40) DEFAULT 'uuid()',
189- `uuid_col` varchar(40) DEFAULT (uuid()),
190- `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
191- PRIMARY KEY (`id`)
192- ) ENGINE=InnoDB DEFAULT CHARSET=utf8
193- SQL ;
167+ $ columnsData = [
168+ 'id ' => ['int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY ' , '' , false ],
169+ 'd ' => ['date DEFAULT \'2011-11-11 \'' , '2011-11-11 ' , false ],
170+ 'dt ' => ['datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ' , 'CURRENT_TIMESTAMP ' , true ],
171+ 'dt1 ' => ['datetime DEFAULT \'2011-11-11 00:00:00 \'' , '2011-11-11 00:00:00 ' , false ],
172+ 'dt2 ' => ['datetime DEFAULT CURRENT_TIMESTAMP ' , 'CURRENT_TIMESTAMP ' , true ],
173+ 'ts ' => ['timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ' , 'CURRENT_TIMESTAMP ' , true ],
174+ 'ts1 ' => ['timestamp DEFAULT \'2011-11-11 00:00:00 \'' , '2011-11-11 00:00:00 ' , false ],
175+ 'ts2 ' => ['timestamp DEFAULT CURRENT_TIMESTAMP ' , 'CURRENT_TIMESTAMP ' , true ],
176+ 'simple_col ' => ['varchar(40) DEFAULT \'uuid() \'' , 'uuid() ' , false ],
177+ ];
178+ if (!$ oldMySQL ) {
179+ $ columnsData ['ts4 ' ] = ['date DEFAULT (CURRENT_DATE + INTERVAL 2 YEAR) ' , '(curdate() + interval 2 year) ' , true ];
180+ $ columnsData ['uuid_col ' ] = ['varchar(40) DEFAULT (uuid()) ' , 'uuid() ' , true ];
194181 }
195182
196- $ command ->setSql ($ sql )->execute ();
183+ $ columns = [];
184+ foreach ($ columnsData as $ column => $ columnData ) {
185+ $ columns [$ column ] = $ columnData [0 ];
186+ }
197187
198- $ schema = $ schema ->getTableSchema ('datetime_test ' );
188+ if ($ db ->getTableSchema ($ tableName , true ) !== null ) {
189+ $ db ->createCommand ()->dropTable ($ tableName )->execute ();
190+ }
199191
200- $ this -> assertNotNull ( $ schema );
192+ $ db -> createCommand ()-> createTable ( $ tableName , $ columns , ' ENGINE=InnoDB DEFAULT CHARSET=utf8 ' )-> execute ( );
201193
202- $ dt = $ schema ->getColumn ('dt ' );
203- $ this ->assertNotNull ($ dt );
204- $ this ->assertInstanceOf (Expression::class, $ dt ->getDefaultValue ());
205- $ this ->assertEquals ('CURRENT_TIMESTAMP ' , (string ) $ dt ->getDefaultValue ());
194+ $ tableSchema = $ db ->getTableSchema ($ tableName );
195+ $ this ->assertNotNull ($ tableSchema );
206196
207- if (!$ oldMySQL ) {
208- $ uuid = $ schema ->getColumn ('uuid_col ' );
209- $ this ->assertNotNull ($ uuid );
210- $ this ->assertInstanceOf (Expression::class, $ uuid ->getDefaultValue ());
211- $ this ->assertEquals ('uuid() ' , (string )$ uuid ->getDefaultValue ());
197+ foreach ($ tableSchema ->getColumns () as $ column ) {
198+ $ columnName = $ column ->getName ();
199+ if ($ columnsData [$ columnName ][2 ]) {
200+ $ this ->assertInstanceOf (Expression::class, $ column ->getDefaultValue ());
201+ } else {
202+ $ this ->assertNotInstanceOf (Expression::class, $ column ->getDefaultValue ());
203+ }
204+ $ this ->assertEquals ($ columnsData [$ columnName ][1 ], (string ) $ column ->getDefaultValue ());
212205 }
213-
214- $ simple = $ schema ->getColumn ('simple_col ' );
215- $ this ->assertNotNull ($ simple );
216- $ this ->assertNotInstanceOf (Expression::class, $ simple ->getDefaultValue ());
217- $ this ->assertEquals ('uuid() ' , (string ) $ simple ->getDefaultValue ());
218-
219- $ ts = $ schema ->getColumn ('ts ' );
220- $ this ->assertNotNull ($ ts );
221- $ this ->assertInstanceOf (Expression::class, $ ts ->getDefaultValue ());
222- $ this ->assertEquals ('CURRENT_TIMESTAMP ' , (string ) $ ts ->getDefaultValue ());
223206 }
224207
225208 /**
0 commit comments