Skip to content

Instantly share code, notes, and snippets.

@btilly
Created August 18, 2011 21:35
Show Gist options
  • Save btilly/1155299 to your computer and use it in GitHub Desktop.
Save btilly/1155299 to your computer and use it in GitHub Desktop.
If you load the dest schema into mysql, the upgrade SQL presented does not work. If task_role does not exist first, the problem goes away. Tested on MySQL Ver 5.0.51a-24+lenny5
#! /usr/bin/perl
use strict;
use warnings;
use SQL::Translator;
use SQL::Translator::Diff;
use Data::Dumper;
$Data::Dumper::Indent = 1;
my $dest_schema = get_schema(q{
DROP TABLE IF EXISTS `task`;
CREATE TABLE `task` (
`id` int(11) NOT NULL auto_increment,
`type` varchar(255) NOT NULL,
`role` enum('primary','secondary','tertiary') NOT NULL default 'primary',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `task_role`;
CREATE TABLE `task_role` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) character set latin1 NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
});
my $source_schema = get_schema(q{
CREATE TABLE `task` (
`id` integer NOT NULL auto_increment,
`role` varchar(255) NOT NULL,
INDEX `task_idx_role` (`role`),
PRIMARY KEY (`id`),
CONSTRAINT `task_fk_role` FOREIGN KEY (`role`) REFERENCES `task_role` (`name`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8;
CREATE TABLE `task_role` (
`id` integer NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8;
});
my $diff = SQL::Translator::Diff::schema_diff(
$dest_schema, 'MySQL', $source_schema, 'MySQL');
print $diff;
sub get_schema {
my $data = shift;
my $t = SQL::Translator->new();
$t->parser('MySQL');
my $out = $t->translate(\$data)
or die $t->error;
return $t->schema();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment