SQL Server Database Upgrade Fails Due to Duplicate "com.atlassian.bitbucket.server.bitbucket-auto-merge" Key in plugin_setting

XMLWordPrintable

      Summary

      When performing an upgrade from Stash 3.x to Bitbucket Server 4.0.0, Bitbucket Server fails to start when upgrading the database schema with the following error:

      2015-09-23 12:00:39,453 ERROR [spring-startup]  c.a.j.s.w.s.JohnsonDispatcherServlet SpringMVC dispatcher [springMvc] could not be started
      org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'crowdUserDao' defined in class path resource [crowd-spi-context.xml]: Unsatisfied dependency expressed through constructor argument with index 1 of type [com.atlassian.stash.internal.project.ProjectDao]: : Error creating bean with name 'projectDao' defined in URL [jar:file:/opt/atlassian/bitbucket/4.0.0/atlassian-bitbucket/WEB-INF/lib/bitbucket-dao-impl-4.0.0.jar!/com/atlassian/stash/internal/project/HibernateProjectDao.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: : Error creating bean with name 'liquibase' defined in class path resource [stash-context.xml]: Invocation of init method failed; nested exception is liquibase.exception.MigrationFailedException: Migration failed for change set liquibase/r4_0/upgrade.xml::STASHDEV-9922-3::bturner:
           Reason: liquibase.exception.DatabaseException: Error executing SQL UPDATE [dbo].[plugin_setting] SET [key_name] = substring(key_name, 38, len(key_name) - 38), [namespace] = 'com.atlassian.bitbucket.server.bitbucket-auto-merge' WHERE namespace = 'com.atlassian.stash.stash-auto-merge': Violation of UNIQUE KEY constraint 'uq_plug_setting_ns_key'. Cannot insert duplicate key in object 'dbo.plugin_setting'. The duplicate key value is (enabled., com.atlassian.bitbucket.server.bitbucket-auto-merge).:
                Caused By: Error executing SQL UPDATE [dbo].[plugin_setting] SET [key_name] = substring(key_name, 38, len(key_name) - 38), [namespace] = 'com.atlassian.bitbucket.server.bitbucket-auto-merge' WHERE namespace = 'com.atlassian.stash.stash-auto-merge': Violation of UNIQUE KEY constraint 'uq_plug_setting_ns_key'. Cannot insert duplicate key in object 'dbo.plugin_setting'. The duplicate key value is (enabled., com.atlassian.bitbucket.server.bitbucket-auto-merge).:
      

      This is occurs when there are more than 1 com.atlassian.stash.stash-auto-merge entries in the plugin_setting table (i.e. auto merging has been enabled/disabled for a repository in the branching model at some point.) If there is only 1 entry, it gets improperly renamed to lose the corresponding ID.

      Environment

      This was tested with MS SQL Server 2012.

      Steps to Reproduce

      1. In Stash 3.x, enable auto merging in the branching model for 2 repositories.
      2. Perform an upgrade to Bitbucket Server 4.0.0.
      3. Bitbucket Server fails to start.

      Expected Results

      Bitbucket Server upgrades the database schema correctly.

      Actual Results

      The below error is thrown in the atlassian-bitbucket.log file:

      2015-09-23 12:00:39,453 ERROR [spring-startup]  c.a.j.s.w.s.JohnsonDispatcherServlet SpringMVC dispatcher [springMvc] could not be started
      org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'crowdUserDao' defined in class path resource [crowd-spi-context.xml]: Unsatisfied dependency expressed through constructor argument with index 1 of type [com.atlassian.stash.internal.project.ProjectDao]: : Error creating bean with name 'projectDao' defined in URL [jar:file:/opt/atlassian/bitbucket/4.0.0/atlassian-bitbucket/WEB-INF/lib/bitbucket-dao-impl-4.0.0.jar!/com/atlassian/stash/internal/project/HibernateProjectDao.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: : Error creating bean with name 'liquibase' defined in class path resource [stash-context.xml]: Invocation of init method failed; nested exception is liquibase.exception.MigrationFailedException: Migration failed for change set liquibase/r4_0/upgrade.xml::STASHDEV-9922-3::bturner:
           Reason: liquibase.exception.DatabaseException: Error executing SQL UPDATE [dbo].[plugin_setting] SET [key_name] = substring(key_name, 38, len(key_name) - 38), [namespace] = 'com.atlassian.bitbucket.server.bitbucket-auto-merge' WHERE namespace = 'com.atlassian.stash.stash-auto-merge': Violation of UNIQUE KEY constraint 'uq_plug_setting_ns_key'. Cannot insert duplicate key in object 'dbo.plugin_setting'. The duplicate key value is (enabled., com.atlassian.bitbucket.server.bitbucket-auto-merge).:
                Caused By: Error executing SQL UPDATE [dbo].[plugin_setting] SET [key_name] = substring(key_name, 38, len(key_name) - 38), [namespace] = 'com.atlassian.bitbucket.server.bitbucket-auto-merge' WHERE namespace = 'com.atlassian.stash.stash-auto-merge': Violation of UNIQUE KEY constraint 'uq_plug_setting_ns_key'. Cannot insert duplicate key in object 'dbo.plugin_setting'. The duplicate key value is (enabled., com.atlassian.bitbucket.server.bitbucket-auto-merge).:
      

      Workaround

      1. Stop Bitbucket Server.
      2. Back up your database.
      3. Execute the following SQL against your database:
        UPDATE  dbo.plugin_setting
        SET     key_name = SUBSTRING(key_name, 38, LEN(key_name) - 37)
               ,namespace = 'com.atlassian.bitbucket.server.bitbucket-auto-merge'
        WHERE   namespace = 'com.atlassian.stash.stash-auto-merge';
        
      4. Start Bitbucket Server

      Possible Side-effects if you did NOT encounter this issue on upgrade

      If you upgraded to 4.0.0, and did not encounter the unique key constraint violation, and had auto-merge enabled on any repositories, then your auto-merge settings will have been silently truncated (by chopping the last decimal digit off every repo id). This will result in the auto-merge settings being applied to different repositories and removed from the repository it was set on.
      As this silent truncation has lost information, we are unable to provide any automated upgrade task to recover it.
      We suggest retrieving the lost information from your backup taken prior to the upgrade of 4.0.0, and re-enabling the auto-merge settings for those repositories.

            Assignee:
            Cristan Szmajda (Inactive)
            Reporter:
            David Lahn (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: