SQLServerCentral - www.sqlservercentral.com

A community of more than 1,600,000 database professionals and growing

Featured Contents

Featured Script

The Voice of the DBA

Rolling Back Migrations

I happen to be a fan of database migrations as a way of making and deploying database changes. This is an approach that tracks each of the scripts run by developers in their working environments and the replays these scripts in production to deploy the changes. It works really well, and is the most bulletproof method I know of for ensuring the changes will work in production. That's not to say there aren't issues, but it's the approach I favor. It's a part of what SQL Change Automation from Redgate Software does, and it's also what Microsoft and other companies see as the future of database deployments.

This is in contrast to making changes in development and then using some technology such as SQL Compare or Schema Compare in SSDT to create a script and use that to deploy changes. That works very well for many people, but I do find that most customers outgrow the technology with certain changes that don't lend themselves to this state or model based method of script generation. 

In either case, rollbacks are a concern for many DBAs and developers. After all, the database is a stateful service, as our data must be maintained over time and there are certain changes that are difficult to rollback. While many developers that might have renamed an entity or added a column might be tempted to just reverse the change, this isn't always easy to automate, especially in most of the tools we use. Often we depend on the skill of a particular individual to manually execute the reversing DDL, and possibly determine what to do with any data that was changed.

I ran across another developer that things migrations are a better way, and that in a year of development, they never had to roll back any changes. While I agree that migrations is a more reliable process, I do think that assuming you'll never roll back a deployment is highly optimistic. I've had tales of application developers being out of sync, of other applications not working with a new version of the database, and more. Some of these might be fixed with a quick roll forward, but ultimately I think that there needs to be an easy way to make rollbacks normal.

The one downside of migrations is that the reversing transactions can be complex, since each migration script might make complex changes moving forward. There isn't much help from most migrations tools, whether that's SQL Change Automation, FlywayDB, Entity Framework migrations, and more. These tools can certainly generate reversing code for simple changes, but any sort of complex alteration requires custom code.

My view is that a few simple rules govern how I view rollbacks. For views, procs, functions, I'd grab the previous version from our VCS and re-deploy that. I might try to run through a DevOps pipeline, but if I were in a hurry, I'd grab the code and run it. I'd also then recommit the old version as the latest one. For tables, we should write reversing migration scripts for any entities that are risky. Risky meaning this might affect my employment status. I'd be sure I had a second deployment pipeline that I could use to run these scripts in QA, staging/pre-prod, etc. after we'd verified the code we were deploying. I'd then have checks to ensure we really were reversing the changes.

The last rule I have is that I use time to make deployments easier. I would never add new columns and drop old ones in the same deployment. If I move or change data, I'd always ensure the old versions of data remained. That way I could reverse changes without problems. I can always do cleanup in a later deployment that just removes objects or data, but I want to be sure that old data is really no longer needed. That means I need to be organized and have a good calendar system to scheduling future cleanup work as well.

Migrations are really a better way to do database deployment, whether you're working in a relational system, or you might be altering documents in a NoSQL system. Replay the changes you've made in development, that you are sure worked on "your" machine. You'll be more confident they'll work on another machine.

Steve Jones from SQLServerCentral.com

Join the debate, and respond to today's editorial on the forums


The Voice of the DBA Podcast

Listen to the MP3 Audio ( 6.4MB) podcast or subscribe to the feed at iTunes and Libsyn. feed

The Voice of the DBA podcast features music by Everyday Jones. No relation, but I stumbled on to them and really like the music.

ADVERTISEMENT
SQL Source Control

How to track every change to your SQL Server database

See who’s changing your database, alongside affected objects, date, time, and reason for the change with SQL Source Control. Get a full change history in your source control system. Learn more

SQL Change Automation

CI/CD  for your SQL Server database

Feeling the pain of managing and deploying database changes manually? Redgate SQL Change Automation completes your database delivery process by building, testing, and deploying the database changes you and your team check into version control.  
Try it free

Featured Contents

 

Basic Administration Tasks - Level 5 of the Stairway to for Azure SQL Database

Arun Sirpal from SQLServerCentral.com

In this next level, learn how to accomplish a few simple administration tasks that may help you manage your Azure SQL Database. More »


 

Free eBook: SQL Server Backup and Restore

Press Release from Redgate

In this free eBook Shawn McGehee offers advice on query tuning, cutting stored procedures, and system process design and implementation for high availability. Discover how to perform backup and restore operations using SQL Server Management Studio (SSMS), basic T-SQL scripts and Redgate's SQL Backup tool. More »


 

Generate HTML Formatted Email of SQL Server Database Consistency Check Errors

Additional Articles from MSSQLTips.com

In this tip we look at to create a SQL Server DBCC CHECKDB errors email formatted in an HTML table. This will report any errors for all of your databases on the server. More »


 

From the SQLServerCentral Blogs - Connecting to SQL Server 2017 using Visual Studio 2017 and getting “An incompatible SQL Server version detected”

hamish.watson8 from SQLServerCentral Blogs

This blog post details the error you may get when using Visual Studio 2017 and you get errors that you... More »


 

From the SQLServerCentral Blogs - Using Extended Events to Capture Implicit Conversions

Grant Fritchey from SQLServerCentral Blogs

Using the appropriate data type to avoid conversions or implicit conversions is a fundamental approach to good T-SQL coding practices.... More »

Question of the Day

Today's Question (by Steve Jones):

I have this data frame in R:

 > HR.hitters rank players Hr 1 23 NA 784 2 25 NA 778 3 25 NA 736 4 26 NA 718 5 27 NA 682 6 23 NA 647 7 29 NA 641 8 30 NA 634 9 27 NA 627 10 31 NA 607 

I now want to add in the years player. I have created this vector:

 > yrs.played <- c(22,23,22,22,22,17,22,22,18,21) 

How do I add my vector to the data frame?

Think you know the answer? Click here, and find out if you are right.


We keep track of your score to give you bragging rights against your peers.
This question is worth 1 point in this category: R Language.

We'd love to give you credit for your own question and answer.
To submit a QOTD, simply log in to the Contribution Center.

ADVERTISEMENT

Exam Ref 70-774 Perform Cloud Data Science with Azure Machine Learning

Prepare for Microsoft Exam 70-774 and help demonstrate your real-world mastery of performing key data science activities with Azure Machine Learning services. Designed for experienced IT professionals ready to advance their status, Exam Ref focuses on the critical thinking and decision-making acumen needed for success at the MCSA level.  Get your copy today from Amazon.

Yesterday's Question of the Day

Yesterday's Question (by Steve Jones):

I have this table:

 CREATE TABLE UserConfig ( UserConfigKey INT IDENTITY(1,1) NOT NULL CONSTRAINT UserConfigPK PRIMARY KEY , UserID INT , IsActive BIT SPARSE , IsSubscriber BIT SPARSE , DefaultQuantity INT SPARSE , Options XML COLUMN_SET FOR ALL_SPARSE_COLUMNS ) GO 

I want to insert a new row and use this statement:

 INSERT dbo.UserConfig ( UserID , Options ) VALUES (9, '<IsActive>3</IsActive><IsSubscriber>0</IsSubscriber>') GO 

What happens?

Answer: The row is inserted, with a null for the Default Quantity column

Explanation:

The rows is inserted, and a NULL is used for any sparse columns not included in the XML document.

Ref: Use Column Sets - click here


» Discuss this question and answer on the forums

Featured Script

sp_spaceused2

Joe Chang from SQLServerCentral.com

this is created as a system stored procedure in master, 

you will need permission to mark as system object.

There may be others who have also created their own procedure with this name, so adjust as appropriate

EXEC sys.sp_MS_marksystemobject'sp_spaceused2'

More »

Database Pros Who Need Your Help

Here's a few of the new posts today on the forums. To see more, visit the forums.

SQL Server 2017 : SQL Server 2017 - Development

Find average of child records within date range - hi, I am working with three different tables: headerTable lineTable and saleDateTable I am trying to figure out average saleAmount (lineTable) for each of the...

Can anyone suggest a SQL Server Advanced course? - Hi! While ago, I had a lot of fun solving the sql-ex.ru queries. Moreover, seeing how other solved the problem was...


SQL Server 2016 : SQL Server 2016 - Administration

CheckDB not completing because of resource errors - I am testing a new SQL 2016 Std build in AWS. Part of the testing includes running checkdb on all...

Monitoring slow running queries of different variety of SQL Server versions without any 3rd party tool. - We have different SQL Servers versions and they are developed by different 3rd party. We receive complaints about performance and...


SQL Server 2016 : SQL Server 2016 - Development and T-SQL

increment months for two dates combination as a new label column - I have to calculate a new Label column for each combination of Start Date and End Date. If start Date...

Running Concatenation (like running totals) - Hi I have table with the following content ColA   ColB  RowNumber 100      X        1 100      Y        2 200      K        1 200      L         2 200     M         3 From this...

Deleting data from a table ... quickly - I'm working on a system which stores what you might call "temporary working data" in a number of database tables....

index scan where seek expected - On a SQL 2016 instance I have a large table with dozens of columns including: COLA INT COLB BIT and nonclustered index: (COLA...


SQL Server 2014 : Administration - SQL Server 2014

MAXDOP ,Server Core & SELECT - Hi Experts, I have a complex select statement with Union All and have multiple select inside which fetches around 1 million...


SQL Server 2014 : Development - SQL Server 2014

Loop Thru XML Tags with Cursor and Insert Parent/Child data into a table. - So, here's the XML.. I am relying on order of presence in file. <?xml version="1.0"?> <ROOT>     <Policy>         <PolicyNumber>HO00000001</PolicyNumber>         <TransactionType>NB</TransactionType>     &nbsp

Display 'blank' cell when it is null. - The data type of the column is decimal(9,2). I wanted an empty cell to be displayed when the output is...


SQL Server 2012 : SQL 2012 - General

Were are the email addresses stored for SSRS Subscriptions ? Also how can I modify them via code - The following code is the furthest I went on digging in to Report Server. Can anyone help me to find...

Cannot backup TDE Certificate - I'm trying to backup the TDE Certificate but the OS reboots when executing the query: BACKUP CERTIFICATE MyCertForTDE TO FILE = 'TestDB1_Cert.cer' WITH...


SQL Server 2012 : SQL Server 2012 - T-SQL

badly perfoming query when IS NOT NULL used in the WHERE clause - I have a query that returns results in under a second (115 rows) if I use IS NULL in the WHERE clause,...

Query slow. Help improve? - Hey all.  I have a contract building a database for a company that bills other companies, and I wrote what...


SQL Server 2008 : SQL Server 2008 - General

How to do a basic loop in SQL - Hi all, I have a need to read every record in a table and compare a name in that table to...

I am struggling with selecting specific information out of a strings - So I have a feed from my sales order entry system.  the first section of the string is the company...

relational calculus - I'm solving a problem using tuple relational calculus ( TRC) in DBMS. Problem Find the name of all the employees who work for...


Reporting Services : SSRS 2014

Report server on two different servers - Hi, Please help me to understand if this can be done.I have SQLServer A hosting the reporting server, i also have...


Programming : Connecting

Oracle SQL Developer to MS SQL Server - I have a coworker in the UK using a MacBook and Oracle SQL Developer to connect to one of our...

This email has been sent to newsletter@newslettercollector.com. To be removed from this list, please click here.
If you have any problems leaving the list, please contact the webmaster@sqlservercentral.com.
This newsletter was sent to you because you signed up at SQLServerCentral.com.
Feel free to forward this to any colleagues that you think might be interested.
If you have received this email from a colleague, you can register to receive it here.
This transmission is ©2018 Redgate Software Ltd, Newnham House, Cambridge Business Park, Cambridge, CB4 0WZ, United Kingdom. All rights reserved.
Contact: webmaster@sqlservercentral.com