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.

CreateDb.java 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program 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. * This program 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.db.createdb;
  21. import com.sonar.orchestrator.config.Configuration;
  22. import com.sonar.orchestrator.db.DatabaseClient;
  23. import com.sonar.orchestrator.db.DatabaseFactory;
  24. import com.sonar.orchestrator.db.DefaultDatabase;
  25. import java.util.function.Consumer;
  26. import org.sonar.api.config.internal.Settings;
  27. import org.sonar.api.config.internal.MapSettings;
  28. import org.sonar.api.utils.log.Logger;
  29. import org.sonar.api.utils.log.Loggers;
  30. import org.sonar.db.SQDatabase;
  31. public class CreateDb {
  32. public static void main(String[] args) {
  33. createDb(configuration -> {
  34. Settings settings = new MapSettings();
  35. configuration.asMap().forEach(settings::setProperty);
  36. logJdbcSettings(settings);
  37. SQDatabase.newDatabase(settings, true).start();
  38. });
  39. }
  40. private static void createDb(Consumer<Configuration> execute) {
  41. Configuration configuration = Configuration.builder()
  42. .addSystemProperties()
  43. .setProperty("orchestrator.keepDatabase", "false")
  44. .build();
  45. DatabaseClient databaseClient = DatabaseFactory.create(configuration, configuration.locators());
  46. DefaultDatabase defaultDatabase = new DefaultDatabase(databaseClient);
  47. defaultDatabase.killOtherConnections();
  48. try {
  49. defaultDatabase.start();
  50. execute.accept(configuration);
  51. } finally {
  52. defaultDatabase.stop();
  53. }
  54. }
  55. private static void logJdbcSettings(Settings settings) {
  56. Logger logger = Loggers.get(CreateDb.class);
  57. for (String key : settings.getKeysStartingWith("sonar.jdbc")) {
  58. logger.info(key + ": " + settings.getString(key));
  59. }
  60. }
  61. }