Skip to content

Commit 476d3d1

Browse files
dbmenesessonartech
authored andcommitted
SONAR-26001 DB migrations to create table(s)
1 parent cfde07c commit 476d3d1

2 files changed

Lines changed: 128 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* SonarQube
3+
* Copyright (C) 2009-2025 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.server.platform.db.migration.version.v202506;
21+
22+
import java.sql.SQLException;
23+
import org.sonar.db.Database;
24+
import org.sonar.server.platform.db.migration.sql.CreateTableBuilder;
25+
import org.sonar.server.platform.db.migration.step.CreateTableChange;
26+
27+
import static org.sonar.server.platform.db.migration.def.IntegerColumnDef.newIntegerColumnDefBuilder;
28+
import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;
29+
30+
public class CreateIssueStatsByRuleKeyTable extends CreateTableChange {
31+
static final String TABLE_NAME = "issue_stats_by_rule_key";
32+
static final String COLUMN_AGGREGATION_TYPE = "aggregation_type";
33+
static final String COLUMN_AGGREGATION_ID = "aggregation_id";
34+
static final String COLUMN_RULE_KEY = "rule_key";
35+
static final String COLUMN_ISSUE_COUNT = "issue_count";
36+
static final String COLUMN_RATING = "rating";
37+
static final String COLUMN_HOTSPOT_COUNT = "hotspot_count";
38+
static final String COLUMN_HOTSPOT_RATING = "hotspot_rating";
39+
40+
public CreateIssueStatsByRuleKeyTable(Database db) {
41+
super(db, TABLE_NAME);
42+
}
43+
44+
@Override
45+
public void execute(Context context, String tableName) throws SQLException {
46+
var dialect = getDialect();
47+
48+
context.execute(new CreateTableBuilder(dialect, tableName)
49+
.addPkColumn(newVarcharColumnDefBuilder().setColumnName(COLUMN_AGGREGATION_TYPE).setIsNullable(false).setLimit(20).build())
50+
.addPkColumn(newVarcharColumnDefBuilder().setColumnName(COLUMN_AGGREGATION_ID).setIsNullable(false).setLimit(40).build())
51+
.addPkColumn(newVarcharColumnDefBuilder().setColumnName(COLUMN_RULE_KEY).setIsNullable(false).setLimit(100).build())
52+
.addColumn(newIntegerColumnDefBuilder().setColumnName(COLUMN_ISSUE_COUNT).setIsNullable(false).build())
53+
.addColumn(newIntegerColumnDefBuilder().setColumnName(COLUMN_RATING).setIsNullable(false).build())
54+
.addColumn(newIntegerColumnDefBuilder().setColumnName(COLUMN_HOTSPOT_COUNT).setIsNullable(true).build())
55+
.addColumn(newIntegerColumnDefBuilder().setColumnName(COLUMN_HOTSPOT_RATING).setIsNullable(true).build())
56+
.build());
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* SonarQube
3+
* Copyright (C) 2009-2025 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.server.platform.db.migration.version.v202506;
21+
22+
import java.sql.SQLException;
23+
import java.sql.Types;
24+
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.extension.RegisterExtension;
26+
import org.sonar.db.MigrationDbTester;
27+
28+
import static org.sonar.server.platform.db.migration.version.v202506.CreateIssueStatsByRuleKeyTable.COLUMN_AGGREGATION_ID;
29+
import static org.sonar.server.platform.db.migration.version.v202506.CreateIssueStatsByRuleKeyTable.COLUMN_AGGREGATION_TYPE;
30+
import static org.sonar.server.platform.db.migration.version.v202506.CreateIssueStatsByRuleKeyTable.COLUMN_HOTSPOT_COUNT;
31+
import static org.sonar.server.platform.db.migration.version.v202506.CreateIssueStatsByRuleKeyTable.COLUMN_HOTSPOT_RATING;
32+
import static org.sonar.server.platform.db.migration.version.v202506.CreateIssueStatsByRuleKeyTable.COLUMN_ISSUE_COUNT;
33+
import static org.sonar.server.platform.db.migration.version.v202506.CreateIssueStatsByRuleKeyTable.COLUMN_RATING;
34+
import static org.sonar.server.platform.db.migration.version.v202506.CreateIssueStatsByRuleKeyTable.COLUMN_RULE_KEY;
35+
import static org.sonar.server.platform.db.migration.version.v202506.CreateIssueStatsByRuleKeyTable.TABLE_NAME;
36+
37+
class CreateIssueStatsByRuleKeyTableTest {
38+
39+
@RegisterExtension
40+
public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(CreateJiraWorkItemsTable.class);
41+
42+
private final CreateIssueStatsByRuleKeyTable underTest = new CreateIssueStatsByRuleKeyTable(db.database());
43+
44+
@Test
45+
void migration_should_create_table() throws SQLException {
46+
db.assertTableDoesNotExist(TABLE_NAME);
47+
48+
underTest.execute();
49+
50+
db.assertTableExists(TABLE_NAME);
51+
db.assertPrimaryKey(TABLE_NAME, "pk_issue_stats_by_rule_key", COLUMN_AGGREGATION_TYPE, COLUMN_AGGREGATION_ID, COLUMN_RULE_KEY);
52+
db.assertColumnDefinition(TABLE_NAME, COLUMN_AGGREGATION_TYPE, Types.VARCHAR, 20, false);
53+
db.assertColumnDefinition(TABLE_NAME, COLUMN_AGGREGATION_ID, Types.VARCHAR, 40, false);
54+
db.assertColumnDefinition(TABLE_NAME, COLUMN_RULE_KEY, Types.VARCHAR, 100, false);
55+
db.assertColumnDefinition(TABLE_NAME, COLUMN_ISSUE_COUNT, Types.INTEGER, null, false);
56+
db.assertColumnDefinition(TABLE_NAME, COLUMN_RATING, Types.INTEGER, null, false);
57+
db.assertColumnDefinition(TABLE_NAME, COLUMN_HOTSPOT_COUNT, Types.INTEGER, null, true);
58+
db.assertColumnDefinition(TABLE_NAME, COLUMN_HOTSPOT_RATING, Types.INTEGER, null, true);
59+
}
60+
61+
@Test
62+
void migration_should_be_reentrant() throws SQLException {
63+
db.assertTableDoesNotExist(TABLE_NAME);
64+
65+
underTest.execute();
66+
underTest.execute();
67+
68+
db.assertTableExists(TABLE_NAME);
69+
}
70+
}

0 commit comments

Comments
 (0)