Skip to content

Commit af3117f

Browse files
SLCORE-1802 Migrate protobuf-based storage to the new database
1 parent 3847e4a commit af3117f

File tree

109 files changed

+4440
-1166
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+4440
-1166
lines changed

backend/commons/src/main/java/org/sonarsource/sonarlint/core/commons/storage/SonarLintDatabase.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,32 @@
3232
import org.jooq.impl.DSL;
3333
import org.sonarsource.sonarlint.core.commons.log.SonarLintLogger;
3434

35+
import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.ACTIVE_RULESETS;
3536
import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.AI_CODEFIX_SETTINGS;
37+
import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.NEW_CODE_DEFINITIONS;
38+
import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.ORGANIZATIONS;
39+
import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.PLUGINS;
40+
import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.PROJECTS;
41+
import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.PROJECT_BRANCHES;
42+
import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.SERVERS;
43+
import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.SERVER_FEATURES;
44+
import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.USERS;
3645

3746
public final class SonarLintDatabase {
3847
private static final SonarLintLogger LOG = SonarLintLogger.get();
3948

4049
private final JdbcConnectionPool dataSource;
4150
private final DSLContext dsl;
51+
private final boolean isInMemory;
4252

4353
@Inject
4454
public SonarLintDatabase(SonarLintDatabaseInitParams sonarLintDatabaseInitParams) {
4555
JdbcConnectionPool ds;
4656
try {
4757
var mode = System.getProperty("sonarlint.db.mode", "file");
58+
this.isInMemory = "mem".equalsIgnoreCase(mode);
4859
String url;
49-
if ("mem".equalsIgnoreCase(mode)) {
60+
if (isInMemory) {
5061
// In-memory mode for tests: keep DB alive until JVM exits to allow multiple connections
5162
url = "jdbc:h2:mem:sonarlint;DB_CLOSE_DELAY=-1";
5263
} else {
@@ -98,6 +109,9 @@ public Connection getConnection() throws SQLException {
98109

99110
public void shutdown() {
100111
try {
112+
if (isInMemory) {
113+
dsl.execute("SHUTDOWN");
114+
}
101115
dataSource.dispose();
102116
LOG.debug("H2Database disposed");
103117
} catch (Exception e) {
@@ -109,6 +123,33 @@ public void cleanupNonExistingConnections(Set<String> existingConnectionIds) {
109123
dsl.deleteFrom(AI_CODEFIX_SETTINGS)
110124
.where(AI_CODEFIX_SETTINGS.CONNECTION_ID.notIn(existingConnectionIds))
111125
.execute();
126+
dsl.deleteFrom(ACTIVE_RULESETS)
127+
.where(ACTIVE_RULESETS.CONNECTION_ID.notIn(existingConnectionIds))
128+
.execute();
129+
dsl.deleteFrom(PROJECTS)
130+
.where(PROJECTS.CONNECTION_ID.notIn(existingConnectionIds))
131+
.execute();
132+
dsl.deleteFrom(SERVERS)
133+
.where(SERVERS.CONNECTION_ID.notIn(existingConnectionIds))
134+
.execute();
135+
dsl.deleteFrom(PROJECT_BRANCHES)
136+
.where(PROJECT_BRANCHES.CONNECTION_ID.notIn(existingConnectionIds))
137+
.execute();
138+
dsl.deleteFrom(PLUGINS)
139+
.where(PLUGINS.CONNECTION_ID.notIn(existingConnectionIds))
140+
.execute();
141+
dsl.deleteFrom(SERVER_FEATURES)
142+
.where(SERVER_FEATURES.CONNECTION_ID.notIn(existingConnectionIds))
143+
.execute();
144+
dsl.deleteFrom(ORGANIZATIONS)
145+
.where(ORGANIZATIONS.CONNECTION_ID.notIn(existingConnectionIds))
146+
.execute();
147+
dsl.deleteFrom(USERS)
148+
.where(USERS.CONNECTION_ID.notIn(existingConnectionIds))
149+
.execute();
150+
dsl.deleteFrom(NEW_CODE_DEFINITIONS)
151+
.where(NEW_CODE_DEFINITIONS.CONNECTION_ID.notIn(existingConnectionIds))
152+
.execute();
112153
}
113154
}
114155

backend/commons/src/main/java/org/sonarsource/sonarlint/core/commons/storage/repository/AiCodeFixRepository.java

Lines changed: 0 additions & 89 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
-- Flyway migration: create server storage tables for H2
2+
-- These tables migrate protobuf-based storage to H2 database
3+
-- Used only in dogfood environment
4+
5+
CREATE TABLE IF NOT EXISTS PROJECTS (
6+
connection_id VARCHAR(255) NOT NULL,
7+
project_key VARCHAR(255) NOT NULL,
8+
last_smart_notification_poll_date TIMESTAMP,
9+
settings JSON,
10+
CONSTRAINT pk_projects PRIMARY KEY (connection_id, project_key)
11+
);
12+
13+
CREATE TABLE IF NOT EXISTS ACTIVE_RULESETS (
14+
connection_id VARCHAR(255) NOT NULL,
15+
project_key VARCHAR(255) NOT NULL,
16+
language_key VARCHAR(50) NOT NULL,
17+
last_modified TIMESTAMP NOT NULL,
18+
rules JSON NOT NULL,
19+
CONSTRAINT pk_active_rulesets PRIMARY KEY (connection_id, project_key, language_key)
20+
);
21+
22+
CREATE TABLE IF NOT EXISTS PROJECT_BRANCHES (
23+
connection_id VARCHAR(255) NOT NULL,
24+
project_key VARCHAR(255) NOT NULL,
25+
name VARCHAR(255) NOT NULL,
26+
is_default BOOLEAN NOT NULL,
27+
CONSTRAINT pk_project_branches PRIMARY KEY (connection_id, project_key, name)
28+
);
29+
30+
CREATE TABLE IF NOT EXISTS PLUGINS (
31+
connection_id VARCHAR(255) NOT NULL,
32+
"key" VARCHAR(255) NOT NULL,
33+
hash VARCHAR(255) NOT NULL,
34+
filename VARCHAR(255) NOT NULL,
35+
CONSTRAINT pk_plugins PRIMARY KEY (connection_id, "key")
36+
);
37+
38+
CREATE TABLE IF NOT EXISTS SERVERS (
39+
connection_id VARCHAR(255) NOT NULL PRIMARY KEY,
40+
version VARCHAR(50),
41+
id VARCHAR(255),
42+
global_settings JSON NOT NULL
43+
);
44+
45+
CREATE TABLE IF NOT EXISTS SERVER_FEATURES (
46+
connection_id VARCHAR(255) NOT NULL PRIMARY KEY,
47+
features VARCHAR(200) ARRAY NOT NULL
48+
);
49+
50+
CREATE TABLE IF NOT EXISTS ORGANIZATIONS (
51+
connection_id VARCHAR(255) NOT NULL PRIMARY KEY,
52+
id VARCHAR(255) NOT NULL,
53+
uuidv4 VARCHAR(36) NOT NULL
54+
);
55+
56+
CREATE TABLE IF NOT EXISTS "USERS" (
57+
connection_id VARCHAR(255) NOT NULL PRIMARY KEY,
58+
id VARCHAR(255) NOT NULL
59+
);
60+
61+
CREATE TABLE IF NOT EXISTS NEW_CODE_DEFINITIONS (
62+
connection_id VARCHAR(255) NOT NULL,
63+
project_key VARCHAR(255) NOT NULL,
64+
mode VARCHAR(50) NOT NULL,
65+
days INT,
66+
threshold_date DATE,
67+
version VARCHAR(50),
68+
reference_branch VARCHAR(255),
69+
CONSTRAINT pk_new_code_definitions PRIMARY KEY (connection_id, project_key)
70+
);
71+

backend/commons/src/test/java/org/sonarsource/sonarlint/core/commons/storage/repository/AiCodeFixRepositoryTest.java

Lines changed: 0 additions & 85 deletions
This file was deleted.

0 commit comments

Comments
 (0)