20230508103736_populate_test_dts_rejected_field.php 937 B

12345678910111213141516171819202122232425
  1. <?php
  2. use Carbon\CarbonImmutable;
  3. use Phinx\Migration\AbstractMigration;
  4. class PopulateTestDtsRejectedField extends AbstractMigration {
  5. public function up() {
  6. $rejects = $this->fetchAll("SELECT `location_id`, MAX(`dts_last_rejected`) AS `dts_last_rejected` FROM `user_location_reject` GROUP BY `location_id`");
  7. foreach($rejects as $reject) {
  8. $location_id = $reject["location_id"];
  9. $dts_rejected = $reject["dts_last_rejected"];
  10. $dts_threshold = (string) CarbonImmutable::parse($dts_rejected, "UTC")->subDays(30);
  11. $tests = $this->fetchAll("SELECT `id` FROM `test` WHERE `location_id`='$location_id' AND `status`='1' AND `dts_created` > '$dts_threshold'");
  12. foreach($tests as $test) {
  13. $test_id = $test["id"];
  14. $this->execute("UPDATE `test` SET `dts_rejected`='$dts_rejected' WHERE `id`='$test_id'");
  15. }
  16. }
  17. }
  18. public function down() {
  19. $this->execute("UPDATE `test` SET `dts_rejected`=NULL WHERE 1");
  20. }
  21. }