2026_11_13_150000_create_telegram_channels.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. use App\Services\TelegramChannelService;
  3. use Illuminate\Database\Migrations\Migration;
  4. use Illuminate\Database\Schema\Blueprint;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Schema;
  7. return new class extends Migration {
  8. public function up(): void
  9. {
  10. if (!Schema::hasTable('telegram_channels')) {
  11. Schema::create('telegram_channels', function (Blueprint $table) {
  12. $table->id();
  13. $table->string('name', 255);
  14. $table->string('target', 64)->unique();
  15. $table->string('telegram_chat_id', 24)->nullable()->unique();
  16. $table->boolean('enabled');
  17. $table->json('message');
  18. $table->timestamps();
  19. });
  20. }
  21. if (!Schema::hasTable('telegram_channel_settings')) {
  22. Schema::create('telegram_channel_settings', function (Blueprint $table) {
  23. $table->unsignedTinyInteger('id')->primary();
  24. $table->unsignedBigInteger('default_channel_id')->nullable();
  25. });
  26. }
  27. DB::transaction(function () {
  28. $settings = DB::table('telegram_channel_settings');
  29. if ($settings->where('id', 1)->exists()) {
  30. return;
  31. }
  32. $defaultId = null;
  33. $legacy = Schema::hasTable('config') ? DB::table('config')->where('field', 'channel_message')->value('val') : null;
  34. $channel = TelegramChannelService::legacyChannel(json_decode($legacy ?? '', true));
  35. if ($channel !== null) {
  36. $defaultId = DB::table('telegram_channels')->insertGetId($channel + ['created_at' => now(), 'updated_at' => now()]);
  37. }
  38. $settings->insert(['id' => 1, 'default_channel_id' => $defaultId]);
  39. });
  40. }
  41. public function down(): void
  42. {
  43. Schema::dropIfExists('telegram_channel_settings');
  44. Schema::dropIfExists('telegram_channels');
  45. }
  46. };