how to compare/validate sql schema

2019-03-12 14:26发布

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?

标签: sql redgate
11条回答
我命由我不由天
2楼-- · 2019-03-12 14:46

I hope I can help - this is the article I suggest reading:

Compare SQL Server database schemas automatically

It describes how you can automate the SQL Server schema comparison and synchronization process using T-SQL, SSMS or a third party tool.

查看更多
放我归山
3楼-- · 2019-03-12 14:47

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.

查看更多
冷血范
4楼-- · 2019-03-12 14:52

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

查看更多
干净又极端
6楼-- · 2019-03-12 14:59

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

查看更多
Fickle 薄情
7楼-- · 2019-03-12 15:03

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.

查看更多
登录 后发表回答