fix(settings): PHP sync for runtime can_edit after deploy
Force can_edit=1 for Montzar/price runtime keys via settings:deploy and settings:sync-runtime-can-edit, so tenant DB stays correct even when an older deploy SQL is still on the server. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Setting;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* تثبیت can_edit=1 برای کلیدهای runtime tenant (Montzar، up/down_price، …).
|
||||
* بعد از deploy_settings_const.sql هم اجرا میشود تا حتی با SQL قدیمی روی سرور درست بماند.
|
||||
*/
|
||||
class SettingsRuntimeCanEditSync
|
||||
{
|
||||
/** @return list<string> */
|
||||
public function keys(): array
|
||||
{
|
||||
return SuperAdminProgramSettingsOverlay::RUNTIME_KEYS;
|
||||
}
|
||||
|
||||
public function sync(): int
|
||||
{
|
||||
$keys = $this->keys();
|
||||
if ($keys === []) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return Setting::query()
|
||||
->whereIn('key', $keys)
|
||||
->where(function ($query) {
|
||||
$query->where('can_edit', '!=', 1)
|
||||
->orWhereNull('can_edit');
|
||||
})
|
||||
->update(['can_edit' => 1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed> key => can_edit for rows still not 1
|
||||
*/
|
||||
public function violations(): array
|
||||
{
|
||||
$keys = $this->keys();
|
||||
if ($keys === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return Setting::query()
|
||||
->whereIn('key', $keys)
|
||||
->where(function ($query) {
|
||||
$query->where('can_edit', '!=', 1)
|
||||
->orWhereNull('can_edit');
|
||||
})
|
||||
->orderBy('key')
|
||||
->pluck('can_edit', 'key')
|
||||
->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed> key => can_edit for all runtime keys present in DB
|
||||
*/
|
||||
public function statusMap(): array
|
||||
{
|
||||
return Setting::query()
|
||||
->whereIn('key', $this->keys())
|
||||
->orderBy('key')
|
||||
->pluck('can_edit', 'key')
|
||||
->all();
|
||||
}
|
||||
|
||||
public function useDatabase(string $database): void
|
||||
{
|
||||
$connection = (string) config('database.default', 'mysql');
|
||||
config(["database.connections.{$connection}.database" => $database]);
|
||||
DB::purge($connection);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user