tags:

views:

1114

answers:

8

I'm looking for a way to validate the SQL schema on a production DB after updating an application version. If the application does not match the DB schema version, there should be a way to warn the user and list the changes needed.

Is there a tool or a framework (to use programatically) with built-in features to do that? Or is there some simple algorithm to run this comparison?

Update: Red gate lists "from $395". Anything free? Or more foolproof than just keeping the version number?

A: 

Make a table and store your version number in there. Just make sure you update it as necessary.

CREATE TABLE version (
    version VARCHAR(255) NOT NULL
)
INSERT INTO version VALUES ('v1.0');

You can then check the version number stored in the database matches the application code during your app's setup or wherever is convenient.

Dan
A: 

SQL Compare by Red Gate.

Justice
+1  A: 

If you are looking for a tool that can compare two databases and show you the difference Red Gate makes SQL Compare

Ed Haber
+5  A: 

You can do it programatically by looking in the data dictionary (sys.objects, sys.columns etc.) of both databases and comparing them. However, there are also tools like Redgate SQL Compare Pro that do this for you. I have specified this as a part of the tooling for QA on data warehouse systems on a few occasions now, including the one I am currently working on. On my current gig this was no problem at all, as the DBA's here were already using it.

The basic methodology for using these tools is to maintain a reference script that builds the database and keep this in version control. Run the script into a scratch database and compare it with your target to see the differences. It will also generate patch scripts if you feel so inclined.

As far as I know there's nothing free that does this unless you feel like writing your own. Redgate is cheap enough that it might as well be free. Even as a QA tool to prove that the production DB is not in the configuration it was meant to be it will save you its purchase price after one incident.

ConcernedOfTunbridgeWells
we love SQL Compare. It's made things really easy.
Bob King
A: 

Which RDBMS is this, and how complex are the potential changes?

Maybe this is just a matter of comparing row counts and index counts for each table -- if you have trigger and stored procedure versions to worry about also then you need something more industrial

David Aldridge
+4  A: 

Try this SQL.
- Run it against each database.
- Save the output to text files.
- Diff the text files.

/* get list of objects in the database */
SELECT name, 
       type 
FROM  sysobjects
ORDER BY type, name

/* get list of columns in each table / parameters for each stored procedure */
SELECT so.name, 
       so.type, 
       sc.name, 
       sc.number, 
       sc.colid, 
       sc.status, 
       sc.type, 
       sc.length, 
       sc.usertype , 
       sc.scale 
FROM   sysobjects  so , 
       syscolumns  sc 
WHERE  so.id = sc.id 
ORDER BY so.type, so.name, sc.name

/* get definition of each stored procedure */
SELECT so.name, 
       so.type, 
       sc.number, 
       sc.text 
FROM   sysobjects  so , 
       syscomments sc 
WHERE  so.id = sc.id 
ORDER BY so.type, so.name, sc.number 
AJ
Obviously a comprehensive tool is better, but this is a nice quick-and-dirty method!
Rory
+1  A: 

You didn't mention which RDMBS you're using: if the INFORMATION SCHEMA views are available in your RDBMS, and if you can reference both schemas from the same host, you can query the INFORMATION SCHEMA views to identify differences in: -tables -columns -column types -constraints (e.g. primary keys, unique constraints, foreign keys, etc)

I've written a set of queries for exactly this purpose on SQL Server for a past job - it worked well to identify differences. Many of the queries were using LEFT JOINs with IS NULL to check for the absence of expected items, others were comparing things like column types or constraint names.

It's a little tedious, but its possible.

Eric Rath
A: 

Try dbForge Data Compare for SQL Server. It can compare and sync any databases, even very large ones. Quick, easy, always delivers a correct result. Try it on your database and comment upon the product.

We can recommend you a reliable SQL comparison tool that offer 3 time’s faster comparison and synchronization of table data in your SQL Server databases. It's dbForge Data Compare for SQL Server.

Main advantages:

  • Speedier comparison and synchronization of large databases
  • Support of native SQL Server backups
  • Custom mapping of tables, columns, and schemas
  • Multiple options to tune your comparison and synchronization
  • Generating comparison and synchronization reports

Plus free 30-day trial and risk-free purchase with 30-day money back guarantee.

Devart