Fix PostgreSQL grammar parsing of UUID, time, timetz column types
[dbsrgits/SQL-Translator.git] / t / 30sqlt-new-diff.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
13 plan tests => 10;
14
15 use_ok('SQL::Translator::Diff') or die "Cannot continue\n";
16
17 my $tr            = SQL::Translator->new;
18
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;
25
26     my $schema = $t->schema;
27     unless ( $schema->name ) {
28         $schema->name( $_ );
29     }
30     ($schema);
31 } (qw/create1.yml create2.yml/);
32
33 # Test for differences
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},
44   {
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' ],
55     fields_to_alter => [
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},
94   {
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' ],
103     fields_to_alter => [
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");
114
115
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 {
142
143           my $opt = $table_diff->{$_};
144           @$opt ? ( $_ => [ map {
145                         (ref $_||'') eq 'ARRAY' ? "@$_" :
146                         (ref $_)                ? $_->name
147                                                 : "$_";
148                       } @$opt
149                     ] )
150                 : ()
151         } keys %$table_diff
152       );
153
154       %table_diffs ? ( $table => \%table_diffs ) : ();
155     } keys %{ $diff->table_diff_hash }
156   };
157
158 }