@@ -76,6 +76,16 @@ class MigrationParser
7676 */
7777 protected $ tableCharsetAndCollationFile ;
7878
79+ /**
80+ * @var string
81+ */
82+ protected $ foreignStructureFile ;
83+
84+ /**
85+ * @var string
86+ */
87+ protected $ hasForeign ;
88+
7989 /**
8090 * MigrationParser constructor.
8191 *
@@ -84,14 +94,18 @@ class MigrationParser
8494 * @param string $keysFile
8595 * @param string $constraintsFile
8696 * @param string $tableCharsetAndCollationFile
97+ * @param string $foreignStructureFile
98+ * @param string $hasForeign
8799 */
88- public function __construct ($ tableName , $ structureFile , $ keysFile , $ constraintsFile , $ tableCharsetAndCollationFile )
100+ public function __construct ($ tableName , $ structureFile , $ keysFile , $ constraintsFile , $ tableCharsetAndCollationFile, $ foreignStructureFile , $ hasForeign )
89101 {
90102 $ this ->tableName = $ tableName ;
91103 $ this ->structureFile = $ structureFile ;
92104 $ this ->keysFile = $ keysFile ;
93105 $ this ->constraintsFile = $ constraintsFile ;
94106 $ this ->tableCharsetAndCollationFile = $ tableCharsetAndCollationFile ;
107+ $ this ->foreignStructureFile = $ foreignStructureFile ;
108+ $ this ->hasForeign = $ hasForeign ;
95109 }
96110
97111 public function makeMigration ()
@@ -110,40 +124,94 @@ public function makeMigration()
110124 $ constraints = trim (implode ($ eol . $ indent12 , $ this ->formatConstraints ())) . $ eol ;
111125 $ tableCollationAndCharset = trim (implode ($ eol . $ indent12 , $ this ->formatTableCollationAndCharset ())) . $ eol ;
112126 $ extras = trim (implode ($ eol . $ indent8 , $ this ->formatExtras ())) . $ eol ;
127+ $ foreign = trim (implode ($ eol . $ indent12 , $ this ->formatForeign ())) . $ eol ;
128+ $ foreignDrop = trim (implode ($ eol . $ indent12 , $ this ->formatForeignDrop ())) . $ eol ;
129+ if ($ this ->hasForeign == "true " ) {
130+ $ className = 'AddFkTo ' . $ this ->studly ($ this ->tableName ) . 'Table ' ;
131+ $ output = file_get_contents (__DIR__ . '/table.stub ' );
132+ $ output = str_replace (
133+ [
134+ ':VERSION: ' ,
135+ 'DummyClass ' ,
136+ 'DummyTable ' ,
137+ "// foreign \n" ,
138+ "// foreignDrop \n" ,
139+ ],
140+ [
141+ $ this ->version ,
142+ $ className ,
143+ $ this ->tableName ,
144+ $ foreign ,
145+ $ foreignDrop ,
146+ ],
147+ $ output
148+ );
149+ } else {
150+ $ className = 'Create ' . $ this ->studly ($ this ->tableName ) . 'Table ' ;
151+ $ output = file_get_contents (__DIR__ . '/create.stub ' );
152+ $ output = str_replace (
153+ [
154+ ':VERSION: ' ,
155+ 'DummyClass ' ,
156+ 'DummyTable ' ,
157+ "// structure \n" ,
158+ "// keys \n" ,
159+ "// constraints \n" ,
160+ "// tableCollationAndCharset \n" ,
161+ "// extras \n" ,
162+ ],
163+ [
164+ $ this ->version ,
165+ $ className ,
166+ $ this ->tableName ,
167+ $ structure ,
168+ $ keys ,
169+ $ constraints ,
170+ $ tableCollationAndCharset ,
171+ $ extras ,
172+ ],
173+ $ output
174+ );
175+ }
113176
114- $ output = file_get_contents (__DIR__ . '/create.stub ' );
115-
116- $ className = 'Create ' . $ this ->studly ($ this ->tableName ) . 'Table ' ;
117-
118- $ output = str_replace (
119- [
120- ':VERSION: ' ,
121- 'DummyClass ' ,
122- 'DummyTable ' ,
123- "// structure \n" ,
124- "// keys \n" ,
125- "// constraints \n" ,
126- "// tableCollationAndCharset \n" ,
127- "// extras \n" ,
128- ],
129- [
130- $ this ->version ,
131- $ className ,
132- $ this ->tableName ,
133- $ structure ,
134- $ keys ,
135- $ constraints ,
136- $ tableCollationAndCharset ,
137- $ extras ,
138- ],
139- $ output
140- );
141177
142178 $ output = preg_replace ("/^(\s*\R){2,}/m " , "\n" , $ output );
143179
144180 return $ output ;
145181 }
146182
183+ public function formatForeign ()
184+ {
185+ $ fields = [];
186+
187+ $ rows = file ($ this ->foreignStructureFile );
188+ array_shift ($ rows );
189+
190+ foreach ($ rows as $ row ) {
191+ list ($ table , $ colName , $ constName , $ refTable , $ refColumnName ) = explode ("\t" ,
192+ $ row , 5 );
193+ $ fields [] = '$table->unsignedBigInteger( \'' . trim ($ colName ) .'\')->change(); ' ;
194+ $ fields [] = '$table->foreign( \'' . trim ($ colName ) .'\')->references( \'' . trim ($ refColumnName ) .'\')->on( \'' . trim ($ refTable ) .'\'); ' ;
195+ }
196+ return array_filter ($ fields );
197+ }
198+
199+ public function formatForeignDrop ()
200+ {
201+ $ fields = [];
202+
203+ $ rows = file ($ this ->foreignStructureFile );
204+ array_shift ($ rows );
205+
206+ foreach ($ rows as $ row ) {
207+ list ($ table , $ colName , $ constName , $ refTable , $ refColumnName ) = explode ("\t" ,
208+ $ row , 5 );
209+ //$table->dropForeign('local_table_foreign_id_foreign');
210+ $ fields [] = '$table->dropForeign( \'' . trim ($ table ) .'_ ' . trim ($ colName ) .'_foreign \'); ' ;
211+ }
212+ return array_filter ($ fields );
213+ }
214+
147215 protected function studly ($ value )
148216 {
149217 $ value = ucwords (str_replace (['- ' , '_ ' ], ' ' , $ value ));
@@ -242,6 +310,24 @@ public function buildStructure()
242310 $ this ->structure ['remember_token ' ]['nullable ' ] = false ;
243311 $ this ->structure ['remember_token ' ]['field ' ] = null ;
244312 }
313+
314+ // look for id primary key
315+
316+ if (
317+ array_key_exists ('id ' , $ this ->structure )
318+ && $ this ->structure ['id ' ]['method ' ] === 'integer '
319+ && $ this ->structure ['id ' ]['autoIncrement ' ] === true
320+ && $ this ->structure ['id ' ]['args ' ] === null
321+ ) {
322+ $ this ->structure ['id ' ]['method ' ] = 'id ' ;
323+ $ this ->structure ['id ' ]['args ' ] = null ;
324+ $ this ->structure ['id ' ]['default ' ] = null ;
325+ $ this ->structure ['id ' ]['nullable ' ] = false ;
326+ $ this ->structure ['id ' ]['field ' ] = null ;
327+ $ this ->structure ['id ' ]['comment ' ] = null ;
328+ $ this ->structure ['id ' ]['autoIncrement ' ] = null ;
329+ $ this ->structure ['id ' ]['unsigned ' ] = null ;
330+ }
245331 }
246332
247333 public function formatStructure ()
@@ -359,7 +445,7 @@ public function buildKeys()
359445 $ primaryColumn = reset ($ primary ['columns ' ]);
360446 $ field = $ this ->structure [$ primaryColumn ];
361447 // and that column is an "increments" field ...
362- if (isset ($ field ['args ' ][ ' autoIncrement ' ]) && $ field ['args ' ][ ' autoIncrement ' ] === ' true ' ) {
448+ if (isset ($ field ['autoIncrement ' ]) && $ field ['autoIncrement ' ] == ' 1 ' ) {
363449 // then don't build the primary key, since Laravel takes care of it
364450 unset($ this ->keys ['PRIMARY ' ]);
365451 }
@@ -375,7 +461,7 @@ public function formatKeys()
375461 $ columns = $ this ->escapeArray ($ data ['columns ' ]);
376462
377463 if ($ field === 'PRIMARY ' ) {
378- $ fields [$ field ] = sprintf ('$table->primary(%s); ' , $ columns );
464+ // $fields[$field] = sprintf('$table->primary(%s);', $columns);
379465 } else {
380466 $ fields [$ field ] = sprintf ('$table->%s(%s, \'%s \'); ' ,
381467 $ data ['method ' ],
0 commit comments