MAss diff changes imported from Ash's local diff-refactor branch
[dbsrgits/SQL-Translator.git] / t / 30sqlt-new-diff-mysql.t
1 #!/usr/bin/perl
2 # vim: set ft=perl:
3
4 use strict;
5 use warnings;
6 use SQL::Translator;
7
8 use File::Spec::Functions qw(catfile updir tmpdir);
9 use FindBin qw($Bin);
10 use Test::More;
11 use Test::Differences;
12 use Test::SQL::Translator qw(maybe_plan);
13
14 plan tests => 4;
15
16 use_ok('SQL::Translator::Diff') or die "Cannot continue\n";
17
18 my $tr            = SQL::Translator->new;
19
20 my ( $source_schema, $target_schema ) = map {
21     my $t = SQL::Translator->new;
22     $t->parser( 'YAML' )
23       or die $tr->error;
24     my $out = $t->translate( catfile($Bin, qw/data diff/, $_ ) )
25       or die $tr->error;
26     
27     my $schema = $t->schema;
28     unless ( $schema->name ) {
29         $schema->name( $_ );
30     }
31     ($schema);
32 } (qw/create1.yml create2.yml/);
33
34 # Test for differences
35 my $out = SQL::Translator::Diff::schema_diff( $source_schema, 'MySQL', $target_schema, 'MySQL', { no_batch_alters => 1} );
36 eq_or_diff($out, <<'## END OF DIFF', "Diff as expected");
37 -- Convert schema 'create1.yml' to 'create2.yml':
38
39 BEGIN TRANSACTION;
40
41 SET foreign_key_checks=0;
42
43
44 CREATE TABLE added (
45   id integer(11)
46 );
47
48
49 SET foreign_key_checks=1;
50
51
52 ALTER TABLE employee DROP FOREIGN KEY FK5302D47D93FE702E;
53 ALTER TABLE person DROP UNIQUE UC_age_name;
54 ALTER TABLE person DROP INDEX u_name;
55 ALTER TABLE employee DROP COLUMN job_title;
56 ALTER TABLE person ADD COLUMN is_rock_star tinyint(4) DEFAULT '1';
57 ALTER TABLE person CHANGE COLUMN person_id person_id integer(11) NOT NULL auto_increment;
58 ALTER TABLE person CHANGE COLUMN name name varchar(20) NOT NULL;
59 ALTER TABLE person CHANGE COLUMN age age integer(11) DEFAULT '18';
60 ALTER TABLE person CHANGE COLUMN iq iq integer(11) DEFAULT '0';
61 ALTER TABLE person CHANGE COLUMN description physical_description text;
62 ALTER TABLE person ADD UNIQUE INDEX unique_name (name);
63 ALTER TABLE employee ADD CONSTRAINT FK5302D47D93FE702E_diff FOREIGN KEY (employee_id) REFERENCES person (person_id);
64 ALTER TABLE person ADD UNIQUE UC_person_id (person_id);
65 ALTER TABLE person ADD UNIQUE UC_age_name (age, name);
66 ALTER TABLE person ENGINE=InnoDB;
67 ALTER TABLE deleted DROP FOREIGN KEY fk_fake;
68 DROP TABLE deleted;
69
70 COMMIT;
71 ## END OF DIFF
72
73 #die $out;
74
75 $out = SQL::Translator::Diff::schema_diff($source_schema, 'MySQL', $target_schema, 'MySQL',
76     { ignore_index_names => 1,
77       ignore_constraint_names => 1
78     });
79
80 eq_or_diff($out, <<'## END OF DIFF', "Diff as expected");
81 -- Convert schema 'create1.yml' to 'create2.yml':
82
83 BEGIN TRANSACTION;
84
85 SET foreign_key_checks=0;
86
87
88 CREATE TABLE added (
89   id integer(11)
90 );
91
92
93 SET foreign_key_checks=1;
94
95
96 ALTER TABLE employee DROP COLUMN job_title;
97 ALTER TABLE person DROP UNIQUE UC_age_name,
98                    ADD COLUMN is_rock_star tinyint(4) DEFAULT '1',
99                    CHANGE COLUMN person_id person_id integer(11) NOT NULL auto_increment,
100                    CHANGE COLUMN name name varchar(20) NOT NULL,
101                    CHANGE COLUMN age age integer(11) DEFAULT '18',
102                    CHANGE COLUMN iq iq integer(11) DEFAULT '0',
103                    CHANGE COLUMN description physical_description text,
104                    ADD UNIQUE UC_person_id (person_id),
105                    ADD UNIQUE UC_age_name (age, name),
106                    ENGINE=InnoDB;
107 ALTER TABLE deleted DROP FOREIGN KEY fk_fake;
108 DROP TABLE deleted;
109
110 COMMIT;
111 ## END OF DIFF
112
113
114 # Test for sameness
115 $out = SQL::Translator::Diff::schema_diff($source_schema, 'MySQL', $source_schema, 'MySQL' );
116
117 eq_or_diff($out, <<'## END OF DIFF', "No differences found");
118 -- Convert schema 'create1.yml' to 'create1.yml':
119
120 -- No differences found
121
122 ## END OF DIFF
123
124 =cut