diff --git a/app/Console/Commands/DeploySettingsConstCommand.php b/app/Console/Commands/DeploySettingsConstCommand.php index 5a408c3..fe9e84a 100644 --- a/app/Console/Commands/DeploySettingsConstCommand.php +++ b/app/Console/Commands/DeploySettingsConstCommand.php @@ -2,6 +2,7 @@ namespace App\Console\Commands; +use App\Services\SettingsRuntimeCanEditSync; use Illuminate\Console\Command; use Symfony\Component\Process\Process; @@ -100,6 +101,8 @@ class DeploySettingsConstCommand extends Command return self::FAILURE; } + $this->syncRuntimeCanEdit($db); + $this->info(" done: {$db}"); } @@ -129,6 +132,38 @@ class DeploySettingsConstCommand extends Command return $path; } + private function syncRuntimeCanEdit(string $database): void + { + try { + $sync = app(SettingsRuntimeCanEditSync::class); + $sync->useDatabase($database); + $updated = $sync->sync(); + $this->line(" runtime can_edit sync: {$updated} row(s) updated"); + + $status = $sync->statusMap(); + if ($status !== []) { + $preview = collect($status) + ->map(fn ($canEdit, $key) => "{$key}={$canEdit}") + ->implode(', '); + $this->line(" runtime keys: {$preview}"); + } + + $violations = $sync->violations(); + if ($violations !== []) { + $this->warn(' WARN: runtime keys still can_edit≠1: '.implode(', ', array_keys($violations))); + } + + $fee = \App\Models\Setting::query() + ->where('key', 'fee_deduct_from_gold') + ->first(['key', 'can_edit']); + if ($fee) { + $this->line(" check fee_deduct_from_gold can_edit={$fee->can_edit}"); + } + } catch (\Throwable $e) { + $this->warn(' runtime can_edit sync skipped: '.$e->getMessage()); + } + } + private function findMysqlBinary(): ?string { $process = new Process(['command', '-v', 'mysql']); diff --git a/app/Console/Commands/SyncRuntimeSettingsCanEditCommand.php b/app/Console/Commands/SyncRuntimeSettingsCanEditCommand.php new file mode 100644 index 0000000..daa9506 --- /dev/null +++ b/app/Console/Commands/SyncRuntimeSettingsCanEditCommand.php @@ -0,0 +1,43 @@ +argument('database') ?: config('database.connections.'.config('database.default').'.database', ''))); + if ($database === '') { + $this->error('DB_DATABASE خالی است یا database را بدهید.'); + + return self::FAILURE; + } + + $sync->useDatabase($database); + $updated = $sync->sync(); + $this->info("DB {$database}: {$updated} row(s) updated"); + + foreach ($sync->statusMap() as $key => $canEdit) { + $this->line(" {$key} => can_edit={$canEdit}"); + } + + $violations = $sync->violations(); + if ($violations !== []) { + $this->error('هنوز can_edit≠1: '.implode(', ', array_keys($violations))); + + return self::FAILURE; + } + + $this->info('OK — همه کلیدهای runtime can_edit=1'); + + return self::SUCCESS; + } +} diff --git a/app/Services/SettingsRuntimeCanEditSync.php b/app/Services/SettingsRuntimeCanEditSync.php new file mode 100644 index 0000000..8aa69f6 --- /dev/null +++ b/app/Services/SettingsRuntimeCanEditSync.php @@ -0,0 +1,75 @@ + */ + 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 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 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); + } +} diff --git a/database/sql/DEPLOY_CHECKLIST.md b/database/sql/DEPLOY_CHECKLIST.md index 79a5f9a..bfa44fe 100644 --- a/database/sql/DEPLOY_CHECKLIST.md +++ b/database/sql/DEPLOY_CHECKLIST.md @@ -98,6 +98,9 @@ php artisan settings:deploy | `2019`/`2021` No بعد از baseline | baseline قدیمی → `fake_package_migrations.sql` | | `Unknown host HOST` | از `.env` بخوان نه placeholder | | `Unknown column 'total_referrals'` | `git pull` + `php artisan migrate` (ensure_total_referrals) | +| `montzar_*` هنوز `can_edit=0` در Navicat | **`git pull` روی server** سپس `php artisan deploy:tenant` — باید `runtime can_edit sync` و `montzar_token=1` چاپ شود | +| گروه Montzar در تنظیمات عادی نیست | **`git pull` + build ادمین** (`ed326a4+`) + `price_provider=montzar` در سوپرادمین | +| کلید runtime در سوپرادمین نیست (عمدی) | API `19d28f2+` + `npm run replicas:sync`؛ web سوپرادمین هم `git pull` + build | | `Unknown column 'order_limit_enabled'` / محدودیت سفارش ذخیره نمی‌شود | `git pull` + `php artisan migrate` (ensure_products_baseline_columns) | | `Unknown column 'product_id'` روی `wallets` / `/customer/me` | `mysql ... < database/sql/gholaktala_upgrade.sql` سپس merge settings | | upgrade کامل قلک طلا | `bash database/sql/run_gholaktala_upgrade.sh` | diff --git a/database/sql/deploy_settings_const.sql b/database/sql/deploy_settings_const.sql index 96d807b..c425f7f 100644 --- a/database/sql/deploy_settings_const.sql +++ b/database/sql/deploy_settings_const.sql @@ -2168,6 +2168,10 @@ UPDATE `settings` SET `can_edit` = 1 WHERE `key` IN ( 'up_price', 'down_price', 'price_extreme_date', 'do_send_liman' ); +SELECT `key`, `can_edit` FROM `settings` +WHERE `key` IN ('montzar_token', 'montzar_diff', 'montzar_open_buy') +ORDER BY `key`; + COMMIT; SELECT 'deploy_settings_const.sql finished' AS status; diff --git a/database/sql/deploy_settings_const_preserve_value_sync_can_edit.sql b/database/sql/deploy_settings_const_preserve_value_sync_can_edit.sql index 12e6d32..59c774a 100644 --- a/database/sql/deploy_settings_const_preserve_value_sync_can_edit.sql +++ b/database/sql/deploy_settings_const_preserve_value_sync_can_edit.sql @@ -1593,6 +1593,15 @@ WHERE `key` = 'accounting_provider' AND TRIM(COALESCE(`value`, '')) IN ('', 'none') AND EXISTS (SELECT 1 FROM (SELECT `value` FROM `settings` WHERE `key` = 'send_to_tahesab') AS s WHERE s.`value` IN ('true', '1', 'yes')); +-- آخرین override: کلیدهای runtime همیشه can_edit=1 (tenant-only) +UPDATE `settings` SET `can_edit` = 1 WHERE `key` IN ( + 'montzar_last_product_log', 'montzar_order_response', + 'montzar_live_price', 'montzar_mazaneh', 'montzar_diff', + 'montzar_truly_buy_mazaneh', 'montzar_truly_sell_mazaneh', + 'montzar_token', 'montzar_closed', + 'up_price', 'down_price', 'price_extreme_date', 'do_send_liman' +); + COMMIT; SELECT 'deploy_settings_const.sql finished' AS status;