ThinkAdmin/plugin/think-plugs-worker/stc/database/20241010000008_install_worker20241010.php
Anyon e7a8c05556 chore(repo): 统一 v8 仓库品牌名称
将 v8 重构分支中残留的 ThinkAdminDeveloper 文本统一调整为 ThinkAdmin,避免迁移到主仓库后继续暴露旧开发仓库名称。

主要内容:

- 更新 README 标题与项目描述。

- 统一 PHP 文件头注释中的项目标识。

- 同步调整测试、配置、插件与文档中的旧仓库名称文本。

- 保持旧包删除说明与架构边界测试语义不变,只清理品牌名称残留。
2026-05-08 16:15:24 +08:00

124 lines
5.5 KiB
PHP

<?php
declare(strict_types=1);
/**
* +----------------------------------------------------------------------
* | ThinkAdmin Plugin
* +----------------------------------------------------------------------
* | Copyright (c) 2014~2026 ThinkAdmin [ thinkadmin.top ]
* +----------------------------------------------------------------------
* | Official Website: https://thinkadmin.top
* +----------------------------------------------------------------------
* | Licensed: https://mit-license.org
* | Disclaimer: https://thinkadmin.top/disclaimer
* | Vip Rights: https://thinkadmin.top/vip-introduce
* +----------------------------------------------------------------------
* | Gitee Repository: https://gitee.com/zoujingli/ThinkAdmin
* | Github Repository: https://github.com/zoujingli/ThinkAdmin
* +----------------------------------------------------------------------
*/
use Phinx\Db\Table;
use think\migration\Migrator;
@set_time_limit(0);
@ini_set('memory_limit', '-1');
class InstallWorker20241010 extends Migrator
{
public function getName(): string
{
return 'WorkerPlugin';
}
public function change(): void
{
$this->createSystemQueue();
}
private function createSystemQueue(): void
{
$table = $this->table('system_queue', [
'engine' => 'InnoDB',
'collation' => 'utf8mb4_general_ci',
'comment' => 'System queue task table',
]);
$this->upgrade($table, [
['code', 'string', ['limit' => 20, 'default' => '', 'null' => false, 'comment' => 'Queue code']],
['exec_hash', 'string', ['limit' => 40, 'default' => '', 'null' => false, 'comment' => 'Singleton execution hash']],
['title', 'string', ['limit' => 100, 'default' => '', 'null' => false, 'comment' => 'Queue title']],
['command', 'string', ['limit' => 500, 'default' => '', 'null' => true, 'comment' => 'Queue command']],
['exec_pid', 'biginteger', ['limit' => 20, 'default' => 0, 'null' => true, 'comment' => 'Worker pid']],
['exec_data', 'text', ['default' => null, 'null' => true, 'comment' => 'Execution payload']],
['exec_time', 'biginteger', ['limit' => 20, 'default' => 0, 'null' => true, 'comment' => 'Scheduled time']],
['exec_desc', 'string', ['limit' => 500, 'default' => '', 'null' => true, 'comment' => 'Execution summary']],
['enter_time', 'decimal', ['precision' => 20, 'scale' => 4, 'default' => '0.0000', 'null' => true, 'comment' => 'Start time']],
['outer_time', 'decimal', ['precision' => 20, 'scale' => 4, 'default' => '0.0000', 'null' => true, 'comment' => 'Finish time']],
['loops_time', 'biginteger', ['limit' => 20, 'default' => 0, 'null' => true, 'comment' => 'Loop interval']],
['attempts', 'biginteger', ['limit' => 20, 'default' => 0, 'null' => true, 'comment' => 'Attempt count']],
['message', 'text', ['default' => null, 'null' => true, 'comment' => 'Progress snapshot']],
['status', 'integer', ['limit' => 1, 'default' => 1, 'null' => true, 'comment' => '1 waiting, 2 running, 3 done, 4 failed']],
['create_time', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'null' => false, 'comment' => 'Created at']],
], [
['columns' => ['code'], 'options' => ['unique' => true, 'name' => 'uq_system_queue_code']],
['columns' => ['exec_hash', 'status'], 'options' => ['name' => 'idx_system_queue_hash_status']],
['columns' => ['status', 'exec_time', 'id'], 'options' => ['name' => 'idx_system_queue_status_exec']],
['columns' => ['status', 'enter_time'], 'options' => ['name' => 'idx_system_queue_status_enter']],
['columns' => ['title'], 'options' => ['name' => 'idx_system_queue_title']],
['columns' => ['create_time'], 'options' => ['name' => 'idx_system_queue_create_time']],
], true);
}
/**
* @param array<int, array<int|string, mixed>> $fields
* @param array<int, array<int|string, mixed>|string> $indexes
*/
private function upgrade(Table $table, array $fields, array $indexes = [], bool $force = false): Table
{
$existing = [];
if ($exists = $table->exists()) {
if (!$force) {
return $table;
}
foreach ($table->getColumns() as $column) {
$existing[] = $column->getName();
}
}
foreach ($fields as $field) {
if (in_array($field[0], $existing, true)) {
$table->changeColumn($field[0], ...array_slice($field, 1));
} else {
$table->addColumn($field[0], ...array_slice($field, 1));
}
}
foreach ($indexes as $index) {
if (is_string($index)) {
$columns = [$index];
$options = [];
} elseif (array_is_list($index) && isset($index[0]) && is_string($index[0])) {
$columns = $index;
$options = [];
} else {
$columns = array_values((array)($index['columns'] ?? []));
$options = (array)($index['options'] ?? []);
}
if ($columns === [] || ($exists && $table->hasIndex($columns))) {
continue;
}
$table->addIndex($columns, $options);
}
$exists ? $table->update() : $table->create();
if ($table->hasColumn('id')) {
$table->changeColumn('id', 'integer', ['limit' => 11, 'identity' => true]);
}
return $table;
}
}