summaryrefslogtreecommitdiff
path: root/database/migrations/2020_09_18_200839_fix_view_to_include_unaired.php
blob: 5f7e1a2d98e692bd844716e793b2a92c36c66b25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class FixViewToIncludeUnaired extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('anime', function (Blueprint $table) {
	    DB::statement(
		    "CREATE OR REPLACE VIEW view_anime_index AS
		    SELECT DISTINCT
			a.mal_id,
			url,
			image_url,
			(CASE WHEN title_eng IS NOT NULL THEN title_eng
			ELSE title_pref END) as title,
			title_pref,
			title_eng,
			anime_type,
			a.created_at,
			a.updated_at,
			episodes,
			synopsis,
			hashtag,
			airing_status,
			a.season_name,
			a.season_year,
			(SELECT score FROM stats as b WHERE b.mal_id = a.mal_id AND score IS NOT NULL ORDER BY created_at ASC LIMIT 1) as score_begin,
			(SELECT score FROM stats as b WHERE b.mal_id = a.mal_id ORDER BY created_at DESC LIMIT 1) as score_today,
			(SELECT watching FROM stats as b WHERE b.mal_id = a.mal_id ORDER BY created_at DESC LIMIT 1) as watching,
			(SELECT members FROM stats as b WHERE b.mal_id = a.mal_id ORDER BY created_at DESC LIMIT 1) as members
			FROM
				anime as a
			JOIN
				stats as s ON a.mal_id = s.mal_id
			ORDER BY
				abs(score_today-score_begin) DESC,
				airing_status ASC
		    ;"
	    );
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('anime', function (Blueprint $table) {
	    DB::statement(
		    "CREATE OR REPLACE VIEW view_anime_index AS
		    SELECT DISTINCT
			a.mal_id,
			url,
			image_url,
			(CASE WHEN title_eng IS NOT NULL THEN title_eng
			ELSE title_pref END) as title,
			title_pref,
			title_eng,
			anime_type,
			a.created_at,
			a.updated_at,
			episodes,
			synopsis,
			hashtag,
			airing_status,
			a.season_name,
			a.season_year,
			(SELECT score FROM stats as b WHERE b.mal_id = a.mal_id AND score IS NOT NULL ORDER BY created_at ASC LIMIT 1) as score_begin,
			(SELECT score FROM stats as b WHERE b.mal_id = a.mal_id ORDER BY created_at DESC LIMIT 1) as score_today,
			(SELECT watching FROM stats as b WHERE b.mal_id = a.mal_id ORDER BY created_at DESC LIMIT 1) as watching,
			(SELECT members FROM stats as b WHERE b.mal_id = a.mal_id ORDER BY created_at DESC LIMIT 1) as members
			FROM
				anime as a
			JOIN
				stats as s ON a.mal_id = s.mal_id
			WHERE
				score IS NOT null
			ORDER BY
				abs(score_today-score_begin) DESC,
				airing_status ASC
		    ;"
	    );
        });
    }
}