8 use File::Spec::Functions qw(catfile updir tmpdir);
11 use Test::Differences;
15 use_ok('SQL::Translator::Diff') or die "Cannot continue\n";
17 my $tr = SQL::Translator->new;
19 my ( $source_schema, $target_schema ) = map {
20 my $t = SQL::Translator->new;
23 my $out = $t->translate( catfile($Bin, qw/data diff /, $_ ) )
26 my $schema = $t->schema;
27 unless ( $schema->name ) {
31 } (qw/create1.yml create2.yml/);
33 # Test for differences
34 my $diff = SQL::Translator::Diff->new({
35 source_schema => $source_schema,
37 target_schema => $target_schema,
39 })->compute_differences;
41 my $diff_hash = make_diff_hash();
43 eq_or_diff($diff_hash->{employee},
45 constraints_to_create => [ 'FK5302D47D93FE702E_diff' ],
46 constraints_to_drop => [ 'FK5302D47D93FE702E' ],
47 fields_to_drop => [ 'job_title' ]
49 "Correct differences correct on employee table");
51 eq_or_diff($diff_hash->{person},
53 constraints_to_create => [ 'UC_person_id', 'UC_age_name' ],
54 constraints_to_drop => [ 'UC_age_name' ],
56 'person_id person_id',
61 fields_to_create => [ 'is_rock_star' ],
62 fields_to_rename => [ 'description physical_description' ],
63 indexes_to_create => [ 'unique_name' ],
64 indexes_to_drop => [ 'u_name' ],
65 table_options => [ 'person' ],
67 "Correct differences correct on person table");
70 [ map { $_->name } @{$diff->tables_to_drop} ],
72 "tables_to_drop right"
76 [ map { $_->name } @{$diff->tables_to_create} ],
78 "tables_to_create right"
82 $diff = SQL::Translator::Diff->new({
83 source_schema => $source_schema,
85 target_schema => $target_schema,
87 ignore_index_names => 1,
88 ignore_constraint_names => 1,
89 })->compute_differences;
91 $diff_hash = make_diff_hash();
93 eq_or_diff($diff_hash->{employee},
95 fields_to_drop => [ 'job_title' ]
97 "Correct differences correct on employee table");
99 eq_or_diff($diff_hash->{person},
101 constraints_to_create => [ 'UC_person_id', 'UC_age_name' ],
102 constraints_to_drop => [ 'UC_age_name' ],
104 'person_id person_id',
109 fields_to_create => [ 'is_rock_star' ],
110 fields_to_rename => [ 'description physical_description' ],
111 table_options => [ 'person' ],
113 "Correct differences correct on person table");
117 $diff = SQL::Translator::Diff->new({
118 source_schema => $source_schema,
119 source_db => 'MySQL',
120 target_schema => $source_schema,
121 target_db => 'MySQL',
122 })->compute_differences;
124 $diff_hash = make_diff_hash();
126 eq_or_diff($diff_hash, {}, "No differences");
128 is( @{$diff->tables_to_drop}, 0, "tables_to_drop right");
129 is( @{$diff->tables_to_create}, 0, "tables_to_create right");
132 # Turn table_diff_hash into something we can eq_or_diff better
138 my $table_diff = $diff->table_diff_hash->{$table};
143 my $opt = $table_diff->{$_};
144 @$opt ? ( $_ => [ map {
145 (ref $_||'') eq 'ARRAY' ? "@$_" :
154 %table_diffs ? ( $table => \%table_diffs ) : ();
155 } keys %{ $diff->table_diff_hash }