Version20150528103216.php 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. namespace Application\Migrations\Schema\V110;
  4. use Application\Migrations\AbstractMigrationChamilo;
  5. use Doctrine\DBAL\Schema\Schema;
  6. /**
  7. * Session date changes
  8. */
  9. class Version20150528103216 extends AbstractMigrationChamilo
  10. {
  11. /**
  12. * @param Schema $schema
  13. */
  14. public function up(Schema $schema)
  15. {
  16. $this->addSql('ALTER TABLE session ADD COLUMN access_start_date datetime');
  17. $this->addSql('ALTER TABLE session ADD COLUMN access_end_date datetime');
  18. $this->addSql('ALTER TABLE session ADD COLUMN coach_access_start_date datetime');
  19. $this->addSql('ALTER TABLE session ADD COLUMN coach_access_end_date datetime');
  20. $this->addSql('ALTER TABLE session ADD COLUMN display_start_date datetime');
  21. $this->addSql('ALTER TABLE session ADD COLUMN display_end_date datetime');
  22. $this->addSql('UPDATE session SET access_start_date = date_start');
  23. $this->addSql("UPDATE session SET access_end_date = CONVERT(CONCAT(date_end, ' 23:59:59'), DATETIME)");
  24. $this->addSql('UPDATE session SET coach_access_start_date = CONVERT(DATE_SUB(date_start, INTERVAL nb_days_access_before_beginning DAY), DATETIME) ');
  25. $this->addSql('UPDATE session SET coach_access_start_date = NULL WHERE nb_days_access_before_beginning = 0');
  26. $this->addSql('UPDATE session SET coach_access_end_date = CONVERT(DATE_ADD(date_end, INTERVAL nb_days_access_after_end DAY), DATETIME) ');
  27. $this->addSql('UPDATE session SET coach_access_end_date = NULL WHERE nb_days_access_after_end = 0');
  28. $this->addSql('UPDATE session SET display_start_date = access_start_date');
  29. $this->addSql('UPDATE session SET display_end_date = access_end_date');
  30. // Set dates to NULL
  31. $this->addSql('UPDATE session SET access_start_date = NULL WHERE access_start_date = "0000-00-00 00:00:00"');
  32. $this->addSql('UPDATE session SET access_end_date = NULL WHERE access_end_date = "0000-00-00 00:00:00"');
  33. $this->addSql('UPDATE session SET coach_access_start_date = NULL WHERE coach_access_start_date = "0000-00-00 00:00:00"');
  34. $this->addSql('UPDATE session SET coach_access_end_date = NULL WHERE coach_access_end_date = "0000-00-00 00:00:00"');
  35. $this->addSql('UPDATE session SET display_start_date = NULL WHERE display_start_date = "0000-00-00 00:00:00"');
  36. $this->addSql('UPDATE session SET display_end_date = NULL WHERE display_end_date = "0000-00-00 00:00:00"');
  37. }
  38. /**
  39. * @param Schema $schema
  40. */
  41. public function down(Schema $schema)
  42. {
  43. $this->addSql('ALTER TABLE session CREATE date_start date NOT NULL');
  44. $this->addSql('ALTER TABLE session CREATE date_end date NOT NULL');
  45. $this->addSql('ALTER TABLE session CREATE nb_days_access_before_beginning TINYINT');
  46. $this->addSql('ALTER TABLE session CREATE nb_days_access_after_end TINYINT');
  47. $this->addSql('UPDATE session SET date_start = access_start_date');
  48. $this->addSql('UPDATE session SET date_end = access_end_date');
  49. $this->addSql('UPDATE session SET nb_days_access_before_beginning = DATEDIFF(access_start_date, coach_access_start_date) WHERE access_start_date != coach_access_start_date AND coach_access_start_date IS NOT NULL');
  50. $this->addSql('UPDATE session SET nb_days_access_after_end = DATEDIFF(coach_access_end_date, coach_access_end_date) WHERE access_end_date != coach_access_end_date AND coach_access_end_date IS NOT NULL');
  51. $this->addSql('UPDATE session SET nb_days_access_before_beginning = 0 WHERE access_start_date = coach_access_start_date OR coach_access_start_date IS NULL');
  52. $this->addSql('UPDATE session SET nb_days_access_after_end = 0 WHERE access_end_date = coach_access_end_date OR coach_access_end_date IS NULL');
  53. $this->addSql('ALTER TABLE session DROP access_start_date');
  54. $this->addSql('ALTER TABLE session DROP access_end_date');
  55. $this->addSql('ALTER TABLE session DROP coach_access_start_date');
  56. $this->addSql('ALTER TABLE session DROP coach_access_end_date');
  57. $this->addSql('ALTER TABLE session DROP display_start_date');
  58. $this->addSql('ALTER TABLE session DROP display_end_date');
  59. }
  60. }