Commit | Line | Data |
da5a1bae |
1 | #!/usr/bin/perl |
2 | # vim: set ft=perl: |
3 | |
4 | use strict; |
4d438549 |
5 | use warnings; |
6 | use SQL::Translator; |
da5a1bae |
7 | |
8 | use File::Spec::Functions qw(catfile updir tmpdir); |
9 | use FindBin qw($Bin); |
10 | use Test::More; |
4d438549 |
11 | use Test::Differences; |
da5a1bae |
12 | |
4d438549 |
13 | plan tests => 10; |
da5a1bae |
14 | |
4d438549 |
15 | use_ok('SQL::Translator::Diff') or die "Cannot continue\n"; |
da5a1bae |
16 | |
4d438549 |
17 | my $tr = SQL::Translator->new; |
da5a1bae |
18 | |
4d438549 |
19 | my ( $source_schema, $target_schema ) = map { |
20 | my $t = SQL::Translator->new; |
21 | $t->parser( 'YAML' ) |
22 | or die $tr->error; |
23 | my $out = $t->translate( catfile($Bin, qw/data diff /, $_ ) ) |
24 | or die $tr->error; |
aee4b66e |
25 | |
4d438549 |
26 | my $schema = $t->schema; |
27 | unless ( $schema->name ) { |
28 | $schema->name( $_ ); |
29 | } |
30 | ($schema); |
31 | } (qw/create1.yml create2.yml/); |
da5a1bae |
32 | |
33 | # Test for differences |
4d438549 |
34 | my $diff = SQL::Translator::Diff->new({ |
35 | source_schema => $source_schema, |
36 | source_db => 'MySQL', |
37 | target_schema => $target_schema, |
38 | target_db => 'MySQL', |
39 | })->compute_differences; |
40 | |
41 | my $diff_hash = make_diff_hash(); |
42 | |
43 | eq_or_diff($diff_hash->{employee}, |
aee4b66e |
44 | { |
4d438549 |
45 | constraints_to_create => [ 'FK5302D47D93FE702E_diff' ], |
46 | constraints_to_drop => [ 'FK5302D47D93FE702E' ], |
47 | fields_to_drop => [ 'job_title' ] |
48 | }, |
49 | "Correct differences correct on employee table"); |
50 | |
51 | eq_or_diff($diff_hash->{person}, |
52 | { |
53 | constraints_to_create => [ 'UC_person_id', 'UC_age_name' ], |
54 | constraints_to_drop => [ 'UC_age_name' ], |
aee4b66e |
55 | fields_to_alter => [ |
4d438549 |
56 | 'person_id person_id', |
57 | 'name name', |
58 | 'age age', |
59 | 'iq iq', |
60 | ], |
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' ], |
66 | }, |
67 | "Correct differences correct on person table"); |
68 | |
69 | eq_or_diff( |
70 | [ map { $_->name } @{$diff->tables_to_drop} ], |
71 | [ "deleted" ], |
72 | "tables_to_drop right" |
73 | ); |
74 | |
75 | eq_or_diff( |
76 | [ map { $_->name } @{$diff->tables_to_create} ], |
77 | [ "added" ], |
78 | "tables_to_create right" |
79 | ); |
80 | |
81 | |
82 | $diff = SQL::Translator::Diff->new({ |
83 | source_schema => $source_schema, |
84 | source_db => 'MySQL', |
85 | target_schema => $target_schema, |
86 | target_db => 'MySQL', |
87 | ignore_index_names => 1, |
88 | ignore_constraint_names => 1, |
89 | })->compute_differences; |
90 | |
91 | $diff_hash = make_diff_hash(); |
92 | |
93 | eq_or_diff($diff_hash->{employee}, |
aee4b66e |
94 | { |
4d438549 |
95 | fields_to_drop => [ 'job_title' ] |
96 | }, |
97 | "Correct differences correct on employee table"); |
98 | |
99 | eq_or_diff($diff_hash->{person}, |
100 | { |
101 | constraints_to_create => [ 'UC_person_id', 'UC_age_name' ], |
102 | constraints_to_drop => [ 'UC_age_name' ], |
aee4b66e |
103 | fields_to_alter => [ |
4d438549 |
104 | 'person_id person_id', |
105 | 'name name', |
106 | 'age age', |
107 | 'iq iq', |
108 | ], |
109 | fields_to_create => [ 'is_rock_star' ], |
110 | fields_to_rename => [ 'description physical_description' ], |
111 | table_options => [ 'person' ], |
112 | }, |
113 | "Correct differences correct on person table"); |
da5a1bae |
114 | |
da5a1bae |
115 | |
4d438549 |
116 | # Test for sameness |
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; |
123 | |
124 | $diff_hash = make_diff_hash(); |
125 | |
126 | eq_or_diff($diff_hash, {}, "No differences"); |
127 | |
128 | is( @{$diff->tables_to_drop}, 0, "tables_to_drop right"); |
129 | is( @{$diff->tables_to_create}, 0, "tables_to_create right"); |
130 | |
131 | |
132 | # Turn table_diff_hash into something we can eq_or_diff better |
133 | sub make_diff_hash { |
134 | |
135 | return { |
136 | map { |
137 | my $table = $_; |
138 | my $table_diff = $diff->table_diff_hash->{$table}; |
139 | |
140 | my %table_diffs = ( |
141 | map { |
aee4b66e |
142 | |
4d438549 |
143 | my $opt = $table_diff->{$_}; |
144 | @$opt ? ( $_ => [ map { |
145 | (ref $_||'') eq 'ARRAY' ? "@$_" : |
146 | (ref $_) ? $_->name |
147 | : "$_"; |
aee4b66e |
148 | } @$opt |
149 | ] ) |
4d438549 |
150 | : () |
151 | } keys %$table_diff |
152 | ); |
153 | |
154 | %table_diffs ? ( $table => \%table_diffs ) : (); |
155 | } keys %{ $diff->table_diff_hash } |
156 | }; |
da5a1bae |
157 | |
4d438549 |
158 | } |