@@ -131,16 +131,17 @@ public static function upgrade(Table $table, array $fields, array $indexs = [],
131
131
* 创建数据库安装脚本
132
132
* @param array $tables
133
133
* @param string $class
134
+ * @param boolean $force
134
135
* @return string[]
135
136
* @throws \Exception
136
137
*/
137
- public static function create2table (array $ tables = [], string $ class = 'InstallTable ' ): array
138
+ public static function create2table (array $ tables = [], string $ class = 'InstallTable ' , bool $ force = false ): array
138
139
{
139
140
if (Library::$ sapp ->db ->connect ()->getConfig ('type ' ) !== 'mysql ' ) {
140
141
throw new Exception (' ** Notify: 只支持 MySql 数据库生成数据库脚本 ' );
141
142
}
142
143
$ br = "\r\n" ;
143
- $ content = static ::_build2table ($ tables , true );
144
+ $ content = static ::_build2table ($ tables , true , $ force );
144
145
$ content = substr ($ content , strpos ($ content , "\n" ) + 1 );
145
146
$ content = '<?php ' . "{$ br }{$ br }use think \\admin \\extend \\PhinxExtend; {$ br }use think\migration\Migrator; {$ br }{$ br }@set_time_limit(0); {$ br }@ini_set('memory_limit', -1); {$ br }{$ br }class {$ class } extends Migrator {$ br }{ {$ br }{$ content }} {$ br }" ;
146
147
return ['file ' => static ::nextFile ($ class ), 'text ' => $ content ];
@@ -218,10 +219,11 @@ private static function _arr2str(array $data): string
218
219
* 生成数据库表格创建模板
219
220
* @param array $tables 指定数据表
220
221
* @param boolean $rehtml 是否返回内容
222
+ * @param boolean $force 强制更新结构
221
223
* @return string
222
224
* @throws \Exception
223
225
*/
224
- private static function _build2table (array $ tables = [], bool $ rehtml = false ): string
226
+ private static function _build2table (array $ tables = [], bool $ rehtml = false , bool $ force = false ): string
225
227
{
226
228
$ br = "\r\n" ;
227
229
$ connect = Library::$ sapp ->db ->connect ();
@@ -281,7 +283,7 @@ private function _create_{$table}()
281
283
'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => ' {$ comment }',
282
284
]);
283
285
// 创建或更新数据表
284
- PhinxExtend::upgrade( \$table, _FIELDS_, _INDEXS_);
286
+ PhinxExtend::upgrade( \$table, _FIELDS_, _INDEXS_, __FORCE__ );
285
287
}
286
288
CODE ;
287
289
// 生成字段内容
@@ -325,7 +327,7 @@ private function _create_{$table}()
325
327
$ _indexString .= "' {$ index }', " ;
326
328
}
327
329
$ _indexString .= PHP_EOL . "\t\t] " ;
328
- $ content = str_replace (['_FIELDS_ ' , '_INDEXS_ ' ], [$ _fieldString , $ _indexString ], $ content ) . PHP_EOL . PHP_EOL ;
330
+ $ content = str_replace (['_FIELDS_ ' , '_INDEXS_ ' , ' __FORCE__ ' ], [$ _fieldString , $ _indexString, $ force ? ' true ' : ' false ' ], $ content ) . PHP_EOL . PHP_EOL ;
329
331
}
330
332
return $ rehtml ? $ content : highlight_string ($ content , true );
331
333
}
0 commit comments