| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <?php
- use App\Services\TelegramChannelService;
- use Illuminate\Database\Migrations\Migration;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Schema;
- return new class extends Migration {
- public function up(): void
- {
- if (!Schema::hasTable('telegram_channels')) {
- Schema::create('telegram_channels', function (Blueprint $table) {
- $table->id();
- $table->string('name', 255);
- $table->string('target', 64)->unique();
- $table->string('telegram_chat_id', 24)->nullable()->unique();
- $table->boolean('enabled');
- $table->json('message');
- $table->timestamps();
- });
- }
- if (!Schema::hasTable('telegram_channel_settings')) {
- Schema::create('telegram_channel_settings', function (Blueprint $table) {
- $table->unsignedTinyInteger('id')->primary();
- $table->unsignedBigInteger('default_channel_id')->nullable();
- });
- }
- DB::transaction(function () {
- $settings = DB::table('telegram_channel_settings');
- if ($settings->where('id', 1)->exists()) {
- return;
- }
- $defaultId = null;
- $legacy = Schema::hasTable('config') ? DB::table('config')->where('field', 'channel_message')->value('val') : null;
- $channel = TelegramChannelService::legacyChannel(json_decode($legacy ?? '', true));
- if ($channel !== null) {
- $defaultId = DB::table('telegram_channels')->insertGetId($channel + ['created_at' => now(), 'updated_at' => now()]);
- }
- $settings->insert(['id' => 1, 'default_channel_id' => $defaultId]);
- });
- }
- public function down(): void
- {
- Schema::dropIfExists('telegram_channel_settings');
- Schema::dropIfExists('telegram_channels');
- }
- };
|