Version20160715122300.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. namespace Application\Migrations\Schema\V111;
  4. use Application\Migrations\AbstractMigrationChamilo;
  5. use Doctrine\DBAL\Schema\Schema;
  6. /**
  7. * Class Version20160715122300
  8. * Add association mapping between Session and CStudentPublication
  9. * @package Application\Migrations\Schema\V111
  10. */
  11. class Version20160715122300 extends AbstractMigrationChamilo
  12. {
  13. /**
  14. * @param Schema $schema
  15. * @throws \Doctrine\DBAL\DBALException
  16. * @throws \Doctrine\DBAL\Schema\SchemaException
  17. */
  18. public function up(Schema $schema)
  19. {
  20. $this->addSql('ALTER TABLE c_student_publication CHANGE session_id session_id INT DEFAULT NULL');
  21. $this->addSql('UPDATE c_student_publication SET session_id = NULL WHERE session_id = 0');
  22. // Fix not existing session id
  23. $this->addSql('DELETE FROM c_student_publication WHERE session_id not in (SELECT id FROM session)');
  24. $this->addSql('ALTER TABLE c_student_publication ADD CONSTRAINT fk_session FOREIGN KEY (session_id) REFERENCES session (id)');
  25. }
  26. /**
  27. * @param Schema $schema
  28. * @throws \Doctrine\DBAL\DBALException
  29. * @throws \Doctrine\DBAL\Schema\SchemaException
  30. */
  31. public function down(Schema $schema)
  32. {
  33. $studentPublication = $schema->getTable('c_student_publication');
  34. $studentPublication->removeForeignKey('fk_session');
  35. $studentPublication->getColumn('session_id')->setNotnull(true);
  36. }
  37. }