Bug #62088 Forward engineer ALTER script generates different SQL than CREATE
Submitted: 4 Aug 2011 22:53 Modified: 5 Aug 2011 14:50
Reporter: Chris Newman Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Workbench: Modeling Severity:S3 (Non-critical)
Version:5.2.31a OS:Windows
Assigned to: CPU Architecture:Any

[4 Aug 2011 22:53] Chris Newman
Description:
I have a schema that creates different SQL in the CREATE script vs. the ALTER script.  I should be able to generate the CREATE script, generate an ALTER script using that CREATE script as the input, and the result should be essentially nothing.  I have proven that I can do that with other schemas.

Here is the ALTER script:
#######################################
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';

ALTER TABLE `ted_test`.`DUT_Session_Build` DROP FOREIGN KEY `fk_DUT_Session_Build_DI1` ;

ALTER TABLE `ted_test`.`Test_Results` DROP FOREIGN KEY `fk_Test_Results_Test_Cases1` ;

ALTER TABLE `ted_test`.`DUT_Session_Build` DROP COLUMN `DI_ID` , ADD COLUMN `DI_ID` INT(11) NULL DEFAULT NULL  AFTER `BLD_ID` , 
  ADD CONSTRAINT `fk_DUT_Session_Build_DI1`
  FOREIGN KEY (`DI_ID` )
  REFERENCES `ted_test`.`DUT_Identifier` (`DI_ID` )
  ON DELETE NO ACTION
  ON UPDATE NO ACTION
, DROP PRIMARY KEY 
, ADD PRIMARY KEY (`BLD_ID`, `DUT_ID`, `SES_ID`, `DI_ID`) 
, ADD INDEX `fk_DUT_Session_Build_DI1` (`DI_ID` ASC) 
, DROP INDEX `fk_DUT_Session_Build_DI1` ;

ALTER TABLE `ted_test`.`Test_Results` DROP COLUMN `TC_ID` , ADD COLUMN `TC_ID` INT(11) NULL DEFAULT NULL  AFTER `RES_ID` , 
  ADD CONSTRAINT `fk_Test_Results_Test_Cases1`
  FOREIGN KEY (`TC_ID` )
  REFERENCES `ted_test`.`Test_Cases` (`TC_ID` )
  ON DELETE NO ACTION
  ON UPDATE NO ACTION
, DROP INDEX `fk_Test_Results_Test_Cases1` 
, ADD INDEX `fk_Test_Results_Test_Cases1` (`TC_ID` ASC) ;

SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
#######################################

Here is the relevant piece of the CREATE script:
#######################################
CREATE  TABLE IF NOT EXISTS `ted_test`.`DUT_Session_Build` (
  `DUT_ID` INT NOT NULL ,
  `SES_ID` INT NOT NULL ,
  `BLD_ID` INT NOT NULL ,
  `DI_ID` INT NULL ,
  PRIMARY KEY (`BLD_ID`, `DUT_ID`, `SES_ID`, `DI_ID`) ,
  INDEX `fk_Result_Status_has_Build_Build1` (`BLD_ID` ASC) ,
  INDEX `fk_DUT_Result_Build_DUTs1` (`DUT_ID` ASC) ,
  INDEX `fk_DUT_Result_Build_Sessions1` (`SES_ID` ASC) ,
  INDEX `fk_DUT_Session_Build_DI1` (`DI_ID` ASC) ,
  CONSTRAINT `fk_Result_Status_has_Build_Build1`
    FOREIGN KEY (`BLD_ID` )
    REFERENCES `ted_test`.`Build` (`BLD_ID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_DUT_Result_Build_DUTs1`
    FOREIGN KEY (`DUT_ID` )
    REFERENCES `ted_test`.`DUTs` (`DUT_ID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_DUT_Result_Build_Sessions1`
    FOREIGN KEY (`SES_ID` )
    REFERENCES `ted_test`.`Sessions` (`SES_ID` )
    ON DELETE CASCADE
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_DUT_Session_Build_DI1`
    FOREIGN KEY (`DI_ID` )
    REFERENCES `ted_test`.`DUT_Identifier` (`DI_ID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE  TABLE IF NOT EXISTS `ted_test`.`Test_Results` (
  `TR_ID` INT NOT NULL AUTO_INCREMENT ,
  `RES_ID` INT NULL ,
  `TC_ID` INT NULL ,
  `TR_State` VARCHAR(45) NULL ,
  `TR_Status` VARCHAR(256) NULL ,
  `Warnings` INT NULL DEFAULT 0 ,
  `Errors` INT NULL DEFAULT 0 ,
  `TR_Start` DATETIME NULL ,
  `TR_Updated` DATETIME NULL ,
  `TR_Percent_Complete` INT NULL DEFAULT 0 ,
  `TR_Result` VARCHAR(45) NULL ,
  `TR_Blessed` TINYINT NULL DEFAULT 0 ,
  PRIMARY KEY (`TR_ID`) ,
  INDEX `fk_Test_Result_Values_Result_Status1` (`RES_ID` ASC) ,
  INDEX `fk_Test_Results_Test_Cases1` (`TC_ID` ASC) ,
  CONSTRAINT `fk_Test_Result_Values_Result_Status1`
    FOREIGN KEY (`RES_ID` )
    REFERENCES `ted_test`.`Result_Status` (`RES_ID` )
    ON DELETE CASCADE
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Test_Results_Test_Cases1`
    FOREIGN KEY (`TC_ID` )
    REFERENCES `ted_test`.`Test_Cases` (`TC_ID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

#######################################

There appear to be a few differences, mostly like the ALTER script is being more specific with field sizes and default values than the CREATE script.

I would retest this in 5.2.34, but generating an ALTER script crashes.  I've already written up that bug.

How to repeat:
I'm not sure exactly since these tables appear similar to my other tables.

I do not want to publicly post my .mwb file, but I will work with whatever developer who works on this.

Suggested fix:
Align the SQL so the ALTER script generates no real SQL statements when compared to the identical CREATE script.
[5 Aug 2011 3:39] Valeriy Kravchuk
What exact bug in 5.2.34 do you mean? Please, give bug number or URL. This is the only active bug report from you that I can see.
[5 Aug 2011 14:06] Chris Newman
http://bugs.mysql.com/bug.php?id=62087
[5 Aug 2011 14:50] Chris Newman
I tried again with my schemas.  I regenerated the CREATE scripts for my past schemas and the ALTER scripts to move between them looked as expected.

So I performed the test again that I described in my initial post.  I opened up an MWB file, generated the CREATE script, generated the ALTER script using that CREATE script as the source, and there were unexpected SQL statements essentially deleting and recreating a few columns/foreign keys.

I found that odd so I tried it again and the ALTER script was clean/empty like I expected.

I should note I've been closing the application and double-clicking the MWB files to reopen Workbench.  Perhaps closing/opening Workbench and moving between schemas is somehow causing the unpredictable behavior.  Maybe there is some stale data accidentally being picked up somewhere?

It seems hard to reproduce now that I am closing Workbench and only reopening the same MWB file.  I've finally managed to generate the files I need so I guess you can close this if you like.