diff --git a/plugin/think-library/src/support/command/Package.php b/plugin/think-library/src/support/command/Package.php index a34880da0..2fabada8d 100644 --- a/plugin/think-library/src/support/command/Package.php +++ b/plugin/think-library/src/support/command/Package.php @@ -80,11 +80,13 @@ class Package extends Command */ private function createScheme(): bool { - $force = $this->input->hasOption('force'); + $force = boolval($this->input->getOption('force')); + $table = trim((string)$this->input->getOption('table')); + $all = boolval($this->input->getOption('all')); // 接收指定打包数据表 - if ($this->input->hasOption('table')) { - $tables = str2arr(strtr($this->input->getOption('table'), '|', ',')); - } elseif ($this->input->hasOption('all')) { + if ($table !== '') { + $tables = str2arr(strtr($table, '|', ',')); + } elseif ($all) { [$tables] = SystemService::getTables(); } else { $tables = Library::$sapp->config->get('phinx.tables', []); @@ -134,10 +136,12 @@ class Package extends Command */ private function createBackup(): bool { + $backup = trim((string)$this->input->getOption('backup')); + $all = boolval($this->input->getOption('all')); // 接收指定打包数据表 - if ($this->input->hasOption('backup')) { - $tables = str2arr(strtr($this->input->getOption('backup'), '|', ',')); - } elseif ($this->input->hasOption('all')) { + if ($backup !== '') { + $tables = str2arr(strtr($backup, '|', ',')); + } elseif ($all) { [$tables] = SystemService::getTables(); } else { [$tables] = SystemService::getTables(); diff --git a/plugin/think-library/src/support/command/Publish.php b/plugin/think-library/src/support/command/Publish.php index 3ad497bf0..70017adfb 100644 --- a/plugin/think-library/src/support/command/Publish.php +++ b/plugin/think-library/src/support/command/Publish.php @@ -91,9 +91,50 @@ class Publish extends Command // 复制数据库脚本 $frdir = rtrim($copy, '\/') . DIRECTORY_SEPARATOR . 'database'; + $this->cleanupPublishedMigrations($frdir, syspath('database/migrations')); ToolsExtend::copy($frdir, syspath('database/migrations'), [], $force, false); } + /** + * 清理已改名但仍残留在根目录的旧迁移脚本. + */ + private function cleanupPublishedMigrations(string $source, string $target): void + { + if (!is_dir($source) || !is_dir($target)) { + return; + } + + $sourceMigrations = []; + foreach (ToolsExtend::find($source, 1, function (\SplFileInfo $info) { + return $info->isFile() && strtolower($info->getExtension()) === 'php'; + }) as $file) { + $name = basename($file); + if (preg_match('/^\d{14}_(.+\.php)$/', $name, $match)) { + $sourceMigrations[$match[1]] = $name; + } + } + if (empty($sourceMigrations)) { + return; + } + + foreach (ToolsExtend::find($target, 1, function (\SplFileInfo $info) { + return $info->isFile() && strtolower($info->getExtension()) === 'php'; + }) as $file) { + $name = basename($file); + if (!preg_match('/^(\d{14})_(.+\.php)$/', $name, $match)) { + continue; + } + [$version, $suffix] = [$match[1], $match[2]]; + if (($sourceMigrations[$suffix] ?? $name) === $name) { + continue; + } + @unlink(rtrim($target, '\/') . DIRECTORY_SEPARATOR . $name); + if (is_dir($dataPath = rtrim($target, '\/') . DIRECTORY_SEPARATOR . $version)) { + ToolsExtend::remove($dataPath); + } + } + } + /** * 解析 json 包. * @return $this