Domanda

I've got two databases, one for my development system and another which is the productive system.

Is it possible to detect the structure changes between both systems? (no data, only the structure changes should be detected)

At the moment I update a textfile, which containes all my structure changes of tables and stored procedures. I want to do that automatically, for example in a deploy shell script.

The following is an attempt to solve this problem. The problem is that I don't get full sql statements which can be executed.

mysqldump --skip-comments --no-data --routines --host=sql.test.de --user=root --password=123 bim_stage > /home/deploybackups/stagestructure.sql 
mysqldump --skip-comments --no-data --routines --host=sql.test.de --user=root --password=123 bim_dev > /home/deploybackups/devstructure.sql

diff /home/deploybackups/stagestructure.sql /home/deploybackups/devstructure.sql > /home/deploybackups/diffstructure.sql
È stato utile?

Soluzione

I'm sure you can fix most problems with some text or GUI compare tool . However, every time the format is changed or someone is not following your standard, everything will seem to have changed. Somewhere down the line you will find yourself fixing strange errors such as counting whitespaces etc.

What I am getting at, is that in my experience this is not entirely the best way to do this. Over the years I've seen a few different attempts on how to solve this updating problem.

  • Visual Studio has one angle on this by creating a database project which keeps all changes in some sort of version log. not entirely sure on the specifics. Simply put you press Sync and tada! It works but is very comprehensive and slow if you ask me.

The easiest method to handle this IMO, is to make all structural changes into a version log/table; so to speak.

Lets say your basic database needs a new Table. You would go into your editor and add it (using GUI or code), press save and you are done.

Instead press Extract SQL; don't press save.

What I propose now, is this:

You build a little internal web interface with a big input box and a submit button. Hitting the submit button will execute any SQL from the input box against your Local development database and return to you the result.

If the DB returns success, the SQL string is added to your changeLogSQLVersionThingyTable with at least three columns:

1) Auto incremented version (Integer) 2) The sql (text) 3) Timestamp (timedate) 4) Developer Id ??

When you have changes to your DB, such as adding the table from before, use this tool instead.

You now have all your changes to your Database in a nice version list. To update a database all you have to do is execute every row in the changeLogSQLVersionThingyTable from what ever version that particular database was updated into last time.

With some effort you can also rebuild your database to a earlier point; just create a new db and run all rows all the way up to your desired version point.

Altri suggerimenti

You can use MySQL GUI tools like SQLyog, to do it easily. SQlyog's Schema Synchronization wizard is capable of doing what you require, i.e. to check for changes in structure and synchronize them. See the screenshot attached.

Schema Sync SQLyog

You're trying to make a technical solution when it's actually the process that is broken. Who made the original change to production without arranging for the exact same sql to be applied to staging, QA, and development?

That said, if you have a "normal"-sized database and these changes happen "infrequently", I'd use the diff method to determine that a change has been made. Manually making a SQL script to affect the change won't be hard. I'd still harass the person who made the change, though.

Note that diff has options to ignore whitespace/blankline changes.

When you're done, check the new schema into your source code repository. It'll come in handy someday.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top