Skip to content

Commit c776b05

Browse files
committed
fix: 数据库脚本生成增加强制更新结构
1 parent 27be18c commit c776b05

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/extend/PhinxExtend.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,17 @@ public static function upgrade(Table $table, array $fields, array $indexs = [],
131131
* 创建数据库安装脚本
132132
* @param array $tables
133133
* @param string $class
134+
* @param boolean $force
134135
* @return string[]
135136
* @throws \Exception
136137
*/
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
138139
{
139140
if (Library::$sapp->db->connect()->getConfig('type') !== 'mysql') {
140141
throw new Exception(' ** Notify: 只支持 MySql 数据库生成数据库脚本');
141142
}
142143
$br = "\r\n";
143-
$content = static::_build2table($tables, true);
144+
$content = static::_build2table($tables, true, $force);
144145
$content = substr($content, strpos($content, "\n") + 1);
145146
$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}";
146147
return ['file' => static::nextFile($class), 'text' => $content];
@@ -218,10 +219,11 @@ private static function _arr2str(array $data): string
218219
* 生成数据库表格创建模板
219220
* @param array $tables 指定数据表
220221
* @param boolean $rehtml 是否返回内容
222+
* @param boolean $force 强制更新结构
221223
* @return string
222224
* @throws \Exception
223225
*/
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
225227
{
226228
$br = "\r\n";
227229
$connect = Library::$sapp->db->connect();
@@ -281,7 +283,7 @@ private function _create_{$table}()
281283
'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '{$comment}',
282284
]);
283285
// 创建或更新数据表
284-
PhinxExtend::upgrade(\$table, _FIELDS_, _INDEXS_);
286+
PhinxExtend::upgrade(\$table, _FIELDS_, _INDEXS_, __FORCE__);
285287
}
286288
CODE;
287289
// 生成字段内容
@@ -325,7 +327,7 @@ private function _create_{$table}()
325327
$_indexString .= "'{$index}', ";
326328
}
327329
$_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;
329331
}
330332
return $rehtml ? $content : highlight_string($content, true);
331333
}

src/support/command/Package.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function configure()
4040
{
4141
$this->setName('xadmin:package');
4242
$this->addOption('all', 'a', Option::VALUE_NONE, 'Backup All Tables');
43+
$this->addOption('force', 'f', Option::VALUE_NONE, 'Force All Update');
4344
$this->addOption('table', 't', Option::VALUE_OPTIONAL, 'Package Tables Scheme', '');
4445
$this->addOption('backup', 'b', Option::VALUE_OPTIONAL, 'Package Tables Backup', '');
4546
$this->setDescription('Generate System Install Package for ThinkAdmin');
@@ -78,6 +79,7 @@ public function handle()
7879
*/
7980
private function createScheme(): bool
8081
{
82+
$force = $this->input->hasOption('force');
8183
// 接收指定打包数据表
8284
if ($this->input->hasOption('table')) {
8385
$tables = str2arr(strtr($this->input->getOption('table'), '|', ','));
@@ -106,7 +108,7 @@ private function createScheme(): bool
106108
$this->setQueueMessage($total, 0, '开始创建数据表创建脚本!');
107109
foreach ($groups as $key => $tbs) {
108110
$name = 'Install' . ucfirst($key) . 'Table';
109-
$phinx = PhinxExtend::create2table($tbs, $name);
111+
$phinx = PhinxExtend::create2table($tbs, $name, $force);
110112
$target = syspath("database/migrations/{$phinx['file']}");
111113
if (file_put_contents($target, $phinx['text']) !== false) {
112114
$this->setQueueMessage($total, ++$count, "创建数据库 {$name} 安装脚本成功!");

0 commit comments

Comments
 (0)