Upgrade Sonarqube 7.9.2 to 8.1

I successful updated sonar from 7.6 to 7.9.2 for update to 8.1 version.
But with update from 7.9.2 to 8.1 faced issue:

2020.02.17 14:36:52 ERROR web[][o.s.s.p.d.m.DatabaseMigrationImpl] DB migration ended with an exception
org.sonar.server.platform.db.migration.step.MigrationStepExecutionException: Execution of migration step #3003 'Populate ProjectQualityGate table from Properties table' failed
        at org.sonar.server.platform.db.migration.step.MigrationStepsExecutorImpl.execute(MigrationStepsExecutorImpl.java:79)
        at org.sonar.server.platform.db.migration.step.MigrationStepsExecutorImpl.execute(MigrationStepsExecutorImpl.java:67)
        at com.google.common.collect.ImmutableList.forEach(ImmutableList.java:405)
        at org.sonar.server.platform.db.migration.step.MigrationStepsExecutorImpl.execute(MigrationStepsExecutorImpl.java:52)
        at org.sonar.server.platform.db.migration.engine.MigrationEngineImpl.execute(MigrationEngineImpl.java:68)
        at org.sonar.server.platform.db.migration.DatabaseMigrationImpl.doUpgradeDb(DatabaseMigrationImpl.java:105)
        at org.sonar.server.platform.db.migration.DatabaseMigrationImpl.doDatabaseMigration(DatabaseMigrationImpl.java:80)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.sql.BatchUpdateException: Batch entry 156 insert into project_qgates(project_uuid, quality_gate_uuid) VALUES ('AW_9IgHTAcZODALNP-5F', 'AXALxbfLAcZODALNP_tw') was aborted: ERROR: duplicate key value violates unique constraint "pk_project_qgates"
  Detail: Key (project_uuid)=(AW_9IgHTAcZODALNP-5F) already exists.  Call getNextException to see other errors in the batch.
        at org.postgresql.jdbc.BatchResultHandler.handleError(BatchResultHandler.java:148)
        at org.postgresql.core.ResultHandlerDelegate.handleError(ResultHandlerDelegate.java:50)
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2234)
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:510)
        at org.postgresql.jdbc.PgStatement.executeBatch(PgStatement.java:853)
        at org.postgresql.jdbc.PgPreparedStatement.executeBatch(PgPreparedStatement.java:1546)
        at org.apache.commons.dbcp2.DelegatingStatement.executeBatch(DelegatingStatement.java:242)
        at org.apache.commons.dbcp2.DelegatingStatement.executeBatch(DelegatingStatement.java:242)
        at org.sonar.server.platform.db.migration.step.UpsertImpl.execute(UpsertImpl.java:68)
        at org.sonar.server.platform.db.migration.version.v80.PopulateProjectQualityGatesTable.populateProjectQualityGates(PopulateProjectQualityGatesTable.java:62)
        at org.sonar.server.platform.db.migration.version.v80.PopulateProjectQualityGatesTable.execute(PopulateProjectQualityGatesTable.java:49)
        at org.sonar.server.platform.db.migration.step.DataChange.execute(DataChange.java:44)
        at org.sonar.server.platform.db.migration.step.MigrationStepsExecutorImpl.execute(MigrationStepsExecutorImpl.java:75)
        ... 9 common frames omitted
Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "pk_project_qgates"
  Detail: Key (project_uuid)=(AW_9IgHTAcZODALNP-5F) already exists.
        at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2497)
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2233)
        ... 19 common frames omitted

I tried to clean up projects in error project_uuid , but it show other one.
Could you please help to understand what “Populate ProjectQualityGate table from Properties table” do and why I got such issue…

Hello @yury_levin!

Welcome to the community!

The migration you are referring to is moving projects quality gate properties (sonar.qualitygate) to another table which is project_quality_gate.

In your case it seems like there is more than one row with property sonar.qualitygate for the project you have referenced.

To have a better view on what is the issue, could you execute this query and share results?
select p.resource_id as project_uuid, count(*) from properties p where p.prop_key = 'sonar.qualitygate' group by project_uuid having count(*) > 1

This query will help to find all projects which have more than one quality gate assigned to it.

Hello;

I’m facing the same problem.

I executed the request and i have this result
Project_uuid nb
1030 2

FYI I updated the SQL request

Hello Ben,

First of all welcome in the community.

In your case it seems that there are two QualityGates assigned to one project which causes migration to fail as we expect only one to exist.

To investigate further let’s try to execute this query in order to identify which QG is the latest one used by your project:

select qg.* as qgate_uuid 
from quality_gates qg
where qg.id in (
select CAST(p.text_value as int) as qgate_id 
from properties p 
where p.resource_id = 1030 and p.prop_key = 'sonar.qualitygate')

Hi Jacek;

done, result is “AW1EAjSdm3prid6H2fbg”

by this query
select qg.uuid as qgate_uuid
from quality_gates qg
where qg.id in (
select CAST(p.text_value as int) as qgate_id
from properties p
where p.resource_id = 1030 and p.prop_key = ‘sonar.qualitygate’)

for full data line I have

id name created_at updated_at is_built_in uuid
2 Sonar_LV 2019-09-18 12:54:18.270 NULL 0 AW1EAjSdm3prid6H2fbg

Problem resolved, by deteting the first propery from properties table.