Visual Studio’s Best Kept Secret – Compare & Update Database Schemas Right in Your IDE
Join the DZone community and get the full member experience.Join For Free
when working with different iterations of a sql database
running on internal, staging and production infrastructure it can become
a pain in the ass rolling out updates at deployment time or keeping
them in sync. developers often use third party tools to help them do
this job, however depending on what version of visual studio you have
installed, there may be another option you have overlooked, and it’s
baked right into the ide.
this post is part of a multi-part series on visual studio’s database tooling. along with an in-depth look at the database tooling, i will include ways to include these tools in your continuous integration setup to help you achieve automated database awesomeness…
visual studio premium upwards contains a whole heap of best-in-class tools for working with databases. one of those tools is the visual studio schema comparison tool i’m going to show you today. but first a little caveat;
in the land of the sql, red gate is king (sometimes…)
if you have worked with sql databases in the production of your application, you have probably heard of the developer tool producer red gate . they produce a whole bunch of tools to make a windows developer’s life easy – in the case of this post specifically, they make a tool called sql compare that is pretty popular among the community. red gate’s products are far from cheaply priced ( a single user license will set you back $395 as of june 2011 ), and in the case that you have a version of visual studio premium or higher there is absolutely no reason to accept this added cost as you have the same if not better tools, sitting right in front of you!
visual studio premium itself is not a cheap development tool either, however many developers have a copy of it for other features that are included in the higher level versions of visual studio – in the case of many msdn subscribers that were subscribed before 2010, when updating to visual studio 2010 they received a free upgrade to premium when microsoft changed the available versions of the ide that were on sale to simplify the decision at purchase time.
this post is not a discussion point as to whether red gate’s or microsoft’s products are better, but simply how developers that own a license of visual studio premium or higher can get better value from their current ide.
so shut up and show me already…
today we’re going to look at a walkthrough of a database schema comparison. to achieve this i am going to work on an assumption that you have an internal testing database, and a live database. this is quite a common approach if you have a staging or internal version of your application for available for testing.
at this point i must make another statement: i hope this database is not the one you use as a shared development database as i strongly agree with troy hunt’s post the unnecessary evil of shared development databases that this way of working is no longer in vogue. once you have gotten the hang of the schema comparison tool i am demonstrating in this post, there will be even less reason for you to work this way, as it will be easy to keep your local version up to date with the version everyone is working with.
the problem with the approach of having a local internal/staging database is that you need to constantly push updates to your production database to keep it in synch with your in-development database as you push updates to your application into production.
the great thing about the tooling built into visual studio is that it allows you to easily view the differences between your internal and production databases, and then either generate a script to bring the two into alignment or update them right then and there.
even if you do not have direct network access to your live database, the principals discussed in this post can be easily accomplished by taking a backup of your production database and restoring it locally. you can then run your comparison against this local mirrored copy.
a small bit of housekeeping
for the purposes of this how-to i'll create two sample databases to compare between so that we have an example to run with.
we will call these two databases versiontest1 and versiontest2 . i'll be using a local instance of sql express, but feel free to use whatever you have at hand including your real internal and external databases if you are “feeling lucky”.
basically the schema’s for these two databases will look like the below images. versiontest1 will have a few more fields, as we’ll treat this like our in-house development database, and versiontest2 will have less fields, as this will be our production/staging database that we want to bring inline with our development database.
you may think i have used a really simple example, and i would tend to agree that my lack of creativity in the use of this example is pretty crap – in my defence though most database changes will be iterative.
|database: versiontest1||database: versiontest2|
open sql management studio and run the following sql scripts to create these two databases and there associated tables.
create database [versiontest1] go use [versiontest1] go create table [dbo].[categories]( [categoryid] [int] identity(1,1) not null, [categoryname] [varchar](50) not null, [categorydescription] [varchar](max) null, constraint [pk_categories] primary key clustered ( [categoryid] asc )with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary] ) on [primary] go create table [dbo].[products]( [productid] [int] not null, [categoryid] [int] not null, [name] [varchar](50) not null, [price] [money] not null, constraint [pk_products] primary key clustered ( [productid] asc )with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary] ) on [primary] go set ansi_padding off go alter table [dbo].[products] with check add constraint [fk_products_categories] foreign key([categoryid]) references [dbo].[categories] ([categoryid]) go alter table [dbo].[products] check constraint [fk_products_categories] go create database [versiontest2] use [versiontest2] go create table [dbo].[categories]( [categoryid] [int] identity(1,1) not null, [categoryname] [varchar](50) not null, constraint [pk_categories] primary key clustered ( [categoryid] asc )with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary] ) on [primary] go create table [dbo].[products]( [productid] [int] not null, [categoryid] [int] not null, [name] [varchar](50) not null, constraint [pk_products] primary key clustered ( [productid] asc )with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary] ) on [primary] alter table [dbo].[products] with check add constraint [fk_products_categories] foreign key([categoryid]) references [dbo].[categories] ([categoryid]) go alter table [dbo].[products] check constraint [fk_products_categories] go
comparing our two databases
open the schema compare tool in visual studio by selecting data > schema compare > new schema comparison from the menu.
in the newly opened window create a new database connection to your source database (the database we want to compare from ) and your destination database (the database we want to compare and merge the schema from the source to ). for the purposes of this demo we’ll create new connections for both databases we created above.
create a new database connection to our source database (using our above talked about internal/staging database) versiontest1
and then create a database connection for versiontest2
now we are ready to run our schema comparison. hit ok and let ‘er rip;
visual studio will now go away and compare the two database’s schemas and work out what differences exist between the two. once it is finished doing this comparison you will be greeted by the screen below showing our source database and our destination database:
visual studio shows quite clearly each item in your database and what it thinks should be done to update your destination database to match your source database’s schema. in the above instance, it is saying that it will update both of our product and category tables on our destination database. this is exactly that we want to happen in the situation that we were updating our production database’s schema.
if you select either of the rows indicating a schema table in the above image, below the list of database schema items visual studio will show the exact change between the tables in sql script form similar to a merge tool in your favourite source control software:
if we want to avoid any of these changes, we can select the update text section of the schema object list and a drop down appears that allows me to skip this particular change from our final update script – pretty cool eh?
if we want to drill down even further into this change, we can see exactly what is different between the tables in a visual fashion by clicking the arrows next to each schema item in the list. this shows us a more fine-grained display of the changes between the tables, and allows us to skip any of the changes individually.
one of the most common things that you usually want to skip is the change of filename for the database and log file, noted by the image below showing that the tool wants to drop the file and create a new one. i will be marking these all as skip .
when we’ve had a bit of a play with the above to get a good outcome for our schema comparison/schema update, we can then shift to seeing a script that can be run against our destination database (in this instance the database versiontest2 ) to bring it in synch, by simply clicking on the icon in the toolbar that shows the label show schema update script.
once the schema update script window shows in the bottom of the screen, we can then select the button next to the show schema update script button we used above marked refresh schema script to update any changes we have made on the schema list window in the generated sql script. below is an example of the screen showing the schema update script
below is the script that visual studio has generated from the above example:
/* deployment script for versiontest2 */ go set ansi_nulls, ansi_padding, ansi_warnings, arithabort, concat_null_yields_null, quoted_identifier on; set numeric_roundabort off; go :setvar databasename "versiontest2" :setvar defaultdatapath "c:\program files\microsoft sql server\mssql10_50.sqlexpress\mssql\data\" :setvar defaultlogpath "c:\program files\microsoft sql server\mssql10_50.sqlexpress\mssql\data\" go use [master] go :on error exit go use [$(databasename)] go print n'altering [dbo].[categories]...'; go alter table [dbo].[categories] add [categorydescription] varchar (max) null; go print n'altering [dbo].[products]...'; go alter table [dbo].[products] add [price] money not null;
now that we have successfully compared our two databases, and figured
out what has changed, and then taken it one step further and compiled a
list of changes we’d like to make to our destination database to bring
it inline with our source database’s schema we have two options:
- save our deployment script for a later execution against our destination database.
write the schema updates we have selected against our destination database to bring it in synch
save my schema update script
to save our schema update script for later execution in sql management studio or to check it into source control, we have two options at the top of the window. the export to editor button in the toolbar will take the schema script and open it in a new sql management studio query window, and the export to file… does exactly as it says and brings up a save file dialog.
write the updates to our destination database
writing the updates to our destination database is even easier. simply click on the write updates button in the visual studio toolbar, and as long as the database user you are using for your database connection has the right permission to run the updates, visual studio will update the destination database in real time for you.
the versions of visual studio higher than the stock standard “professional” edition contain a lot of extra features that developers who own them often overlook or are completely unaware of. the database tools are priceless if you are lucky enough to have a version of visual studio that contains them.
in the future i really hope that microsoft includes these database tools inside the standard version of visual studio they release.
next up, i’ll show you how you can automate the creation of the above database “change” script in your continuous integration setup.
Published at DZone with permission of Douglas Rathbone, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.