Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,32 @@
import org.jooq.impl.DSL;
import org.sonarsource.sonarlint.core.commons.log.SonarLintLogger;

import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.ACTIVE_RULESETS;
import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.AI_CODEFIX_SETTINGS;
import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.NEW_CODE_DEFINITIONS;
import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.ORGANIZATIONS;
import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.PLUGINS;
import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.PROJECTS;
import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.PROJECT_BRANCHES;
import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.SERVERS;
import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.SERVER_FEATURES;
import static org.sonarsource.sonarlint.core.commons.storage.model.Tables.USERS;

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

private final JdbcConnectionPool dataSource;
private final DSLContext dsl;
private final boolean isInMemory;

@Inject
public SonarLintDatabase(SonarLintDatabaseInitParams sonarLintDatabaseInitParams) {
JdbcConnectionPool ds;
try {
var mode = System.getProperty("sonarlint.db.mode", "file");
this.isInMemory = "mem".equalsIgnoreCase(mode);
String url;
if ("mem".equalsIgnoreCase(mode)) {
if (isInMemory) {
// In-memory mode for tests: keep DB alive until JVM exits to allow multiple connections
url = "jdbc:h2:mem:sonarlint;DB_CLOSE_DELAY=-1";
} else {
Expand Down Expand Up @@ -98,6 +109,9 @@ public Connection getConnection() throws SQLException {

public void shutdown() {
try {
if (isInMemory) {
dsl.execute("SHUTDOWN");
}
dataSource.dispose();
LOG.debug("H2Database disposed");
} catch (Exception e) {
Expand All @@ -109,6 +123,33 @@ public void cleanupNonExistingConnections(Set<String> existingConnectionIds) {
dsl.deleteFrom(AI_CODEFIX_SETTINGS)
.where(AI_CODEFIX_SETTINGS.CONNECTION_ID.notIn(existingConnectionIds))
.execute();
dsl.deleteFrom(ACTIVE_RULESETS)
.where(ACTIVE_RULESETS.CONNECTION_ID.notIn(existingConnectionIds))
.execute();
dsl.deleteFrom(PROJECTS)
.where(PROJECTS.CONNECTION_ID.notIn(existingConnectionIds))
.execute();
dsl.deleteFrom(SERVERS)
.where(SERVERS.CONNECTION_ID.notIn(existingConnectionIds))
.execute();
dsl.deleteFrom(PROJECT_BRANCHES)
.where(PROJECT_BRANCHES.CONNECTION_ID.notIn(existingConnectionIds))
.execute();
dsl.deleteFrom(PLUGINS)
.where(PLUGINS.CONNECTION_ID.notIn(existingConnectionIds))
.execute();
dsl.deleteFrom(SERVER_FEATURES)
.where(SERVER_FEATURES.CONNECTION_ID.notIn(existingConnectionIds))
.execute();
dsl.deleteFrom(ORGANIZATIONS)
.where(ORGANIZATIONS.CONNECTION_ID.notIn(existingConnectionIds))
.execute();
dsl.deleteFrom(USERS)
.where(USERS.CONNECTION_ID.notIn(existingConnectionIds))
.execute();
dsl.deleteFrom(NEW_CODE_DEFINITIONS)
.where(NEW_CODE_DEFINITIONS.CONNECTION_ID.notIn(existingConnectionIds))
.execute();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
-- Flyway migration: create server storage tables for H2
-- These tables migrate protobuf-based storage to H2 database
-- Used only in dogfood environment

CREATE TABLE IF NOT EXISTS PROJECTS (
connection_id VARCHAR(255) NOT NULL,
project_key VARCHAR(255) NOT NULL,
last_smart_notification_poll_date TIMESTAMP,
settings JSON,
CONSTRAINT pk_projects PRIMARY KEY (connection_id, project_key)
);

CREATE TABLE IF NOT EXISTS ACTIVE_RULESETS (
connection_id VARCHAR(255) NOT NULL,
project_key VARCHAR(255) NOT NULL,
language_key VARCHAR(50) NOT NULL,
last_modified TIMESTAMP NOT NULL,
rules JSON NOT NULL,
CONSTRAINT pk_active_rulesets PRIMARY KEY (connection_id, project_key, language_key)
);

CREATE TABLE IF NOT EXISTS PROJECT_BRANCHES (
connection_id VARCHAR(255) NOT NULL,
project_key VARCHAR(255) NOT NULL,
name VARCHAR(255) NOT NULL,
is_default BOOLEAN NOT NULL,
CONSTRAINT pk_project_branches PRIMARY KEY (connection_id, project_key, name)
);

CREATE TABLE IF NOT EXISTS PLUGINS (
connection_id VARCHAR(255) NOT NULL,
"key" VARCHAR(255) NOT NULL,
hash VARCHAR(255) NOT NULL,
filename VARCHAR(255) NOT NULL,
CONSTRAINT pk_plugins PRIMARY KEY (connection_id, "key")
);

CREATE TABLE IF NOT EXISTS SERVERS (
connection_id VARCHAR(255) NOT NULL PRIMARY KEY,
version VARCHAR(50),
id VARCHAR(255),
global_settings JSON NOT NULL
);

CREATE TABLE IF NOT EXISTS SERVER_FEATURES (
connection_id VARCHAR(255) NOT NULL PRIMARY KEY,
features VARCHAR(200) ARRAY NOT NULL
);

CREATE TABLE IF NOT EXISTS ORGANIZATIONS (
connection_id VARCHAR(255) NOT NULL PRIMARY KEY,
id VARCHAR(255) NOT NULL,
uuidv4 VARCHAR(36) NOT NULL
);

CREATE TABLE IF NOT EXISTS "USERS" (
connection_id VARCHAR(255) NOT NULL PRIMARY KEY,
id VARCHAR(255) NOT NULL
);

CREATE TABLE IF NOT EXISTS NEW_CODE_DEFINITIONS (
connection_id VARCHAR(255) NOT NULL,
project_key VARCHAR(255) NOT NULL,
mode VARCHAR(50) NOT NULL,
days INT,
threshold_date DATE,
version VARCHAR(50),
reference_branch VARCHAR(255),
CONSTRAINT pk_new_code_definitions PRIMARY KEY (connection_id, project_key)
);

This file was deleted.

Loading
Loading