mirror of
https://gitee.com/zoujingli/ThinkAdmin.git
synced 2026-06-09 05:08:17 +08:00
fix: 数据库脚本生成增加强制更新结构
This commit is contained in:
parent
c94449ab62
commit
b099474147
@ -131,16 +131,17 @@ class PhinxExtend
|
|||||||
* 创建数据库安装脚本
|
* 创建数据库安装脚本
|
||||||
* @param array $tables
|
* @param array $tables
|
||||||
* @param string $class
|
* @param string $class
|
||||||
|
* @param boolean $force
|
||||||
* @return string[]
|
* @return string[]
|
||||||
* @throws \Exception
|
* @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') {
|
if (Library::$sapp->db->connect()->getConfig('type') !== 'mysql') {
|
||||||
throw new Exception(' ** Notify: 只支持 MySql 数据库生成数据库脚本');
|
throw new Exception(' ** Notify: 只支持 MySql 数据库生成数据库脚本');
|
||||||
}
|
}
|
||||||
$br = "\r\n";
|
$br = "\r\n";
|
||||||
$content = static::_build2table($tables, true);
|
$content = static::_build2table($tables, true, $force);
|
||||||
$content = substr($content, strpos($content, "\n") + 1);
|
$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}";
|
$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];
|
return ['file' => static::nextFile($class), 'text' => $content];
|
||||||
@ -218,10 +219,11 @@ class PhinxExtend
|
|||||||
* 生成数据库表格创建模板
|
* 生成数据库表格创建模板
|
||||||
* @param array $tables 指定数据表
|
* @param array $tables 指定数据表
|
||||||
* @param boolean $rehtml 是否返回内容
|
* @param boolean $rehtml 是否返回内容
|
||||||
|
* @param boolean $force 强制更新结构
|
||||||
* @return string
|
* @return string
|
||||||
* @throws \Exception
|
* @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";
|
$br = "\r\n";
|
||||||
$connect = Library::$sapp->db->connect();
|
$connect = Library::$sapp->db->connect();
|
||||||
@ -281,7 +283,7 @@ class PhinxExtend
|
|||||||
'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '{$comment}',
|
'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '{$comment}',
|
||||||
]);
|
]);
|
||||||
// 创建或更新数据表
|
// 创建或更新数据表
|
||||||
PhinxExtend::upgrade(\$table, _FIELDS_, _INDEXS_);
|
PhinxExtend::upgrade(\$table, _FIELDS_, _INDEXS_, __FORCE__);
|
||||||
}
|
}
|
||||||
CODE;
|
CODE;
|
||||||
// 生成字段内容
|
// 生成字段内容
|
||||||
@ -325,7 +327,7 @@ CODE;
|
|||||||
$_indexString .= "'{$index}', ";
|
$_indexString .= "'{$index}', ";
|
||||||
}
|
}
|
||||||
$_indexString .= PHP_EOL . "\t\t]";
|
$_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);
|
return $rehtml ? $content : highlight_string($content, true);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -40,6 +40,7 @@ class Package extends Command
|
|||||||
{
|
{
|
||||||
$this->setName('xadmin:package');
|
$this->setName('xadmin:package');
|
||||||
$this->addOption('all', 'a', Option::VALUE_NONE, 'Backup All Tables');
|
$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('table', 't', Option::VALUE_OPTIONAL, 'Package Tables Scheme', '');
|
||||||
$this->addOption('backup', 'b', Option::VALUE_OPTIONAL, 'Package Tables Backup', '');
|
$this->addOption('backup', 'b', Option::VALUE_OPTIONAL, 'Package Tables Backup', '');
|
||||||
$this->setDescription('Generate System Install Package for ThinkAdmin');
|
$this->setDescription('Generate System Install Package for ThinkAdmin');
|
||||||
@ -78,6 +79,7 @@ class Package extends Command
|
|||||||
*/
|
*/
|
||||||
private function createScheme(): bool
|
private function createScheme(): bool
|
||||||
{
|
{
|
||||||
|
$force = $this->input->hasOption('force');
|
||||||
// 接收指定打包数据表
|
// 接收指定打包数据表
|
||||||
if ($this->input->hasOption('table')) {
|
if ($this->input->hasOption('table')) {
|
||||||
$tables = str2arr(strtr($this->input->getOption('table'), '|', ','));
|
$tables = str2arr(strtr($this->input->getOption('table'), '|', ','));
|
||||||
@ -106,7 +108,7 @@ class Package extends Command
|
|||||||
$this->setQueueMessage($total, 0, '开始创建数据表创建脚本!');
|
$this->setQueueMessage($total, 0, '开始创建数据表创建脚本!');
|
||||||
foreach ($groups as $key => $tbs) {
|
foreach ($groups as $key => $tbs) {
|
||||||
$name = 'Install' . ucfirst($key) . 'Table';
|
$name = 'Install' . ucfirst($key) . 'Table';
|
||||||
$phinx = PhinxExtend::create2table($tbs, $name);
|
$phinx = PhinxExtend::create2table($tbs, $name, $force);
|
||||||
$target = syspath("database/migrations/{$phinx['file']}");
|
$target = syspath("database/migrations/{$phinx['file']}");
|
||||||
if (file_put_contents($target, $phinx['text']) !== false) {
|
if (file_put_contents($target, $phinx['text']) !== false) {
|
||||||
$this->setQueueMessage($total, ++$count, "创建数据库 {$name} 安装脚本成功!");
|
$this->setQueueMessage($total, ++$count, "创建数据库 {$name} 安装脚本成功!");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user