5
votes

i recently tried to upgrade from SonarQube 5.6.1 LTS to 6.0.

I used the Upgrade Guide but i have got an database migration error.

Fail to execute database migration: org.sonar.db.version.v60.CleanUsurperRootComponents com.microsoft.sqlserver.jdbc.SQLServerException: Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CS_AS" and "Latin1_General_CS_AS" in the equal to operation.

I'm usings SQL 2014 with SQL_Latin1_General_CP1_CS_AS.

I used for the setup the following guide:

SonarQube Setup Gruide for .Net Users v.1.3

There are 157 columns with the wrong collation "Latin1_General_CS_AS" in the Sonar database.

Regards

Daniel

1
Same problem here. Look at this SO question: "the only real difference is the sort order for some characters"Jeroen Heier

1 Answers

7
votes

Option 1: Start with a new and empty database.

Option 2: Restore the SonarQube database and change the database collation using SQL Management Studio (assuming that the database is called SonarQube) before starting the upgrade again:

 -- Show current collation
 USE [master]
 GO

 SELECT [collation_name]
 FROM   [sys].[databases]
 WHERE  name = 'SonarQube'
 GO

The result should be Latin1_General_CS_AS. If the query returns SQL_Latin1_General_CP1_CS_AS change the database collation before upgrading to 6.0:

USE [master]
GO

ALTER DATABASE [SonarQube] SET SINGLE_USER WITH ROLLBACK IMMEDIATE    
ALTER DATABASE [SonarQube] COLLATE Latin1_General_CS_AS;  
ALTER DATABASE [SonarQube] SET MULTI_USER
GO

Option 3: (last resort) Change the database collation (see option 2) and change the database manually (using SQL Management Studio). First execute the following query

USE [SonarQube]
GO

SELECT  '[' + SCHEMA_NAME(t.[schema_id]) + '].[' + t.[name] + '] -> ' + c.[name]
,       'ALTER TABLE [' + SCHEMA_NAME(t.[schema_id]) + '].[' + t.[name] + ']
          ALTER COLUMN [' + c.[name] + '] ' + UPPER(tt.name) + 
         CASE WHEN t.name NOT IN ('ntext', 'text') 
             THEN '(' + 
                 CASE 
                     WHEN tt.name IN ('nchar', 'nvarchar') AND c.max_length != -1 
                         THEN CAST(c.max_length / 2 AS VARCHAR(10))
                     WHEN tt.name IN ('char', 'varchar') AND c.max_length != -1 
                         THEN CAST(c.max_length AS VARCHAR(10))
                     WHEN tt.name IN ('nchar', 'nvarchar', 'char', 'varchar') AND c.max_length = -1 
                         THEN 'MAX'
                     ELSE CAST(c.max_length AS VARCHAR(10)) 
                 END + ')' 
             ELSE '' 
         END + ' COLLATE Latin1_General_CS_AS' + 
         CASE WHEN c.[is_nullable] = 1 
             THEN ' NULL'
             ELSE ' NOT NULL'
         END
 FROM  [sys].[columns] c
 JOIN  [sys].[tables]  t ON c.[object_id] = t.[object_id]
 JOIN  [sys].[types]   tt ON c.[system_type_id] = tt.[system_type_id] AND c.[user_type_id] = tt.[user_type_id]
 WHERE c.[collation_name] IS NOT NULL
 AND   c.[collation_name] != 'Latin1_General_CS_AS'
 AND   t.[type] = 'U'
 GO

This will return several lines; for example:

ALTER TABLE [dbo].[resource_index] 
ALTER COLUMN [root_component_uuid] NVARCHAR(50) COLLATE Latin1_General_CS_AS NOT NULL

Some columns cannot be changed in this way because the column is used in an index that must be dropped first. For example:

-- Pay attention: generate script first!
DROP INDEX [resource_index_component] ON [dbo].[resource_index]
GO

ALTER TABLE [dbo].[resource_index] 
ALTER COLUMN [component_uuid] NVARCHAR(50) COLLATE Latin1_General_CS_AS NOT NULL
GO

-- Generate the create script in SQL Management Studio...
CREATE NONCLUSTERED INDEX [resource_index_component] 
  ON [dbo].[resource_index] ([component_uuid] ASC) 
  WITH (...) ON ...
GO

Restart SonarQube and start the upgrade again.