33class MigrationParser
44{
55
6+ const TS_UPDATE_STRING = 'ON UPDATE CURRENT_TIMESTAMP ' ;
7+
68 /**
79 * @var string
810 */
9- protected $ version = '1.6 .0 ' ;
11+ protected $ version = '1.7 .0 ' ;
1012
1113 /**
1214 * @var array
@@ -94,10 +96,10 @@ public function __construct($tableName, $structureFile, $keysFile, $constraintsF
9496
9597 public function makeMigration ()
9698 {
99+ $ this ->buildTableCollationAndCharset ();
97100 $ this ->buildStructure ();
98101 $ this ->buildKeys ();
99102 $ this ->buildConstraints ();
100- $ this ->buildTableCollationAndCharset ();
101103
102104 $ indent8 = str_repeat (' ' , 8 );
103105 $ indent12 = str_repeat (' ' , 12 );
@@ -119,6 +121,8 @@ public function makeMigration()
119121 $ output
120122 );
121123
124+ $ output = preg_replace ("/^(\s*\R){2,}/m " , "\n" , $ output );
125+
122126 return $ output ;
123127 }
124128
@@ -156,8 +160,8 @@ public function buildStructure()
156160 'field ' => $ field ,
157161 'nullable ' => ($ null === 'YES ' ),
158162 'default ' => ($ default !== 'NULL ' ) ? $ default : null ,
159- 'characterSet ' => ($ characterSet !== 'NULL ' ) ? $ characterSet : null ,
160- 'collation ' => ($ collation !== 'NULL ' ) ? $ collation : null ,
163+ 'characterSet ' => ($ characterSet !== 'NULL ' && $ characterSet !== $ this -> tableCharset ) ? $ characterSet : null ,
164+ 'collation ' => ($ collation !== 'NULL ' && $ collation !== $ this -> tableCollation ) ? $ collation : null ,
161165 '_colType ' => $ colType ,
162166 ];
163167
@@ -232,6 +236,11 @@ public function formatStructure()
232236 || $ method === 'double '
233237 || $ method === 'float ' ;
234238
239+ if ($ method ==='timestamp ' && $ data ['args ' ] === self ::TS_UPDATE_STRING ) {
240+ $ data ['default ' ] = $ data ['default ' ] . ' ' . $ data ['args ' ];
241+ $ data ['args ' ] = null ;
242+ }
243+
235244 $ temp = '$table-> ' . $ method ;
236245 if ($ data ['field ' ]) {
237246 $ temp .= '( \'' . $ field . '\'' ;
@@ -259,8 +268,8 @@ public function formatStructure()
259268 $ temp .= '->default( ' . $ data ['default ' ] . ') ' ;
260269 } elseif ($ method ==='boolean ' ) {
261270 $ temp .= '->default( ' . ($ data ['default ' ] ? 'true ' : 'false ' ) . ') ' ;
262- } elseif (strtolower (trim ($ data ['default ' ])) === ' current_timestamp ' ) {
263- $ temp .= '->default(\DB::raw( \'CURRENT_TIMESTAMP \')) ' ;
271+ } elseif (stripos (trim ($ data ['default ' ]), ' CURRENT_TIMESTAMP ' ) !== false ) {
272+ $ temp .= '->default(\DB::raw( \'' . trim ( $ data [ ' default ' ]) . ' \')) ' ;
264273 } else {
265274 $ temp .= '->default( \'' . $ this ->trimStringQuotes ($ data ['default ' ]) . '\') ' ;
266275 }
@@ -405,9 +414,6 @@ public function buildTableCollationAndCharset()
405414 {
406415 $ this ->constraints = [];
407416
408- $ rows = file ($ this ->constraintsFile );
409- array_shift ($ rows );
410-
411417 $ rows = file ($ this ->tableCharsetAndCollationFile );
412418 array_shift ($ rows );
413419
@@ -439,13 +445,6 @@ protected function copyToClipboard($content)
439445 shell_exec ($ cmd );
440446 }
441447
442- protected function extractSize ($ string )
443- {
444- if (preg_match ('#\(([^)]+)\)# ' , $ string , $ m )) {
445- return $ m [1 ];
446- }
447- }
448-
449448 protected function escapeArray ($ array )
450449 {
451450 $ array = (array )$ array ;
@@ -585,6 +584,9 @@ protected function parseTime($type, $args, $typeExtra, $extra)
585584
586585 protected function parseTimestamp ($ type , $ args , $ typeExtra , $ extra )
587586 {
587+ if (stripos ($ extra , self ::TS_UPDATE_STRING ) !==false ) {
588+ $ args = self ::TS_UPDATE_STRING ;
589+ }
588590 return $ this ->defaultParse ('timestamp ' , $ args );
589591 }
590592
0 commit comments