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
situs toto pucuk4d indobet situs toto toto slot toto slot toto slot depo 10k 8kuda4d judi bola toto toto toto situs toto toto toto slot bobatoto daftar logototo situs toto situs toto toto bydplay patentoto situs toto slot 4D situs toto leon188 situs toto slot gacor terbaru toto toto situs toto ilmutoto mix parlay bayitoto mawar800 login popotogel prize hongkong malam ini toto joker11 toto slot lingkartoto ilmutoto https://foundvinylrecords.com/ depo 5k panen100 semibola MySlot188 monk4d popo togel https://asupantoto.co/ pompa4d slot pascol4d login pascol4d login sbobet88 sbobet88 https://nolimithoki.dev/ ini777 dinasti33 slot situs toto slot88 slot jepang slot depo 5k pucuk4d https://www.juara288.asia/ https://shopjoliboutique.com/ slot 4d RP888 toto toto pascol4d situs toto kari4d flokitoto slot gacor logototo slot depo 5k bobatoto bobatoto bobatoto sontogel PASCOL4D LOGIN mpo slot mataramtoto slot gacor situs toto slot situs slot situs toto pakde4d link slot popotogel login slot gacor nobu99 situs toto logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo Slot Malaysia bobatoto istanaslot sumbartoto mataramtoto meteorbet88 login 33ag rp888 AMANAHTOTO www.pajaktoto.io kari4d kari4d kari4d kari4d pascol4d pascol4d pascol4d situs toto toto slot toto slot SLOT GACOR slot gacor slot bento11 link slot gacor AMANAHTOTO KARI4D PUCUK4D pascol4d situs slot situs slot toto slot titi4d situs toto KARI4D DVTOTO PUCUK4D AMANAHTOTO slot gacor toto toto slot slot AMANAHTOTO toto jinhoki AMANAHTOTO toto slot slot777 situs slot situs slot situs slot situs slot AMANAHTOTO https://www.asupan188.dev/ slot gacor gampang menang coloktoto coloktoto coloktoto toto slot toto slot gacor bwo303 situs toto toto slot slot gacor slot88 slot gacor slot gacor slot gacor slot gacor situs slot link slot toto toto toto slot gacor slot resmi
situs toto pucuk4d indobet situs toto toto slot toto slot toto slot depo 10k 8kuda4d judi bola toto toto toto situs toto toto toto slot bobatoto daftar logototo situs toto situs toto toto bydplay patentoto situs toto slot 4D situs toto leon188 situs toto slot gacor terbaru toto toto situs toto ilmutoto mix parlay bayitoto mawar800 login popotogel prize hongkong malam ini toto joker11 toto slot lingkartoto ilmutoto https://foundvinylrecords.com/ depo 5k panen100 semibola MySlot188 monk4d popo togel https://asupantoto.co/ pompa4d slot pascol4d login pascol4d login sbobet88 sbobet88 https://nolimithoki.dev/ ini777 dinasti33 slot situs toto slot88 slot jepang slot depo 5k pucuk4d https://www.juara288.asia/ https://shopjoliboutique.com/ slot 4d RP888 toto toto pascol4d situs toto kari4d flokitoto slot gacor logototo slot depo 5k bobatoto bobatoto bobatoto sontogel PASCOL4D LOGIN mpo slot mataramtoto slot gacor situs toto slot situs slot situs toto pakde4d link slot popotogel login slot gacor nobu99 situs toto logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo Slot Malaysia bobatoto istanaslot sumbartoto mataramtoto meteorbet88 login 33ag rp888 AMANAHTOTO www.pajaktoto.io kari4d kari4d kari4d kari4d pascol4d pascol4d pascol4d situs toto toto slot toto slot SLOT GACOR slot gacor slot bento11 link slot gacor AMANAHTOTO KARI4D PUCUK4D pascol4d situs slot situs slot toto slot titi4d situs toto KARI4D DVTOTO PUCUK4D AMANAHTOTO slot gacor toto toto slot slot AMANAHTOTO toto jinhoki AMANAHTOTO toto slot slot777 situs slot situs slot situs slot situs slot AMANAHTOTO https://www.asupan188.dev/ slot gacor gampang menang coloktoto coloktoto coloktoto toto slot toto slot gacor bwo303 situs toto toto slot slot gacor slot88 slot gacor slot gacor slot gacor slot gacor situs slot link slot toto toto toto slot gacor slot resmi