mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-01 15:32:48 +08:00
生成数据库迁移文件支持生成更新的迁移文件
This commit is contained in:
@@ -12,14 +12,14 @@ use think\console\Output;
|
||||
|
||||
class VersionBase extends Command
|
||||
{
|
||||
public const VERSION = 'v2.0.107';
|
||||
public const VERSION = 'v2.0.108';
|
||||
|
||||
public const PRODUCT_VERSION = '';
|
||||
|
||||
public const LAYUI_VERSION = '2.8.17';
|
||||
|
||||
public const COMMENT = [
|
||||
'更新版本提示优先自定义的路径',
|
||||
'生成数据库迁移文件支持生成更新的迁移文件',
|
||||
'发布新版本',
|
||||
];
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ class MigrateBase extends Command
|
||||
->addOption('tableName', '', Option::VALUE_OPTIONAL, '要生成的表名')
|
||||
->addOption('fileName', '', Option::VALUE_OPTIONAL, '要生成的文件名')
|
||||
->addOption('force', 'f', Option::VALUE_NONE, '强制生成')
|
||||
->addOption('update', 'u', Option::VALUE_NONE, '生成新代码')
|
||||
->setDescription('the curd:migrate command');
|
||||
}
|
||||
|
||||
@@ -38,6 +39,7 @@ class MigrateBase extends Command
|
||||
$file_name = $input->getOption('fileName');
|
||||
$table_name = $input->getOption('tableName');
|
||||
$force = $input->getOption('force');
|
||||
$update = $input->hasOption('update');
|
||||
|
||||
if (empty($table)) {
|
||||
$output->error('请输入表名');
|
||||
@@ -68,34 +70,33 @@ class MigrateBase extends Command
|
||||
}
|
||||
|
||||
$dist_file_path = App::getRootPath() . '/database/migrations/' . date('YmdHis') . '_' . $file_name . '.php';
|
||||
|
||||
$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$/';
|
||||
|
||||
$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;
|
||||
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$/';
|
||||
|
||||
$preg_result = preg_match($patt, $patt_file);
|
||||
|
||||
if ($preg_result) {
|
||||
$is_extis = true;
|
||||
}
|
||||
}
|
||||
$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}");
|
||||
@@ -198,6 +199,11 @@ class MigrateBase extends Command
|
||||
$data['table_keys'] = $table_keys;
|
||||
$data['table_keys_uni'] = $table_keys_uni;
|
||||
$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);
|
||||
|
||||
@@ -205,5 +211,13 @@ class MigrateBase extends Command
|
||||
file_put_contents(__DIR__ . '/migrate_output.php', "<?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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,6 @@ class {$class_name} extends Migrator
|
||||
{volist name="table_keys_text" id="vo"}->addIndex('{$vo}',['type'=>'fulltext'])
|
||||
{/volist}
|
||||
{volist name="table_keys" id="vo"}->addIndex('{$vo}')
|
||||
{/volist}->create();
|
||||
{/volist}->{$method}();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user