Retrace will reach End of Life on March 31, 2027. Click here to learn more.

How To Ensure a Successful Database Upgrade

By: Stackify Team
  |  May 31, 2024
How To Ensure a Successful Database Upgrade

Ensuring smooth transitions during application upgrades are important. Maintaining system consistency and stability with a database upgrade is paramount.

Databases are complex systems with numerous stored procedures, each intricately linked to critical functionalities. Changes to these procedures during an upgrade could potentially disrupt overall system performance and negatively impact thousands of customers. With such high stakes, the pressure on IT operations to ensure zero downtime and bug-free transitions is immense.

The Data Verification Solution

As a senior backend developer, I recently undertook a significant database upgrade project. To tackle this challenge, I devised a meticulous data verification strategy. At the heart of this strategy is a well-crafted and rigorously tested SQL script. Take a look:

DROP TABLE IF EXISTS #tmpDataCompareResult

CREATE TABLE #tmpDataCompareResult
(
   StoredProcedure NVARCHAR(256),
   SourceDb NVARCHAR(256),
   SourceCount INT,
   TargetDb NVARCHAR(256),
   TargetCount INT,
   CompareResult AS (CASE WHEN SourceCount = TargetCount THEN 'Identical' ELSE 'Not Identical' END)
)

DROP PROCEDURE IF EXISTS #CompareDataResultsFrom
GO

CREATE PROCEDURE #CompareDataResultsFrom
   @TargetDbName NVARCHAR(100),
   @Sql NVARCHAR(MAX)
AS
BEGIN

SET NOCOUNT ON;

DROP TABLE IF EXISTS #tmpSource
DROP TABLE IF EXISTS #tmpTarget

CREATE TABLE #tmpSource ([ ] INT)
CREATE TABLE #tmpTarget ([ ] INT)

DECLARE @AlterTable NVARCHAR(MAX)
DECLARE @AlterTargetTable NVARCHAR(MAX)
DECLARE @SqlResultSchema NVARCHAR(MAX) = (SELECT CONCAT('sp_executesql ', 'N''', @Sql, ''''))
DECLARE @TargetSql NVARCHAR(MAX) = (SELECT CONCAT(QUOTENAME(@TargetDbName), '.', @Sql))
DECLARE @SourceCount INT = 0
DECLARE @TargetCount INT = 0

-- alter #tmpSource schema
SELECT @AlterTable = string_agg(N'ALTER TABLE #tmpSource ADD ' + QUOTENAME([name]) + ' ' + system_type_name + NCHAR(13) + NCHAR (10), '') WITHIN GROUP (ORDER BY column_ordinal)
FROM sys.dm_exec_describe_first_result_set (@Sql, NULL, 0) a WHERE a.is_hidden = 0

SET @AlterTable += N'ALTER TABLE #tmpSource DROP COLUMN [ ]' + nchar(13) + nchar(10)

EXEC (@AlterTable)

-- alter #tmpTarget schema
SELECT @AlterTable = string_agg(N'ALTER TABLE #tmpTarget ADD ' + QUOTENAME([name]) + ' ' + system_type_name + NCHAR(13) + NCHAR (10), '') WITHIN GROUP (ORDER BY column_ordinal)
FROM sys.dm_exec_describe_first_result_set (@Sql, NULL, 0) a WHERE a.is_hidden = 0

SET @AlterTable += N'ALTER TABLE #tmpTarget DROP COLUMN [ ]' + nchar(13) + nchar(10)

EXEC (@AlterTable)

INSERT INTO #tmpSource
EXEC sp_executesql @Sql
SET @SourceCount = (SELECT COUNT(*) FROM #tmpSource)

INSERT INTO #tmpTarget
EXEC sp_executesql @TargetSql
SET @TargetCount = (SELECT COUNT(*) FROM #tmpTarget)

INSERT INTO #tmpDataCompareResult (StoredProcedure, SourceDb, SourceCount, TargetDb, TargetCount)
VALUES (@Sql, DB_NAME(), @SourceCount, @TargetDbName, @TargetCount)

DROP TABLE IF EXISTS #tmpSource
DROP TABLE IF EXISTS #tmpTarget

END
GO

SET NOCOUNT ON

DECLARE @TargetDb NVARCHAR(1000) = 'KnockMaster-PROD-ESv1'
DECLARE @Sql NVARCHAR(MAX)
DECLARE @CompareSql NVARCHAR(MAX)

DROP TABLE IF EXISTS #tmp
CREATE TABLE #tmp ([Sql] NVARCHAR(MAX))

INSERT INTO #tmp ([Sql]) VALUES ('dbo.sproc_MasterClient_GetDetail @clientId = null')

DECLARE tmp_cursor CURSOR LOCAL STATIC FORWARD_ONLY FOR
SELECT [Sql]
FROM #tmp

OPEN tmp_cursor

FETCH NEXT FROM tmp_cursor
INTO @Sql

WHILE @@FETCH_STATUS = 0
BEGIN

  SET @CompareSql = CONCAT('EXEC #CompareDataResultsFrom @TargetDbName = ', '''', @TargetDb, '''', ', @Sql = ', '''', @Sql, '''')

   EXEC sp_executesql @CompareSql

   FETCH NEXT FROM tmp_cursor
   INTO @Sql

END

CLOSE tmp_cursor
DEALLOCATE tmp_cursor

SELECT * FROM #tmpDataCompareResult a
ORDER BY a.CompareResult, a.StoredProcedure

Sample Results

Below is a screenshot illustrating a sample result of the data comparison:

The End Result

The verification process yielded excellent results – zero downtime and no bugs – despite the complexity of the system and the potential impact on so many customers. Using my SQL script and robust data verification strategy, any IT practitioner can ensure similar success.

As more and more companies create, deploy and maintain applications, DevOps teams need to embrace software project management best practices to maintain consistently stable, high-performance applications. The customer experience is always priority one, and leveraging the right processes and tools for the job is imperative.

Here at Stackify, our DevOps team uses the deployment tracking features of our own Retrace lifecycle APM to ensure successful upgrades. With code-level tracing, centralized error and log management, deployment tracking and more, Retrace helps IT teams keep applications fully optimized.

Jesse Gador is a senior backend .NET developer for Retrace by Stackify – a BMC Company. He is passionate about optimizing system performance and enhancing code quality, especially during complex migration projects involving SQL, Azure Kubernetes, .NET, C#, Docker and more.

Improve Your Code with Retrace APM

Stackify's APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
Explore Retrace's product features to learn more.

Learn More
togel slot situs toto pucuk4d agb99 toto slot toto togel indobet situs toto toto slot toto slot toto slot toto toto slot slot depo 10k toto togel 8kuda4d toto judi bola kenangan4d toto situs toto toto toto toto situs toto toto toto slot bobatoto daftar situs toto logototo situs toto situs toto toto bydplay patentoto situs toto slot 4D situs toto leon188 situs toto slot gacor terbaru toto toto situs toto situs toto MORFINTOTO ilmutoto toto mix parlay bayitoto toto toto toto toto ilmutoto toto toto mawar800 login popotogel prize hongkong malam ini toto joker11 toto slot pucuk4d lingkartoto Pucuk4D Login UGSLOT slot gacor ilmutoto https://foundvinylrecords.com/ depo 5k toto slot gacor panen100 semibola Pakde4D situs gacor MySlot188 toto togel 77ag monk4d popo togel tikus4d https://asupantoto.co/ Login Batak5D pompa4d lingkartoto Pakde4D lingkartoto emas 55 AMANAHTOTO efekjitu slot pascol4d login pascol4d login sbobet88 sbobet88 https://nolimithoki.dev/ ini777 benteng786 dinasti33 slot slot gacor kaskustoto situs toto slot88 slot gacor slot jepang slot depo 5k pucuk4d patentoto https://www.juara288.asia/ flokitoto https://www.aishe-j.org/ barges88 mataramtoto https://shopjoliboutique.com/ apo388 slot 4d RP888 toto toto toto toto toto slot gacor toto mataramtoto pascol4d situs toto kari4d flokitoto slot gacor logototo slot depo 5k bobatoto bobatoto bobatoto slot gacor sontogel PASCOL4D LOGIN mpo slot sumbartoto mataramtoto slot gacor situs toto slot situs slot situs toto pakde4d slot toto link slot toto
togel slot situs toto pucuk4d agb99 toto slot toto togel indobet situs toto toto slot toto slot toto slot toto toto slot slot depo 10k toto togel 8kuda4d toto judi bola kenangan4d toto situs toto toto toto toto situs toto toto toto slot bobatoto daftar situs toto logototo situs toto situs toto toto bydplay patentoto situs toto slot 4D situs toto leon188 situs toto slot gacor terbaru toto toto situs toto situs toto MORFINTOTO ilmutoto toto mix parlay bayitoto toto toto toto toto ilmutoto toto toto mawar800 login popotogel prize hongkong malam ini toto joker11 toto slot pucuk4d lingkartoto Pucuk4D Login UGSLOT slot gacor ilmutoto https://foundvinylrecords.com/ depo 5k toto slot gacor panen100 semibola Pakde4D situs gacor MySlot188 toto togel 77ag monk4d popo togel tikus4d https://asupantoto.co/ Login Batak5D pompa4d lingkartoto Pakde4D lingkartoto emas 55 AMANAHTOTO efekjitu slot pascol4d login pascol4d login sbobet88 sbobet88 https://nolimithoki.dev/ ini777 benteng786 dinasti33 slot slot gacor kaskustoto situs toto slot88 slot gacor slot jepang slot depo 5k pucuk4d patentoto https://www.juara288.asia/ flokitoto https://www.aishe-j.org/ barges88 mataramtoto https://shopjoliboutique.com/ apo388 slot 4d RP888 toto toto toto toto toto slot gacor toto mataramtoto pascol4d situs toto kari4d flokitoto slot gacor logototo slot depo 5k bobatoto bobatoto bobatoto slot gacor sontogel PASCOL4D LOGIN mpo slot sumbartoto mataramtoto slot gacor situs toto slot situs slot situs toto pakde4d slot toto link slot toto