umbraco / Umbraco-CMS

Umbraco is a free and open source .NET content management system helping you deliver delightful digital experiences.
https://umbraco.com
MIT License
4.43k stars 2.67k forks source link

V13.3.0 - Upgrade Error - SqlException: Either the parameter @objname is ambiguous or the claimed @objtype (OBJECT) is wrong. #16267

Closed justin-nevitech closed 1 month ago

justin-nevitech commented 4 months ago

Which Umbraco version are you using? (Please write the exact version, example: 10.1.0)

13.3.0

Bug summary

Upgrading a site to V13.3.0 throws the following SQL exception on a database that was previously migrated from V7:

Either the parameter @objname is ambiguous or the claimed @objtype (OBJECT) is wrong.###

Specifics

The error is caused by the following SQL statement for the V13.3.0 upgrade in the AlignContentVersionTable function within AlignUpgradedDatabase:

EXEC sp_rename N'DF_cmsContentVersion_VersionDate', N'DF_umbracoContentVersion_versionDate', N'OBJECT'

See here:

https://github.com/umbraco/Umbraco-CMS/blob/b0b2b597403bb381c23ff47f2b1ffc8aeaab1dc0/src/Umbraco.Infrastructure/Migrations/Upgrade/V_13_3_0/AlignUpgradedDatabase.cs#L139

This is assuming the name of the constraint being renamed is DF_cmsContentVersion_VersionDate, but for some reason the name of the constraint in my database was DFumbracoCoVersi__6DCC4D03. Once I manually renamed the constraint to DF_cmsContentVersion_VersionDate the upgrade would continue and run successfully.

The database had recently been upgraded from V7 to V13.2.2 (via V8, V10, v11 and v12) but checking a copy of the V7 database the name of the constraint had been there since V7.

The full stack trace is:

Microsoft.Data.SqlClient.SqlException (0x80131904): Either the parameter @objname is ambiguous or the claimed @objtype (OBJECT) is wrong.
at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at Microsoft.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean isAsync, Int32 timeout, Boolean asyncWrite)
at Microsoft.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String methodName)
at Microsoft.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at StackExchange.Profiling.Data.ProfiledDbCommand.ExecuteNonQuery() in C:\\projects\\dotnet\\src\\MiniProfiler.Shared\\Data\\ProfiledDbCommand.cs:line 268
at Umbraco.Cms.Infrastructure.Persistence.FaultHandling.FaultHandlingDbCommand.<ExecuteNonQuery>b__32_0()
at Umbraco.Cms.Infrastructure.Persistence.FaultHandling.FaultHandlingDbCommand.<>c__DisplayClass38_0`1.<Execute>b__0()
at Umbraco.Cms.Infrastructure.Persistence.FaultHandling.RetryPolicy.ExecuteAction[TResult](Func`1 func)
at Umbraco.Cms.Infrastructure.Persistence.FaultHandling.FaultHandlingDbCommand.Execute[T](Func`1 f)
at Umbraco.Cms.Infrastructure.Persistence.FaultHandling.FaultHandlingDbCommand.ExecuteNonQuery()
at NPoco.Database.<>c__DisplayClass296_0.<ExecuteNonQueryHelper>b__0()
at NPoco.Database.ExecutionHook[T](Func`1 action)
at NPoco.Database.ExecuteNonQueryHelper(DbCommand cmd)
at NPoco.Database.Execute(String sql, CommandType commandType, Object[] args)
at NPoco.Database.Execute(Sql Sql)
at Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_13_3_0.AlignUpgradedDatabase.AlignContentVersionTable(ColumnInfo[] columns)
at Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_13_3_0.AlignUpgradedDatabase.Migrate()
at Umbraco.Cms.Infrastructure.Migrations.MigrationBase.Run()
at Umbraco.Cms.Infrastructure.Migrations.MigrationPlanExecutor.RunMigration(Type migrationType, MigrationContext context)
at Umbraco.Cms.Infrastructure.Migrations.MigrationPlanExecutor.RunScopedMigration(Type migrationType, MigrationPlan plan)
at Umbraco.Cms.Infrastructure.Migrations.MigrationPlanExecutor.RunMigrationPlan(MigrationPlan plan, String fromState)

I'm mainly included this here so anyone else that comes across this can find a solution.

Steps to reproduce

I've checked a few other V7 databases and found one other instance where the constraint was named differently, so I don't know at what point that constraint was created or by what. I don't have any other steps to reproduce other than what is already described above.

Expected result / actual result

The upgrade should not fail if it cannot find the constraint being renamed. As the constraint already exists (albeit with the wrong name), this should not cause any problems. If possible, the incorrect constraint name should be detected and renamed.

github-actions[bot] commented 4 months ago

Hi there @justin-nevitech!

Firstly, a big thank you for raising this issue. Every piece of feedback we receive helps us to make Umbraco better.

We really appreciate your patience while we wait for our team to have a look at this but we wanted to let you know that we see this and share with you the plan for what comes next.

We wish we could work with everyone directly and assess your issue immediately but we're in the fortunate position of having lots of contributions to work with and only a few humans who are able to do it. We are making progress though and in the meantime, we will keep you in the loop and let you know when we have any questions.

Thanks, from your friendly Umbraco GitHub bot :robot: :slightly_smiling_face:

PhilMakower commented 4 months ago

Great I had the same error message. I had also migrated a site from v7 and the constraint had a similar name to yours but with a different hex value on the end. Once I changed the name to the correct one as above, the migration completed and I got a site!

PhilMakower commented 4 months ago

I created different databases for the various stages of migration: v7 to v8, v8 to v10 then v10 to v13. So I can see those databases and what they contained at the various stages. I can see that the constraint in the v7 database is called DF__cmsConten__Versi__5C6CB6D7 in my case. I guess that changing the constraint name in the v7 database before I start the migration again would solve the issue? The name does not seem to be changed during the migrations apart from the v13 to v13.3, so hopefully this won't stop the other migrations from working and will fix the v10 to v13.3 migration?

justin-nevitech commented 4 months ago

That's great you managed to recreate the problem and the same fix worked, that's why I raised the issue mainly so it helped others! I guess you can change the constraint name at any point before migrating to v13.3?

PFEW-ITSD commented 4 months ago

Thanks for this @justin-nevitech! I was having a similar issue in trying to upgrade some sites from v7 to v13, with the error occurring in the upgrade from v10 to v13. Changing the constraint name got everything working for me.

PhilMakower commented 3 months ago

Did the final migration, changed the constraint name in the v7 DB, all OK

Migaroez commented 1 month ago

Hey y'all this is an interesting one. Did some digging in the code and found the methods that are responsible for creating the SQL commands based on the DTO definitions. From the (unclear) git history it seems like all of this was introduced around version 6. Do any of you know whether these DB's that have the issue were created prior to V7?

justin-nevitech commented 1 month ago

Hi @Migaroez No, the version I was upgrading was originally written in V7...

Migaroez commented 1 month ago

As a possible solution to detect the right constraint to be renamed, could you run the following SQL on the original DB and see if it gives the correct system-named constraint?

SELECT obj_Constraint.NAME AS 'constraintName'
    FROM sys.objects obj_table 
        JOIN sys.objects obj_Constraint 
            ON obj_table.object_id = obj_Constraint.parent_object_id 
        JOIN sys.sysconstraints constraints 
             ON constraints.constid = obj_Constraint.object_id 
        JOIN sys.columns columns 
             ON columns.object_id = obj_table.object_id 
            AND columns.column_id = constraints.colid 
    WHERE obj_table.NAME = 'cmsContentVersion'
    AND columns.NAME = 'versionDate'
    AND obj_Constraint.type = 'D'
Migaroez commented 1 month ago

Hi @Migaroez No, the version I was upgrading was originally written in V7...

Do you happen to know what exact version the initial DB was created in? Not super important if we can get the fix to work, but I like understanding problems 😁

justin-nevitech commented 1 month ago

Hi @Migaroez The earliest reference I can find to the initial version is 7.4.3

I've run your SQL and it does return the name of the incorrect constraint.

PhilMakower commented 1 month ago

In 2015 my site was on 7.1.6. The project started in 2014 so may have started on a previous version but probably we stayed with the version we started from. We used Contour for forms. (this made the migration extra complicated since we actually managed to stay with Contour for v7 upgrades!) https://our.umbraco.com/download/releases/716 says it is from 25 Aug 2014 which fits.

Migaroez commented 1 month ago

Would either of you be up for sending me a v12 or v13.x.priorToWhenTheTroubledMigrationWasIntroduced version of the impacted DB Or test the PR branch on your db when it's ready?

justin-nevitech commented 1 month ago

I'd be happy to test the PR branch when it is ready. I don't think I've got a copy of the database on those versions so I would have to migrate again from the V7 version I have.

Migaroez commented 1 month ago

@justin-nevitech PR is ready for testing

justin-nevitech commented 1 month ago

Hi @Migaroez Just tested a migration with your PR branch and it all works fine, thank you!

Before:

image

After:

image

Zeegaan commented 1 month ago

Fixed in https://github.com/umbraco/Umbraco-CMS/pull/16891