5

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

G. Ann - SonarSource Team
  • 22,346
  • 4
  • 40
  • 76
d1no
  • 143
  • 3
  • 14
  • 3
    You should be interested by this thread: https://groups.google.com/forum/#!searchin/sonarqube/collation%7Csort:relevance/sonarqube/2HYcA97CdY0/JtFRC2YMBgAJ – Simon Brandhof Aug 05 '16 at 09:05
  • Same problem here. Look at [this](http://stackoverflow.com/questions/9698618/sql-latin1-general-cp1-cs-as-vs-latin1-general-cs-as) SO question: "the only real difference is the sort order for some characters" – Jeroen Heier Aug 05 '16 at 15:05

1 Answers1

7

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.

Jeroen Heier
  • 3,520
  • 15
  • 31
  • 32
  • 1
    Why oh why!!! Why did SonarQube change the collation requirements!?!?!?! I have to go through option 3 to upgrade to v6 and it's going to be a nightmare! – Tyler Smith Aug 16 '16 at 14:27
  • I used option 2 and it seems to have worked like a charm. Thanks so much for this. – markdemich Aug 19 '16 at 15:15
  • I used option 2 and it seems to have worked like a charm. Thanks so much for this. UPDATE: I may have jumped the gun. After updating the DB, it seems I may have some kind of corruption. My Quality Profiles had no activated rules, but when I looked at the rules, some had the Deactivate button which implies they are activated. It's very odd. – markdemich Aug 19 '16 at 16:55
  • @TylerSmith, did you get this work. I really don't want to do option 3. – markdemich Aug 19 '16 at 16:56
  • @markdemich Yes I got option 3 to work. When I used option 2 i had 176 columns were still using the wrong collation. I had to drop about 50 indexes as option 3 suggests to finish changing the collation for the entire DB. It was very tedious but took me only about 2 hours to compile a re-usable script (I haven't completed the upgrade in production yet). – Tyler Smith Aug 19 '16 at 18:33
  • @markdemich No activated rules: Check your plugin installation and versions. – Jeroen Heier Aug 20 '16 at 05:46
  • 1
    I wound up only upgrading the the latest 5.x. When I was done, I didn't have any activated rules. I needed to delete the data/es folder an restart. That seemed to get it to work. – markdemich Aug 20 '16 at 23:19