update_schema.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. $cidReset = true;
  4. //require_once '../inc/global.inc.php';
  5. // Access restrictions
  6. api_protect_admin_script(true);
  7. if (api_get_configuration_value('sync_db_with_schema') != true) {
  8. api_not_allowed(true);
  9. }
  10. $em = Database::getManager();
  11. $connection = Database::getManager()->getConnection();
  12. $sm = $connection->getSchemaManager();
  13. $fromSchema = $sm->createSchema();
  14. $tool = new \Doctrine\ORM\Tools\SchemaTool(Database::getManager());
  15. $metadatas = $em->getMetadataFactory()->getAllMetadata();
  16. $toSchema = $tool->getSchemaFromMetadata($metadatas);
  17. $comparator = new \Doctrine\DBAL\Schema\Comparator();
  18. $schemaDiff = $comparator->compare($fromSchema, $toSchema);
  19. $sqlList = $schemaDiff->toSaveSql($connection->getDatabasePlatform());
  20. $content = '';
  21. if (!empty($sqlList)) {
  22. $form = new FormValidator('update');
  23. $form->addHtml(
  24. Display::return_message('If you click in update database. The SQL queries below will be executed in the database. Consider creating a DB dump before doing this.')
  25. );
  26. $form->addButtonSave(get_lang('Update database'));
  27. $content = $form->returnForm();
  28. if ($form->validate()) {
  29. error_log('---- Sync DB with schema ---');
  30. foreach ($sqlList as $sql) {
  31. Database::query($sql);
  32. error_log($sql);
  33. }
  34. error_log('---- End sync ---');
  35. Display::addFlash(
  36. Display::return_message(
  37. get_lang(count($sqlList).' queries were executed. Check your error.log'),
  38. 'success'
  39. )
  40. );
  41. header('Location: '.api_get_self());
  42. exit;
  43. }
  44. $content .= '<pre>';
  45. foreach ($sqlList as $sql) {
  46. $content .= ($sql).'; <br />';
  47. }
  48. $content .= '</pre>';
  49. } else {
  50. Display::addFlash(Display::return_message(get_lang('Nothing else to update')));
  51. }
  52. Display::display_header(get_lang('SyncDatabaseWithSchema'));
  53. echo $content ;
  54. Display::display_footer();