From edad0ffebfed3097ecf25f740d859bd8bb17b7ef Mon Sep 17 00:00:00 2001 From: AIZAWA Hina Date: Sun, 1 Dec 2024 01:18:54 +0900 Subject: [PATCH] Make season data --- migrations/m241130_140301_seasons.php | 100 ++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 migrations/m241130_140301_seasons.php diff --git a/migrations/m241130_140301_seasons.php b/migrations/m241130_140301_seasons.php new file mode 100644 index 000000000..1d7bc62d3 --- /dev/null +++ b/migrations/m241130_140301_seasons.php @@ -0,0 +1,100 @@ + + */ + +declare(strict_types=1); + +use app\components\db\Migration; +use yii\db\Expression; + +final class m241130_140301_seasons extends Migration +{ + /** + * @inheritdoc + */ + public function safeUp() + { + $data = []; + foreach (range(2024, 2030) as $year) { + foreach ([3, 6, 9, 12] as $month) { + if ($record = $this->makeRecord($year, $month)) { + $data[] = $record; + } + } + } + + $this->batchInsert( + '{{%season3}}', + ['key', 'name', 'start_at', 'end_at', 'term'], + $data, + ); + + return true; + } + + /** + * @inheritdoc + */ + public function safeDown() + { + $this->delete( + '{{%season3}}', + ['and', + ['>=', 'start_at', '2024-12-01T00:00:00+00:00'], + ], + ); + + return true; + } + + /** + * @return array{string, string, string, string, Expression}|null + */ + private function makeRecord(int $year, int $month): ?array + { + if ($year === 2024 && $month < 12) { + return null; + } + + $start = (new DateTimeImmutable('now', new DateTimeZone('Etc/UTC'))) + ->setDate($year, $month, 1) + ->setTime(0, 0, 0); + $end = $start->add(new DateInterval('P3M')); + + return [ + sprintf('season%s', $start->format('Ym')), + vsprintf('%s Season %04d', [ + match ((int)$start->format('n')) { + 3 => 'Fresh', + 6 => 'Sizzle', + 9 => 'Drizzle', + 12 => 'Chill', + }, + (int)$start->format('Y'), + ]), + $start->format(DateTime::ATOM), + $end->format(DateTime::ATOM), + new Expression( + vsprintf('tstzrange(%s, %s, %s)', [ + $this->db->quoteValue($start->format(DateTime::ATOM)), + $this->db->quoteValue($end->format(DateTime::ATOM)), + $this->db->quoteValue('[)'), + ]), + ), + ]; + } + + /** + * @inheritdoc + */ + protected function vacuumTables(): array + { + return [ + '{{%season3}}', + ]; + } +}