import java.util.List;
import org.sonar.db.dialect.Dialect;
+import org.sonar.db.dialect.H2;
+import org.sonar.db.dialect.MsSql;
+import org.sonar.db.dialect.MySql;
import org.sonar.db.dialect.Oracle;
+import org.sonar.db.dialect.PostgreSql;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
}
public List<String> build() {
- if (Oracle.ID.equals(dialect.getId())) {
- return dropOracleSequenceAndTriggerAndTableStatements();
+ switch (dialect.getId()) {
+ case Oracle.ID:
+ return forOracle(tableName);
+ case H2.ID:
+ case MySql.ID:
+ case PostgreSql.ID:
+ return singletonList("drop table if exists " + tableName);
+ case MsSql.ID:
+ // "if exists" is supported only since MSSQL 2016.
+ return singletonList("drop table " + tableName);
+ default:
+ throw new IllegalStateException("Unsupported DB: " + dialect.getId());
}
- return singletonList(dropTableStatement());
}
- private String dropTableStatement() {
- return "DROP TABLE " + tableName;
+ private static List<String> forOracle(String tableName) {
+ return asList(
+ dropIfExistsOnOracle("DROP SEQUENCE " + tableName + "_seq", -2289),
+ dropIfExistsOnOracle("DROP TRIGGER " + tableName + "_idt", -4080),
+ dropIfExistsOnOracle("DROP TABLE " + tableName, -942));
}
- private List<String> dropOracleSequenceAndTriggerAndTableStatements() {
- return asList(dropSequenceFor(tableName), dropTriggerFor(tableName), dropTableStatement());
- }
-
- private static String dropSequenceFor(String tableName) {
+ private static String dropIfExistsOnOracle(String command, int codeIfNotExists) {
return "BEGIN\n" +
- " EXECUTE IMMEDIATE 'DROP SEQUENCE " + tableName + "_seq';\n" +
+ "EXECUTE IMMEDIATE '" + command + "';\n" +
"EXCEPTION\n" +
- " WHEN OTHERS THEN\n" +
- " IF SQLCODE != -2289 THEN\n" +
- " RAISE;\n" +
- " END IF;\n" +
+ "WHEN OTHERS THEN\n" +
+ " IF SQLCODE != " + codeIfNotExists + " THEN\n" +
+ " RAISE;\n" +
+ " END IF;\n" +
"END;";
}
-
- private static String dropTriggerFor(String tableName) {
- return "BEGIN\n" +
- " EXECUTE IMMEDIATE 'DROP TRIGGER " + tableName + "_idt';\n" +
- "EXCEPTION\n" +
- " WHEN OTHERS THEN\n" +
- " IF SQLCODE != -4080 THEN\n" +
- " RAISE;\n" +
- " END IF;\n" +
- "END;";
-
- }
-
}
*/
package org.sonar.server.platform.db.migration.version.v62;
-import com.google.common.collect.ImmutableList;
-import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
-import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.db.Database;
-import org.sonar.db.DatabaseUtils;
import org.sonar.server.platform.db.migration.sql.DropTableBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;
+import static java.util.Arrays.asList;
import static org.sonar.core.util.stream.MoreCollectors.toList;
public class DropIssueFiltersTables extends DdlChange {
- private static final Logger LOGGER = Loggers.get(DropIssueFiltersTables.class);
-
- private static final List<String> TABLES_TO_DROP = ImmutableList.of("issue_filters", "issue_filter_favourites");
-
- private final Database db;
-
public DropIssueFiltersTables(Database db) {
super(db);
- this.db = db;
}
@Override
public void execute(Context context) throws SQLException {
- List<String> tablesToDrop = getEffectiveTablesToDrop();
- LOGGER.info("Removing tables {}", tablesToDrop);
- context.execute(tablesToDrop
+ List<String> tables = asList("issue_filters", "issue_filter_favourites");
+ Loggers.get(getClass()).info("Removing tables {}", tables);
+ context.execute(tables
.stream()
- .flatMap(table -> new DropTableBuilder(db.getDialect(), table).build().stream())
+ .flatMap(table -> new DropTableBuilder(getDialect(), table).build().stream())
.collect(toList()));
}
-
- private List<String> getEffectiveTablesToDrop() throws SQLException {
- try (Connection connection = db.getDataSource().getConnection()) {
- return TABLES_TO_DROP.stream()
- .filter(table -> DatabaseUtils.tableExists(table, connection))
- .collect(toList());
- }
- }
}
*/
package org.sonar.server.platform.db.migration.version.v62;
-import com.google.common.collect.ImmutableList;
-import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
-import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.db.Database;
-import org.sonar.db.DatabaseUtils;
import org.sonar.server.platform.db.migration.sql.DropTableBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;
+import static java.util.Arrays.asList;
import static org.sonar.core.util.stream.MoreCollectors.toList;
public class DropMeasureFiltersTables extends DdlChange {
- private static final Logger LOGGER = Loggers.get(DropMeasureFiltersTables.class);
-
- private static final List<String> TABLES_TO_DROP = ImmutableList.of("measure_filters", "measure_filter_favourites");
-
- private final Database db;
-
public DropMeasureFiltersTables(Database db) {
super(db);
- this.db = db;
}
@Override
public void execute(Context context) throws SQLException {
- List<String> tablesToDrop = getEffectiveTablesToDrop();
- LOGGER.info("Removing tables {}", tablesToDrop);
- context.execute(tablesToDrop
+ List<String> tables = asList("measure_filters", "measure_filter_favourites");
+ Loggers.get(getClass()).info("Removing tables {}", tables);
+ context.execute(tables
.stream()
- .flatMap(table -> new DropTableBuilder(db.getDialect(), table).build().stream())
+ .flatMap(table -> new DropTableBuilder(getDialect(), table).build().stream())
.collect(toList()));
}
-
- private List<String> getEffectiveTablesToDrop() throws SQLException {
- try (Connection connection = db.getDataSource().getConnection()) {
- return TABLES_TO_DROP.stream()
- .filter(table -> DatabaseUtils.tableExists(table, connection))
- .collect(toList());
- }
- }
}
*/
package org.sonar.server.platform.db.migration.version.v62;
-import com.google.common.collect.ImmutableList;
-import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
-import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.db.Database;
-import org.sonar.db.DatabaseUtils;
import org.sonar.server.platform.db.migration.sql.DropTableBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;
+import static java.util.Arrays.asList;
import static org.sonar.core.util.stream.MoreCollectors.toList;
public class DropRelatedDashboardTables extends DdlChange {
- private static final Logger LOGGER = Loggers.get(DropRelatedDashboardTables.class);
-
- private static final List<String> TABLES_TO_DROP = ImmutableList.of("widget_properties", "widgets", "active_dashboards", "dashboards");
-
- private final Database db;
-
public DropRelatedDashboardTables(Database db) {
super(db);
- this.db = db;
}
@Override
public void execute(Context context) throws SQLException {
- List<String> tablesToDrop = getEffectiveTablesToDrop();
- LOGGER.info("Removing tables {}", tablesToDrop);
+ List<String> tablesToDrop = asList("widget_properties", "widgets", "active_dashboards", "dashboards");
+ Loggers.get(getClass()).info("Removing tables {}", tablesToDrop);
context.execute(tablesToDrop
.stream()
- .flatMap(table -> new DropTableBuilder(db.getDialect(), table).build().stream())
+ .flatMap(table -> new DropTableBuilder(getDialect(), table).build().stream())
.collect(toList()));
}
- private List<String> getEffectiveTablesToDrop() throws SQLException {
- try (Connection connection = db.getDataSource().getConnection()) {
- return TABLES_TO_DROP.stream()
- .filter(table -> DatabaseUtils.tableExists(table, connection))
- .collect(toList());
- }
- }
}
*/
package org.sonar.server.platform.db.migration.version.v70;
-import java.sql.Connection;
import java.sql.SQLException;
import org.sonar.db.Database;
-import org.sonar.db.DatabaseUtils;
import org.sonar.server.platform.db.migration.sql.DropTableBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;
public class DropLoadedTemplatesTable extends DdlChange {
- private static final String LOADED_TEMPLATES = "loaded_templates";
- private final Database db;
-
public DropLoadedTemplatesTable(Database db) {
super(db);
- this.db = db;
}
@Override
public void execute(Context context) throws SQLException {
- if (tableExists()) {
- context.execute(new DropTableBuilder(db.getDialect(), LOADED_TEMPLATES).build());
- }
- }
-
- private boolean tableExists() throws SQLException {
- try (Connection connection = db.getDataSource().getConnection()) {
- return DatabaseUtils.tableExists(LOADED_TEMPLATES, connection);
- }
+ context.execute(new DropTableBuilder(getDialect(), "loaded_templates").build());
}
}
public class DropTableProjectLinks extends DdlChange {
- private static final String TABLE_NAME = "project_links";
-
public DropTableProjectLinks(Database db) {
super(db);
}
@Override
public void execute(Context context) throws SQLException {
- context.execute(new DropTableBuilder(getDialect(), TABLE_NAME).build());
+ context.execute(new DropTableBuilder(getDialect(), "project_links").build());
}
}
@Test
public void drop_tables_on_mysql() {
assertThat(new DropTableBuilder(new MySql(), "issues")
- .build()).containsOnly("DROP TABLE issues");
+ .build()).containsOnly("drop table if exists issues");
}
@Test
public void drop_tables_on_postgresql() {
assertThat(new DropTableBuilder(new PostgreSql(), "issues")
- .build()).containsOnly("DROP TABLE issues");
+ .build()).containsOnly("drop table if exists issues");
}
@Test
public void drop_tables_on_mssql() {
assertThat(new DropTableBuilder(new MsSql(), "issues")
- .build()).containsOnly("DROP TABLE issues");
+ .build()).containsOnly("drop table issues");
}
@Test
public void drop_tables_on_h2() {
assertThat(new DropTableBuilder(new H2(), "issues")
- .build()).containsOnly("DROP TABLE issues");
+ .build()).containsOnly("drop table if exists issues");
}
@Test
assertThat(new DropTableBuilder(new Oracle(), "issues")
.build()).containsExactly(
"BEGIN\n" +
- " EXECUTE IMMEDIATE 'DROP SEQUENCE issues_seq';\n" +
+ "EXECUTE IMMEDIATE 'DROP SEQUENCE issues_seq';\n" +
"EXCEPTION\n" +
- " WHEN OTHERS THEN\n" +
- " IF SQLCODE != -2289 THEN\n" +
- " RAISE;\n" +
- " END IF;\n" +
+ "WHEN OTHERS THEN\n" +
+ " IF SQLCODE != -2289 THEN\n" +
+ " RAISE;\n" +
+ " END IF;\n" +
"END;",
"BEGIN\n" +
- " EXECUTE IMMEDIATE 'DROP TRIGGER issues_idt';\n" +
+ "EXECUTE IMMEDIATE 'DROP TRIGGER issues_idt';\n" +
"EXCEPTION\n" +
- " WHEN OTHERS THEN\n" +
- " IF SQLCODE != -4080 THEN\n" +
- " RAISE;\n" +
- " END IF;\n" +
+ "WHEN OTHERS THEN\n" +
+ " IF SQLCODE != -4080 THEN\n" +
+ " RAISE;\n" +
+ " END IF;\n" +
"END;",
- "DROP TABLE issues");
+ "BEGIN\n" +
+ "EXECUTE IMMEDIATE 'DROP TABLE issues';\n" +
+ "EXCEPTION\n" +
+ "WHEN OTHERS THEN\n" +
+ " IF SQLCODE != -942 THEN\n" +
+ " RAISE;\n" +
+ " END IF;\n" +
+ "END;");
}
@Test
import java.sql.SQLException;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.api.utils.System2;
import org.sonar.db.CoreDbTester;
public class DropTableResourceIndexTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
@Rule
public CoreDbTester db = CoreDbTester.createForSchema(DropTableResourceIndexTest.class, "schema.sql");
}
@Test
- public void migration_is_not_re_entrant() throws Exception {
+ public void migration_is_reentrant() throws Exception {
underTest.execute();
-
- expectedException.expect(IllegalStateException.class);
underTest.execute();
+
+ db.assertTableDoesNotExist("resource_index");
}
}
import java.sql.SQLException;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.db.CoreDbTester;
public class DropTableAuthorsTest {
@Rule
public CoreDbTester db = CoreDbTester.createForSchema(DropTableAuthorsTest.class, "authors.sql");
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
private DropTableAuthors underTest = new DropTableAuthors(db.database());
}
@Test
- public void execute_is_not_reentrant() throws Exception {
+ public void execute_is_reentrant() throws Exception {
underTest.execute();
-
- expectedException.expect(IllegalStateException.class);
-
underTest.execute();
+
+ db.assertTableDoesNotExist("authors");
}
}
import java.sql.SQLException;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.db.CoreDbTester;
public class DropTableProjectLinksTest {
@Rule
public final CoreDbTester dbTester = CoreDbTester.createForSchema(DropTableProjectLinksTest.class, "project_links.sql");
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
private DropTableProjectLinks underTest = new DropTableProjectLinks(dbTester.database());
}
@Test
- public void migration_is_not_reentrant() throws SQLException {
+ public void migration_is_reentrant() throws SQLException {
underTest.execute();
-
- expectedException.expect(IllegalStateException.class);
-
underTest.execute();
+
+ dbTester.assertTableDoesNotExist("project_links");
}
}