生成数据库迁移文件支持生成更新的迁移文件

This commit is contained in:
augushong
2024-10-15 13:49:23 +08:00
parent afd8b81712
commit b04321e380
3 changed files with 43 additions and 29 deletions

View File

@@ -12,14 +12,14 @@ use think\console\Output;
class VersionBase extends Command class VersionBase extends Command
{ {
public const VERSION = 'v2.0.107'; public const VERSION = 'v2.0.108';
public const PRODUCT_VERSION = ''; public const PRODUCT_VERSION = '';
public const LAYUI_VERSION = '2.8.17'; public const LAYUI_VERSION = '2.8.17';
public const COMMENT = [ public const COMMENT = [
'更新版本提示优先自定义的路径', '生成数据库迁移文件支持生成更新的迁移文件',
'发布新版本', '发布新版本',
]; ];

View File

@@ -26,6 +26,7 @@ class MigrateBase extends Command
->addOption('tableName', '', Option::VALUE_OPTIONAL, '要生成的表名') ->addOption('tableName', '', Option::VALUE_OPTIONAL, '要生成的表名')
->addOption('fileName', '', Option::VALUE_OPTIONAL, '要生成的文件名') ->addOption('fileName', '', Option::VALUE_OPTIONAL, '要生成的文件名')
->addOption('force', 'f', Option::VALUE_NONE, '强制生成') ->addOption('force', 'f', Option::VALUE_NONE, '强制生成')
->addOption('update', 'u', Option::VALUE_NONE, '生成新代码')
->setDescription('the curd:migrate command'); ->setDescription('the curd:migrate command');
} }
@@ -38,6 +39,7 @@ class MigrateBase extends Command
$file_name = $input->getOption('fileName'); $file_name = $input->getOption('fileName');
$table_name = $input->getOption('tableName'); $table_name = $input->getOption('tableName');
$force = $input->getOption('force'); $force = $input->getOption('force');
$update = $input->hasOption('update');
if (empty($table)) { if (empty($table)) {
$output->error('请输入表名'); $output->error('请输入表名');
@@ -68,34 +70,33 @@ class MigrateBase extends Command
} }
$dist_file_path = App::getRootPath() . '/database/migrations/' . date('YmdHis') . '_' . $file_name . '.php'; $dist_file_path = App::getRootPath() . '/database/migrations/' . date('YmdHis') . '_' . $file_name . '.php';
if(!$update){
$patt_path = App::getRootPath() . '/database/migrations/*' . $file_name . '.php';
$patt_files = glob($patt_path);
$is_extis = false;
foreach ($patt_files as $patt_file) {
$patt = '/.*database\/migrations\/\d+_' . $file_name . '.php$/';
$patt_path = App::getRootPath() . '/database/migrations/*' . $file_name . '.php'; $preg_result = preg_match($patt, $patt_file);
$patt_files = glob($patt_path); if ($preg_result) {
$is_extis = true;
$is_extis = false;
foreach ($patt_files as $patt_file) {
$patt = '/.*database\/migrations\/\d+_' . $file_name . '.php$/';
$preg_result = preg_match($patt, $patt_file);
if ($preg_result) {
$is_extis = true;
}
}
if ($is_extis) {
$output->error('文件已存在:' . $patt_files[0]);
if (!$force) {
$confirm_force = $output->confirm($input, '确定要覆盖文件吗?', false);
if (!$confirm_force) {
return;
} }
} }
$output->highlight('执行覆盖操作');
$dist_file_path = $patt_files[0]; if ($is_extis) {
$output->error('文件已存在:' . $patt_files[0]);
if (!$force) {
$confirm_force = $output->confirm($input, '确定要覆盖文件吗?如果您想生成更新文件请添加-u参数', false);
if (!$confirm_force) {
return;
}
}
$output->highlight('执行覆盖操作');
$dist_file_path = $patt_files[0];
}
} }
$columns = Db::query("SHOW FULL COLUMNS FROM {$this->tablePrefix}{$this->table}"); $columns = Db::query("SHOW FULL COLUMNS FROM {$this->tablePrefix}{$this->table}");
@@ -198,6 +199,11 @@ class MigrateBase extends Command
$data['table_keys'] = $table_keys; $data['table_keys'] = $table_keys;
$data['table_keys_uni'] = $table_keys_uni; $data['table_keys_uni'] = $table_keys_uni;
$data['table_keys_text'] = $table_keys_text; $data['table_keys_text'] = $table_keys_text;
$data['method'] = 'create';
if($update){
$data['method'] = 'update';
}
$migrate_content = View::fetch(app_file_path('common/command/curd/migrate.tpl'), $data); $migrate_content = View::fetch(app_file_path('common/command/curd/migrate.tpl'), $data);
@@ -205,5 +211,13 @@ class MigrateBase extends Command
file_put_contents(__DIR__ . '/migrate_output.php', "<?php\n\n" . $migrate_content); file_put_contents(__DIR__ . '/migrate_output.php', "<?php\n\n" . $migrate_content);
} }
file_put_contents($dist_file_path, "<?php\n\n" . $migrate_content); file_put_contents($dist_file_path, "<?php\n\n" . $migrate_content);
if($update){
$output->info('已为您生成数据库迁移代码的更新代码,但仍然包含了所有的字段,因此您需要删除多余的添加指令,并根据您的表结构修改更新的指令');
$output->info('请查看:'. $dist_file_path);
}else{
$output->info('已为您生成数据库迁移代码');
$output->info('请查看:'. $dist_file_path);
}
} }
} }

View File

@@ -35,6 +35,6 @@ class {$class_name} extends Migrator
{volist name="table_keys_text" id="vo"}->addIndex('{$vo}',['type'=>'fulltext']) {volist name="table_keys_text" id="vo"}->addIndex('{$vo}',['type'=>'fulltext'])
{/volist} {/volist}
{volist name="table_keys" id="vo"}->addIndex('{$vo}') {volist name="table_keys" id="vo"}->addIndex('{$vo}')
{/volist}->create(); {/volist}->{$method}();
} }
} }