<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
    public function up(): void
    {
        // Сначала удаляем, если уже существует
        DB::statement("DROP VIEW IF EXISTS `all_news_view` ");

        // Создаем вьюху
        DB::statement("
            CREATE VIEW `all_news_view` AS
            SELECT
                `id`,
                DATE_FORMAT(`date`, '%m') AS `month`,
                DATE_FORMAT(`date`, '%d') AS `day`,
                DATE_FORMAT(`date`, '%H:%i') AS `time`,
                `header`,
                `long_text` AS `article`,
                `from_id` AS `author`,
                `img_src`,
                `author_type`,
                `copy_post_id`,
                `post_type`
            FROM
                `news`
            WHERE
                `long_text` <> ''
            ORDER BY `date` DESC
        ");
    }

    public function down(): void
    {
        DB::statement("DROP VIEW IF EXISTS `all_news_view` ");
    }
};
