Skip to content

Commit

Permalink
fix: 数据库脚本生成增加强制更新结构
Browse files Browse the repository at this point in the history
  • Loading branch information
zoujingli committed Nov 17, 2024
1 parent 27be18c commit c776b05
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/extend/PhinxExtend.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,17 @@ public static function upgrade(Table $table, array $fields, array $indexs = [],
* 创建数据库安装脚本
* @param array $tables
* @param string $class
* @param boolean $force
* @return string[]
* @throws \Exception
*/
public static function create2table(array $tables = [], string $class = 'InstallTable'): array
public static function create2table(array $tables = [], string $class = 'InstallTable', bool $force = false): array
{
if (Library::$sapp->db->connect()->getConfig('type') !== 'mysql') {
throw new Exception(' ** Notify: 只支持 MySql 数据库生成数据库脚本');
}
$br = "\r\n";
$content = static::_build2table($tables, true);
$content = static::_build2table($tables, true, $force);
$content = substr($content, strpos($content, "\n") + 1);
$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}";
return ['file' => static::nextFile($class), 'text' => $content];
Expand Down Expand Up @@ -218,10 +219,11 @@ private static function _arr2str(array $data): string
* 生成数据库表格创建模板
* @param array $tables 指定数据表
* @param boolean $rehtml 是否返回内容
* @param boolean $force 强制更新结构
* @return string
* @throws \Exception
*/
private static function _build2table(array $tables = [], bool $rehtml = false): string
private static function _build2table(array $tables = [], bool $rehtml = false, bool $force = false): string
{
$br = "\r\n";
$connect = Library::$sapp->db->connect();
Expand Down Expand Up @@ -281,7 +283,7 @@ private function _create_{$table}()
'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '{$comment}',
]);
// 创建或更新数据表
PhinxExtend::upgrade(\$table, _FIELDS_, _INDEXS_);
PhinxExtend::upgrade(\$table, _FIELDS_, _INDEXS_, __FORCE__);
}
CODE;
// 生成字段内容
Expand Down Expand Up @@ -325,7 +327,7 @@ private function _create_{$table}()
$_indexString .= "'{$index}', ";
}
$_indexString .= PHP_EOL . "\t\t]";
$content = str_replace(['_FIELDS_', '_INDEXS_'], [$_fieldString, $_indexString], $content) . PHP_EOL . PHP_EOL;
$content = str_replace(['_FIELDS_', '_INDEXS_', '__FORCE__'], [$_fieldString, $_indexString, $force ? 'true' : 'false'], $content) . PHP_EOL . PHP_EOL;
}
return $rehtml ? $content : highlight_string($content, true);
}
Expand Down
4 changes: 3 additions & 1 deletion src/support/command/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function configure()
{
$this->setName('xadmin:package');
$this->addOption('all', 'a', Option::VALUE_NONE, 'Backup All Tables');
$this->addOption('force', 'f', Option::VALUE_NONE, 'Force All Update');
$this->addOption('table', 't', Option::VALUE_OPTIONAL, 'Package Tables Scheme', '');
$this->addOption('backup', 'b', Option::VALUE_OPTIONAL, 'Package Tables Backup', '');
$this->setDescription('Generate System Install Package for ThinkAdmin');
Expand Down Expand Up @@ -78,6 +79,7 @@ public function handle()
*/
private function createScheme(): bool
{
$force = $this->input->hasOption('force');
// 接收指定打包数据表
if ($this->input->hasOption('table')) {
$tables = str2arr(strtr($this->input->getOption('table'), '|', ','));
Expand Down Expand Up @@ -106,7 +108,7 @@ private function createScheme(): bool
$this->setQueueMessage($total, 0, '开始创建数据表创建脚本!');
foreach ($groups as $key => $tbs) {
$name = 'Install' . ucfirst($key) . 'Table';
$phinx = PhinxExtend::create2table($tbs, $name);
$phinx = PhinxExtend::create2table($tbs, $name, $force);
$target = syspath("database/migrations/{$phinx['file']}");
if (file_put_contents($target, $phinx['text']) !== false) {
$this->setQueueMessage($total, ++$count, "创建数据库 {$name} 安装脚本成功!");
Expand Down

0 comments on commit c776b05

Please sign in to comment.