Commit | Line | Data |
4f3b7c4e |
1 | use strict; |
2 | |
3 | use FindBin qw/$Bin/; |
4 | |
5 | use Test::More; |
6 | #use Test::SQL::Translator; |
7 | use Test::Exception; |
8 | use Data::Dumper; |
9 | use SQL::Translator; |
10 | use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants); |
11 | use Test::SQL::Translator qw(schema_ok); |
12 | |
13 | # Simple options. -d for debug |
14 | my %opt; |
15 | BEGIN { map { $opt{$_}=1 if s/^-// } @ARGV; } |
16 | use constant DEBUG => (exists $opt{d} ? 1 : 0); |
17 | |
18 | |
19 | # Testing 1,2,3,4... |
20 | #============================================================================= |
21 | |
22 | #BEGIN { |
23 | # maybe_plan(212, 'SQL::Translator::Parser::XML::SQLFairy'); |
24 | #} |
25 | |
26 | my $testschema = "$Bin/data/xml/schema.xml"; |
27 | open (my $testschema_fh, '<', $testschema) or die "$testschema: $!"; |
28 | |
29 | my $sqlt; |
30 | $sqlt = SQL::Translator->new({ |
31 | debug => DEBUG, |
32 | show_warnings => 1, |
33 | add_drop_table => 1, |
34 | }); |
35 | die "Can't find test schema $testschema" unless -e $testschema; |
36 | |
37 | my $sql; |
38 | { |
39 | my @w; |
40 | local $SIG{__WARN__} = sub { push @w, $_[0] if $_[0] =~ /The database_event tag is deprecated - please use database_events/ }; |
41 | |
42 | $sql = $sqlt->translate( |
43 | parser => 'XML', |
44 | producer => 'MySQL', |
45 | data => do { local $/; <$testschema_fh> }, |
46 | ) or die $sqlt->error; |
47 | print $sql if DEBUG; |
48 | |
49 | ok (@w, 'database_event deprecation warning issued'); |
50 | } |
51 | |
52 | # Test the schema objs generted from the XML |
53 | # |
54 | my $scma = $sqlt->schema; |
55 | |
56 | # Hmmm, when using schema_ok the field test data gets a bit too nested and |
57 | # fiddly to work with. (See 28xml-xmi-parser-sqlfairy.t for more a split out |
58 | # version) |
59 | schema_ok( $scma, { |
60 | tables => [ |
61 | { |
62 | name => "Basic", |
63 | options => [ { ENGINE => 'InnoDB' } ], |
64 | extra => { |
65 | foo => "bar", |
66 | hello => "world", |
67 | bar => "baz", |
68 | }, |
69 | fields => [ |
70 | { |
71 | name => "id", |
72 | data_type => "int", |
73 | default_value => undef, |
74 | is_nullable => 0, |
75 | size => 10, |
76 | is_primary_key => 1, |
77 | is_auto_increment => 1, |
78 | extra => { ZEROFILL => 1 }, |
79 | }, |
80 | { |
81 | name => "title", |
82 | data_type => "varchar", |
83 | is_nullable => 0, |
84 | default_value => "hello", |
85 | size => 100, |
86 | }, |
87 | { |
88 | name => "description", |
89 | data_type => "text", |
90 | is_nullable => 1, |
91 | default_value => "", |
92 | }, |
93 | { |
94 | name => "email", |
95 | data_type => "varchar", |
96 | size => 500, |
97 | is_unique => 1, |
98 | default_value => undef, |
99 | is_nullable => 1, |
100 | extra => { |
101 | foo => "bar", |
102 | hello => "world", |
103 | bar => "baz", |
104 | } |
105 | }, |
106 | { |
107 | name => "explicitnulldef", |
108 | data_type => "varchar", |
109 | default_value => undef, |
110 | is_nullable => 1, |
111 | size => 255, |
112 | }, |
113 | { |
114 | name => "explicitemptystring", |
115 | data_type => "varchar", |
116 | default_value => "", |
117 | is_nullable => 1, |
118 | size => 255, |
119 | }, |
120 | { |
121 | name => "emptytagdef", |
122 | data_type => "varchar", |
123 | default_value => "", |
124 | is_nullable => 1, |
125 | comments => "Hello emptytagdef", |
126 | size => 255, |
127 | }, |
128 | { |
129 | name => "another_id", |
130 | data_type => "int", |
131 | size => "10", |
132 | default_value => 2, |
133 | is_nullable => 1, |
134 | is_foreign_key => 1, |
135 | }, |
136 | { |
137 | name => "timest", |
138 | data_type => "timestamp", |
139 | size => "0", |
140 | is_nullable => 1, |
141 | }, |
142 | ], |
143 | constraints => [ |
144 | { |
145 | type => PRIMARY_KEY, |
146 | fields => ["id"], |
147 | extra => { |
148 | foo => "bar", |
149 | hello => "world", |
150 | bar => "baz", |
151 | }, |
152 | }, |
153 | { |
154 | name => 'emailuniqueindex', |
155 | type => UNIQUE, |
156 | fields => ["email"], |
157 | }, |
158 | { |
159 | type => FOREIGN_KEY, |
160 | fields => ["another_id"], |
161 | reference_table => "Another", |
162 | reference_fields => ["id"], |
163 | name => 'Basic_fk' |
164 | }, |
165 | ], |
166 | indices => [ |
167 | { |
168 | name => "titleindex", |
169 | fields => ["title"], |
170 | extra => { |
171 | foo => "bar", |
172 | hello => "world", |
173 | bar => "baz", |
174 | }, |
175 | }, |
176 | ], |
177 | }, # end table Basic |
178 | { |
179 | name => "Another", |
180 | extra => { |
181 | foo => "bar", |
182 | hello => "world", |
183 | bar => "baz", |
184 | }, |
185 | options => [ { ENGINE => 'InnoDB' } ], |
186 | fields => [ |
187 | { |
188 | name => "id", |
189 | data_type => "int", |
190 | default_value => undef, |
191 | is_nullable => 0, |
192 | size => 10, |
193 | is_primary_key => 1, |
194 | is_auto_increment => 1, |
195 | }, |
196 | ], |
197 | }, # end table Another |
198 | ], # end tables |
199 | |
200 | views => [ |
201 | { |
202 | name => 'email_list', |
203 | sql => "SELECT email FROM Basic WHERE (email IS NOT NULL)", |
204 | fields => ['email'], |
205 | extra => { |
206 | foo => "bar", |
207 | hello => "world", |
208 | bar => "baz", |
209 | }, |
210 | }, |
211 | ], |
212 | |
213 | triggers => [ |
214 | { |
215 | name => 'foo_trigger', |
216 | perform_action_when => 'after', |
217 | database_events => 'insert', |
218 | on_table => 'Basic', |
219 | action => 'update modified=timestamp();', |
220 | extra => { |
221 | foo => "bar", |
222 | hello => "world", |
223 | bar => "baz", |
224 | }, |
225 | }, |
226 | { |
227 | name => 'bar_trigger', |
228 | perform_action_when => 'before', |
229 | database_events => 'insert,update', |
230 | on_table => 'Basic', |
231 | action => 'update modified2=timestamp();', |
232 | extra => { |
233 | hello => "aliens", |
234 | }, |
235 | }, |
236 | ], |
237 | |
238 | procedures => [ |
239 | { |
240 | name => 'foo_proc', |
241 | sql => 'select foo from bar', |
242 | parameters => ['foo', 'bar'], |
243 | owner => 'Nomar', |
244 | comments => 'Go Sox!', |
245 | extra => { |
246 | foo => "bar", |
247 | hello => "world", |
248 | bar => "baz", |
249 | }, |
250 | }, |
251 | ], |
252 | |
253 | }); # end schema |
254 | |
255 | done_testing; |