Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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 com.google.common.annotations.VisibleForTesting;
  22. import org.apache.commons.dbutils.DbUtils;
  23. import org.apache.ibatis.session.SqlSession;
  24. import org.picocontainer.Startable;
  25. import org.sonar.api.server.ServerSide;
  26. import org.sonar.api.platform.ServerUpgradeStatus;
  27. import org.sonar.api.utils.log.Loggers;
  28. import org.sonar.db.DdlUtils;
  29. import org.sonar.server.db.DbClient;
  30. import org.sonar.server.plugins.ServerPluginRepository;
  31. import java.sql.Connection;
  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. @ServerSide
  39. public class DatabaseMigrator implements Startable {
  40. private final DbClient dbClient;
  41. private final MigrationStep[] migrations;
  42. private final ServerUpgradeStatus serverUpgradeStatus;
  43. /**
  44. * ServerPluginRepository is used to ensure H2 schema creation is done only after copy of bundle plugins have been done
  45. */
  46. public DatabaseMigrator(DbClient dbClient, MigrationStep[] migrations, ServerUpgradeStatus serverUpgradeStatus,
  47. ServerPluginRepository unused) {
  48. this.dbClient = dbClient;
  49. this.migrations = migrations;
  50. this.serverUpgradeStatus = serverUpgradeStatus;
  51. }
  52. @Override
  53. public void start() {
  54. createDatabase();
  55. }
  56. @Override
  57. public void stop() {
  58. // Nothing to do
  59. }
  60. /**
  61. * @return true if the database has been created, false if this database is not supported or if database has already been created
  62. */
  63. @VisibleForTesting
  64. boolean createDatabase() {
  65. if (DdlUtils.supportsDialect(dbClient.database().getDialect().getId()) && serverUpgradeStatus.isFreshInstall()) {
  66. Loggers.get(getClass()).info("Create database");
  67. SqlSession session = dbClient.openSession(false);
  68. Connection connection = null;
  69. try {
  70. connection = session.getConnection();
  71. createSchema(connection, dbClient.database().getDialect().getId());
  72. return true;
  73. } finally {
  74. session.close();
  75. // The connection is probably already closed by session.close()
  76. // but it's not documented in mybatis javadoc.
  77. DbUtils.closeQuietly(connection);
  78. }
  79. }
  80. return false;
  81. }
  82. public void executeMigration(String className) {
  83. MigrationStep migration = getMigration(className);
  84. try {
  85. migration.execute();
  86. } catch (Exception e) {
  87. // duplication between log and exception because webapp does not correctly log initial stacktrace
  88. String msg = "Fail to execute database migration: " + className;
  89. Loggers.get(getClass()).error(msg, e);
  90. throw new IllegalStateException(msg, e);
  91. }
  92. }
  93. private MigrationStep getMigration(String className) {
  94. for (MigrationStep migration : migrations) {
  95. if (migration.getClass().getName().equals(className)) {
  96. return migration;
  97. }
  98. }
  99. throw new IllegalArgumentException("Database migration not found: " + className);
  100. }
  101. @VisibleForTesting
  102. protected void createSchema(Connection connection, String dialectId) {
  103. DdlUtils.createSchema(connection, dialectId);
  104. }
  105. }