status !== 'active') { throw new CloudError('当前实例状态不允许修改名称', 409); } $initialName = (string) $instance->name; return $instance->getConnection()->transaction(static function () use ( $instance, $initialName, $name ): array { // Compare under a row lock in PHP. The production column uses a // case-insensitive collation, so a SQL name predicate cannot be a // safe compare-and-swap guard for case-preserving display names. $current = CloudInstance::where('id', (int) $instance->id) ->where('uuid', (string) $instance->uuid) ->where('user_id', (int) $instance->user_id) ->lockForUpdate() ->first(['id', 'name', 'status']); if ($current === null || (string) $current->status !== 'active') { throw new CloudError('实例状态已变化,请刷新后重试', 409); } $currentName = (string) $current->name; if (hash_equals($currentName, $name)) { return ['id' => (string) $current->id, 'name' => $name]; } if (!hash_equals($currentName, $initialName)) { throw new CloudError('实例名称已变化,请刷新后重试', 409); } $current->name = $name; $current->updated_at = time(); $current->save(); return ['id' => (string) $current->id, 'name' => $name]; }); } }