views:

158

answers:

1

I'm involved is a project to migrate a project from Oracle to MySQL. I Have a script that i'm running from the MySQL shell command, called CreateTables.sql that looks like this internally:

source table\DropForeignKeys.sql
source tables\Site.sql
source tables\Language.sql
source tables\Country.sql
source tables\Locale.sql
source tables\Tag.sql

mysql --user=root --password --database=junkdb -vv < CreateTables.sql

What I'm after is a way to make the execution for the first script DropForeignKeys.sql conditional based on if the db has any tables of not. Alternatively it would be nice if there were a way to drop constraint if not exists but such a construct does not exists in MySQL to my knowledge.

So my question is how do I make the dropping of foreign key constraints conditional at script level or constraint level, so that I can have a reliable re-playable script?

+1  A: 

What I'm after is a way to make the execution for the first script DropForeignKeys.sql conditional based on if the db has any tables of not.

Conditional logic (IF/ELSE) is only supported in functions and stored procedures - you'd have to use something that resembles:

DELIMITER $$

DROP PROCEDURE IF EXISTS upgrade_database $$
CREATE PROCEDURE upgrade_database()
BEGIN

 -- INSERT NEW RECORD IF PREEXISTING RECORD DOESNT EXIST
 IF((SELECT COUNT(*) AS column_exists 
       FROM information_schema.columns 
      WHERE table_name = 'test' 
        AND column_name = 'test7') = 0) THEN
   ALTER TABLE test ADD COLUMN `test7` int(10) NOT NULL;
   UPDATE test SET test7 = test;
   SELECT 'Altered!';
 ELSE
   SELECT 'Not altered!';
 END IF;
END $$

DELIMITER ;
CALL upgrade_database();

Rather than reference INFORMATION_SCHEMA.COLUMNS, you could reference INFORMATION_SCHEMA.KEY_COLUMN_USAGE.

Depending on your needs:

ALTER TABLE [table name] DISABLE KEYS 
ALTER TABLE [table name] ENABLE KEYS 

...will disable and re-enable the keys attached to that table without needing to know each one. You can disable and enable keys on a database level using SET foreign_key_checks = 0; to disable, and SET foreign_key_checks = 1; to enable them.

It surprises me that MySQL doesn't seem to have a better way of dealing with this common scripting problem.

Oracle doesn't either, but constraints aren't really something you want to alter blindly without knowing details.

The reason I need the drop foreign keys script is because drop table yields an error when their are FK attachments. Will disabling FK checks allow for me to drop the tables?

Yes, dropping or disabling the constraints will allow you to drop the table but be aware - in order to re-enable the fk check you'll need the data in the parent to match the existing data in the child tables.

OMG Ponies
That could definitely work, thanks. It surprises me that MySQL doesn't seem to have a better way of dealing with this common scripting problem. The reason I need the drop foreign keys script is because drop table yields an error when their are FK attachments. Will disabling FK checks allow for me to drop the tables?
James