Version20150603181728.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. * Lp changes
  8. */
  9. class Version20150603181728 extends AbstractMigrationChamilo
  10. {
  11. /**
  12. * @param Schema $schema
  13. */
  14. public function preUp(Schema $schema)
  15. {
  16. $this->addSql("ALTER TABLE course ENGINE=InnoDB");
  17. $this->addSql("ALTER TABLE c_group_info ENGINE=InnoDB");
  18. $this->addSql("ALTER TABLE session ENGINE=InnoDB");
  19. $this->addSql("ALTER TABLE user ENGINE=InnoDB");
  20. $this->addSql("ALTER TABLE c_item_property ENGINE=InnoDB");
  21. }
  22. /**
  23. * @param Schema $schema
  24. */
  25. public function up(Schema $schema)
  26. {
  27. $table = $schema->getTable('c_lp');
  28. if (!$table->hasColumn('max_attempts')) {
  29. $this->addSql('ALTER TABLE c_lp ADD max_attempts INT NOT NULL');
  30. }
  31. if (!$table->hasColumn('subscribe_users')) {
  32. $this->addSql('ALTER TABLE c_lp ADD subscribe_users INT NOT NULL DEFAULT 0');
  33. }
  34. $this->addSql('ALTER TABLE c_item_property MODIFY c_id INT DEFAULT NULL');
  35. $this->addSql('ALTER TABLE c_item_property MODIFY lastedit_user_id INT NOT NULL');
  36. $this->addSql('ALTER TABLE c_item_property MODIFY to_group_id INT NULL');
  37. $this->addSql('ALTER TABLE c_item_property MODIFY insert_user_id INT DEFAULT NULL');
  38. $this->addSql('ALTER TABLE c_item_property MODIFY start_visible DATETIME DEFAULT NULL');
  39. $this->addSql('ALTER TABLE c_item_property MODIFY end_visible DATETIME DEFAULT NULL');
  40. $this->addSql('ALTER TABLE c_item_property MODIFY session_id INT DEFAULT NULL');
  41. $this->addSql('ALTER TABLE c_item_property MODIFY to_user_id INT NULL');
  42. $this->addSql("UPDATE c_item_property SET start_visible = NULL WHERE start_visible = '0000-00-00 00:00:00'");
  43. $this->addSql("UPDATE c_item_property SET end_visible = NULL WHERE end_visible = '0000-00-00 00:00:00'");
  44. // Remove inconsistencies about non-existing courses
  45. $this->addSql("DELETE FROM c_item_property WHERE session_id IS NOT NULL and session_id <> 0 AND session_id NOT IN (SELECT id FROM session)");
  46. $this->addSql("DELETE FROM c_item_property WHERE to_user_id IS NOT NULL and to_user_id <> 0 AND to_user_id NOT IN (SELECT id FROM user)");
  47. $this->addSql("DELETE FROM c_item_property WHERE to_user_id IS NOT NULL AND to_user_id <> 0 AND to_user_id NOT IN (SELECT id FROM user)");
  48. // Sometimes the user was deleted but we need to keep the document.
  49. // Taking first admin
  50. $this->addSql("UPDATE c_item_property SET insert_user_id = (SELECT u.user_id FROM admin a INNER JOIN user u ON (u.user_id = a.user_id AND u.active = 1) LIMIT 1) WHERE insert_user_id IS NOT NULL AND insert_user_id <> 0 AND insert_user_id NOT IN (SELECT id FROM user)");
  51. // Remove inconsistencies about non-existing users
  52. $this->addSql("DELETE FROM c_item_property WHERE c_id NOT IN (SELECT id FROM course)");
  53. // Remove inconsistencies about non-existing users
  54. $this->addSql("DELETE FROM course_rel_user WHERE user_id NOT IN (SELECT id FROM user)");
  55. $this->addSql("DELETE FROM c_item_property WHERE c_id NOT IN (SELECT id FROM course)");
  56. $this->addSql("UPDATE c_item_property SET to_group_id = NULL WHERE to_group_id = 0");
  57. $this->addSql('UPDATE c_item_property cip SET cip.to_group_id = (SELECT cgi.iid FROM c_group_info cgi WHERE cgi.c_id = cip.c_id AND cgi.id = cip.to_group_id)');
  58. $this->addSql("DELETE FROM c_item_property WHERE to_group_id IS NOT NULL AND to_group_id <> 0 AND to_group_id NOT IN (SELECT iid FROM c_group_info)");
  59. $this->addSql('UPDATE c_item_property SET to_user_id = NULL WHERE to_user_id = 0');
  60. $this->addSql('UPDATE c_item_property SET insert_user_id = NULL WHERE insert_user_id = 0');
  61. $this->addSql('UPDATE c_item_property SET session_id = NULL WHERE session_id = 0');
  62. $this->addSql('ALTER TABLE c_item_property ADD CONSTRAINT FK_1D84C18191D79BD3 FOREIGN KEY (c_id) REFERENCES course(id)');
  63. $this->addSql('ALTER TABLE c_item_property ADD CONSTRAINT FK_1D84C181330D47E9 FOREIGN KEY (to_group_id) REFERENCES c_group_info (iid)');
  64. $this->addSql('ALTER TABLE c_item_property ADD CONSTRAINT FK_1D84C18129F6EE60 FOREIGN KEY (to_user_id) REFERENCES user (id)');
  65. $this->addSql('ALTER TABLE c_item_property ADD CONSTRAINT FK_1D84C1819C859CC3 FOREIGN KEY (insert_user_id) REFERENCES user (id)');
  66. $this->addSql('ALTER TABLE c_item_property ADD CONSTRAINT FK_1D84C181613FECDF FOREIGN KEY (session_id) REFERENCES session (id)');
  67. $this->addSql('CREATE INDEX IDX_1D84C18191D79BD3 ON c_item_property (c_id)');
  68. $this->addSql('CREATE INDEX IDX_1D84C181330D47E9 ON c_item_property (to_group_id)');
  69. $this->addSql('CREATE INDEX IDX_1D84C18129F6EE60 ON c_item_property (to_user_id)');
  70. $this->addSql('CREATE INDEX IDX_1D84C1819C859CC3 ON c_item_property (insert_user_id)');
  71. $this->addSql('CREATE INDEX IDX_1D84C181613FECDF ON c_item_property (session_id)');
  72. }
  73. /**
  74. * @param Schema $schema
  75. */
  76. public function down(Schema $schema)
  77. {
  78. $this->addSql('ALTER TABLE c_lp DROP max_attempts, DROP subscribe_users');
  79. $this->addSql('ALTER TABLE c_item_property DROP FOREIGN KEY FK_1D84C18191D79BD3');
  80. $this->addSql('ALTER TABLE c_item_property DROP FOREIGN KEY FK_1D84C181330D47E9');
  81. $this->addSql('ALTER TABLE c_item_property DROP FOREIGN KEY FK_1D84C18129F6EE60');
  82. $this->addSql('ALTER TABLE c_item_property DROP FOREIGN KEY FK_1D84C1819C859CC3');
  83. $this->addSql('ALTER TABLE c_item_property DROP FOREIGN KEY FK_1D84C181613FECDF');
  84. $this->addSql('DROP INDEX IDX_1D84C18191D79BD3 ON c_item_property');
  85. $this->addSql('DROP INDEX IDX_1D84C181330D47E9 ON c_item_property');
  86. $this->addSql('DROP INDEX IDX_1D84C18129F6EE60 ON c_item_property');
  87. $this->addSql('DROP INDEX IDX_1D84C1819C859CC3 ON c_item_property');
  88. $this->addSql('DROP INDEX IDX_1D84C181613FECDF ON c_item_property');
  89. $this->addSql('ALTER TABLE c_item_property CHANGE c_id c_id INT NOT NULL, CHANGE insert_user_id insert_user_id INT NOT NULL, CHANGE session_id session_id INT NOT NULL, CHANGE start_visible start_visible DATETIME NOT NULL, CHANGE end_visible end_visible DATETIME NOT NULL');
  90. }
  91. }