-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONAR-23978 Create table 'user_ai_tool_usages' and add column 'detect…
…ed_ai_code' to table 'projects'
- Loading branch information
1 parent
3ce1c4c
commit e118109
Showing
6 changed files
with
240 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...org/sonar/server/platform/db/migration/version/v202501/CreateUserAIToolUsagesTableIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* SonarQube | ||
* Copyright (C) 2009-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.server.platform.db.migration.version.v202501; | ||
|
||
import java.sql.SQLException; | ||
import java.sql.Types; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.sonar.db.MigrationDbTester; | ||
import org.sonar.server.platform.db.migration.step.DdlChange; | ||
|
||
import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE; | ||
import static org.sonar.server.platform.db.migration.version.v202501.CreateUserAIToolUsagesTable.COLUMN_ACTIVATED_AT; | ||
import static org.sonar.server.platform.db.migration.version.v202501.CreateUserAIToolUsagesTable.COLUMN_LAST_ACTIVITY_AT; | ||
import static org.sonar.server.platform.db.migration.version.v202501.CreateUserAIToolUsagesTable.COLUMN_USER_UUID; | ||
import static org.sonar.server.platform.db.migration.version.v202501.CreateUserAIToolUsagesTable.COLUMN_UUID; | ||
import static org.sonar.server.platform.db.migration.version.v202501.CreateUserAIToolUsagesTable.USER_AI_TOOLS_USAGES_TABLE_NAME; | ||
|
||
class CreateUserAIToolUsagesTableIT { | ||
|
||
@RegisterExtension | ||
public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(CreateUserAIToolUsagesTable.class); | ||
|
||
private final DdlChange underTest = new CreateUserAIToolUsagesTable(db.database()); | ||
|
||
@Test | ||
void execute_shouldCreateTable() throws SQLException { | ||
db.assertTableDoesNotExist(USER_AI_TOOLS_USAGES_TABLE_NAME); | ||
|
||
underTest.execute(); | ||
|
||
db.assertTableExists(USER_AI_TOOLS_USAGES_TABLE_NAME); | ||
db.assertPrimaryKey(USER_AI_TOOLS_USAGES_TABLE_NAME, "pk_user_ai_tool_usages", COLUMN_UUID); | ||
db.assertColumnDefinition(USER_AI_TOOLS_USAGES_TABLE_NAME, COLUMN_USER_UUID, Types.VARCHAR, UUID_SIZE, false); | ||
db.assertColumnDefinition(USER_AI_TOOLS_USAGES_TABLE_NAME, COLUMN_ACTIVATED_AT, Types.BIGINT, null, false); | ||
db.assertColumnDefinition(USER_AI_TOOLS_USAGES_TABLE_NAME, COLUMN_LAST_ACTIVITY_AT, Types.BIGINT, null, true); | ||
} | ||
|
||
@Test | ||
void execute_shouldBeReentrant() throws SQLException { | ||
db.assertTableDoesNotExist(USER_AI_TOOLS_USAGES_TABLE_NAME); | ||
|
||
underTest.execute(); | ||
underTest.execute(); | ||
|
||
db.assertTableExists(USER_AI_TOOLS_USAGES_TABLE_NAME); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
.../server/platform/db/migration/version/v202501/AddDetectedAICodeColumnToProjectsTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* SonarQube | ||
* Copyright (C) 2009-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.server.platform.db.migration.version.v202501; | ||
|
||
import java.sql.SQLException; | ||
import org.sonar.db.Database; | ||
import org.sonar.server.platform.db.migration.def.BooleanColumnDef; | ||
import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder; | ||
import org.sonar.server.platform.db.migration.step.DdlChange; | ||
|
||
import static org.sonar.db.DatabaseUtils.tableColumnExists; | ||
|
||
public class AddDetectedAICodeColumnToProjectsTable extends DdlChange { | ||
static final String PROJECTS_TABLE_NAME = "projects"; | ||
static final String DETECTED_AI_CODE_COLUMN_NAME = "detected_ai_code"; | ||
|
||
public AddDetectedAICodeColumnToProjectsTable(Database db) { | ||
super(db); | ||
} | ||
|
||
@Override | ||
public void execute(Context context) throws SQLException { | ||
try (var connection = getDatabase().getDataSource().getConnection()) { | ||
if (!tableColumnExists(connection, PROJECTS_TABLE_NAME, DETECTED_AI_CODE_COLUMN_NAME)) { | ||
var columnDef = BooleanColumnDef.newBooleanColumnDefBuilder() | ||
.setColumnName(DETECTED_AI_CODE_COLUMN_NAME) | ||
.setIsNullable(false) | ||
.setDefaultValue(false) | ||
.build(); | ||
context.execute(new AddColumnsBuilder(getDialect(), PROJECTS_TABLE_NAME) | ||
.addColumn(columnDef) | ||
.build()); | ||
} | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...a/org/sonar/server/platform/db/migration/version/v202501/CreateUserAIToolUsagesTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* SonarQube | ||
* Copyright (C) 2009-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.server.platform.db.migration.version.v202501; | ||
|
||
import java.sql.SQLException; | ||
import org.sonar.db.Database; | ||
import org.sonar.server.platform.db.migration.sql.CreateTableBuilder; | ||
import org.sonar.server.platform.db.migration.step.CreateTableChange; | ||
|
||
import static org.sonar.server.platform.db.migration.def.BigIntegerColumnDef.newBigIntegerColumnDefBuilder; | ||
import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE; | ||
import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder; | ||
|
||
public class CreateUserAIToolUsagesTable extends CreateTableChange { | ||
|
||
static final String USER_AI_TOOLS_USAGES_TABLE_NAME = "user_ai_tool_usages"; | ||
static final String COLUMN_UUID = "uuid"; | ||
static final String COLUMN_USER_UUID = "user_uuid"; | ||
static final String COLUMN_ACTIVATED_AT = "activated_at"; | ||
static final String COLUMN_LAST_ACTIVITY_AT = "last_activity_at"; | ||
|
||
|
||
protected CreateUserAIToolUsagesTable(Database db) { | ||
super(db, USER_AI_TOOLS_USAGES_TABLE_NAME); | ||
} | ||
|
||
@Override | ||
public void execute(Context context, String tableName) throws SQLException { | ||
context.execute(new CreateTableBuilder(getDialect(), tableName) | ||
.addPkColumn(newVarcharColumnDefBuilder().setColumnName(COLUMN_UUID).setIsNullable(false).setLimit(UUID_SIZE).build()) | ||
.addColumn(newVarcharColumnDefBuilder().setColumnName(COLUMN_USER_UUID).setIsNullable(false).setLimit(UUID_SIZE).build()) | ||
.addColumn(newBigIntegerColumnDefBuilder().setColumnName(COLUMN_ACTIVATED_AT).setIsNullable(false).build()) | ||
.addColumn(newBigIntegerColumnDefBuilder().setColumnName(COLUMN_LAST_ACTIVITY_AT).setIsNullable(true).build()) | ||
.build()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...ver/platform/db/migration/version/v202501/AddDetectedAICodeColumnToProjectsTableTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* SonarQube | ||
* Copyright (C) 2009-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.server.platform.db.migration.version.v202501; | ||
|
||
import java.sql.SQLException; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.sonar.db.MigrationDbTester; | ||
|
||
import static java.sql.Types.BOOLEAN; | ||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode; | ||
import static org.sonar.server.platform.db.migration.version.v202501.AddDetectedAICodeColumnToProjectsTable.DETECTED_AI_CODE_COLUMN_NAME; | ||
import static org.sonar.server.platform.db.migration.version.v202501.AddDetectedAICodeColumnToProjectsTable.PROJECTS_TABLE_NAME; | ||
|
||
class AddDetectedAICodeColumnToProjectsTableTest { | ||
|
||
@RegisterExtension | ||
public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(AddDetectedAICodeColumnToProjectsTable.class); | ||
|
||
private final AddDetectedAICodeColumnToProjectsTable underTest = new AddDetectedAICodeColumnToProjectsTable(db.database()); | ||
|
||
@Test | ||
void execute_whenColumnDoesNotExist_shouldCreateColumn() throws SQLException { | ||
db.assertColumnDoesNotExist(PROJECTS_TABLE_NAME, DETECTED_AI_CODE_COLUMN_NAME); | ||
underTest.execute(); | ||
assertColumnExists(); | ||
} | ||
|
||
@Test | ||
void execute_whenColumnsAlreadyExists_shouldNotFail() throws SQLException { | ||
underTest.execute(); | ||
assertColumnExists(); | ||
assertThatCode(underTest::execute).doesNotThrowAnyException(); | ||
} | ||
|
||
private void assertColumnExists() { | ||
db.assertColumnDefinition(PROJECTS_TABLE_NAME, DETECTED_AI_CODE_COLUMN_NAME, BOOLEAN, null, false); | ||
} | ||
|
||
} |