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