@@ -30,7 +30,7 @@ public static function tableExists($tableName)
3030 */
3131 public static function tableInfo ($ tableName )
3232 {
33- $ result = [ 'tablename ' => $ tableName ];
33+ $ result = [ 'tablename ' => $ tableName, ' originalTablename ' => $ tableName ];
3434
3535 $ tableInfo = DB ::select ( "PRAGMA table_info(' $ tableName') " );
3636
@@ -95,30 +95,17 @@ public static function tableGet($tableName)
9595 /**
9696 * Create table.
9797 *
98- * @param string $tableName
99- * @param object $structure
10098 * @return array
10199 */
102- public static function tableStore ($ tableName , $ structure )
100+ public static function tableStore ()
103101 {
104- if (TableController::tableExists ($ tableName ))
105- return TableController::tableUpdate ($ tableName ,$ structure );
106-
107- Schema::create ($ tableName , function ($ t ) use ($ structure ){
108-
109- foreach ($ structure as $ column => $ type ){
110-
111- if ($ column =="id " )
112- $ t ->$ type ($ column );
113-
114- else
115- $ t ->$ type ($ column )->nullable ();
116-
117- }
102+ Schema::create ('_temp_ ' , function ($ table ){
103+
104+ $ table ->increments ('id ' );
118105
119106 });
120107
121- return TableController::tableGet ($ tableName );
108+ return TableController::tableGet (' _temp_ ' );
122109 }
123110
124111 /**
@@ -128,15 +115,20 @@ public static function tableStore($tableName,$structure)
128115 * @param object $structure
129116 * @return array
130117 */
131- public static function tableUpdate ($ tableName ,$ structureNew )
118+ public static function tableUpdate ($ tableName ,$ tableInfoNew )
132119 {
133120 if (!TableController::tableExists ($ tableName ))
134- return TableController::tableStore ($ tableName , $ structureNew );
121+ return TableController::tableStore ();
135122
136- $ tableInfo = TableController::tableInfo ($ tableName );
123+ // Get Current table
124+ $ tableInfoOld = TableController::tableInfo ($ tableName );
137125
138- $ structureOld = $ tableInfo ['columns ' ];
126+ $ structureOld = $ tableInfoOld ['columns ' ];
127+
128+ // Get New table
129+ $ structureNew = $ tableInfoNew ['columns ' ];
139130
131+
140132 // Check for new columns
141133 $ addThese = array ();
142134
@@ -208,8 +200,12 @@ public static function tableUpdate($tableName,$structureNew)
208200
209201 }
210202
203+ // Check for rename table
204+ if ($ tableInfoNew ['originalTablename ' ] != $ tableInfoNew ['tablename ' ])
205+ Schema::rename ($ tableInfoNew ['originalTablename ' ], $ tableInfoNew ['tablename ' ]);
206+
211207
212- return TableController::tableGet ($ tableName );
208+ return TableController::tableGet ($ tableInfoNew [ ' tablename ' ] );
213209 }
214210
215211}
0 commit comments