You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DatabaseMigrator.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * SonarQube is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.db.migrations;
  21. import java.sql.Connection;
  22. import org.apache.commons.dbutils.DbUtils;
  23. import org.apache.ibatis.session.SqlSession;
  24. import org.picocontainer.Startable;
  25. import org.sonar.api.ServerComponent;
  26. import org.sonar.api.platform.ServerUpgradeStatus;
  27. import org.sonar.api.utils.log.Loggers;
  28. import org.sonar.core.persistence.DdlUtils;
  29. import org.sonar.server.db.DbClient;
  30. import com.google.common.annotations.VisibleForTesting;
  31. import org.sonar.server.plugins.ServerPluginRepository;
  32. /**
  33. * Restore schema by executing DDL scripts. Only H2 database is supported.
  34. * Other databases are created by Ruby on Rails migrations.
  35. *
  36. * @since 2.12
  37. */
  38. public class DatabaseMigrator implements ServerComponent, Startable {
  39. private final DbClient dbClient;
  40. private final MigrationStep[] migrations;
  41. private final ServerUpgradeStatus serverUpgradeStatus;
  42. /**
  43. * ServerPluginInstaller is used to ensure H2 schema creation is done only after copy of bundle plugins have been done
  44. */
  45. public DatabaseMigrator(DbClient dbClient, MigrationStep[] migrations, ServerUpgradeStatus serverUpgradeStatus,
  46. ServerPluginRepository unused) {
  47. this.dbClient = dbClient;
  48. this.migrations = migrations;
  49. this.serverUpgradeStatus = serverUpgradeStatus;
  50. }
  51. @Override
  52. public void start() {
  53. createDatabase();
  54. }
  55. @Override
  56. public void stop() {
  57. // Nothing to do
  58. }
  59. /**
  60. * @return true if the database has been created, false if this database is not supported or if database has already been created
  61. */
  62. @VisibleForTesting
  63. boolean createDatabase() {
  64. if (DdlUtils.supportsDialect(dbClient.database().getDialect().getId()) && serverUpgradeStatus.isFreshInstall()) {
  65. Loggers.get(getClass()).info("Create database");
  66. SqlSession session = dbClient.openSession(false);
  67. Connection connection = null;
  68. try {
  69. connection = session.getConnection();
  70. createSchema(connection, dbClient.database().getDialect().getId());
  71. return true;
  72. } finally {
  73. session.close();
  74. // The connection is probably already closed by session.close()
  75. // but it's not documented in mybatis javadoc.
  76. DbUtils.closeQuietly(connection);
  77. }
  78. }
  79. return false;
  80. }
  81. public void executeMigration(String className) {
  82. MigrationStep migration = getMigration(className);
  83. try {
  84. migration.execute();
  85. } catch (Exception e) {
  86. // duplication between log and exception because webapp does not correctly log initial stacktrace
  87. String msg = "Fail to execute database migration: " + className;
  88. Loggers.get(getClass()).error(msg, e);
  89. throw new IllegalStateException(msg, e);
  90. }
  91. }
  92. private MigrationStep getMigration(String className) {
  93. for (MigrationStep migration : migrations) {
  94. if (migration.getClass().getName().equals(className)) {
  95. return migration;
  96. }
  97. }
  98. throw new IllegalArgumentException("Database migration not found: " + className);
  99. }
  100. @VisibleForTesting
  101. protected void createSchema(Connection connection, String dialectId) {
  102. DdlUtils.createSchema(connection, dialectId);
  103. }
  104. }