Commit | Line | Data |
0494e672 |
1 | #!/usr/bin/perl |
2 | # vim: set ft=perl: |
3 | # |
0494e672 |
4 | |
b8661f30 |
5 | use strict; |
44fb27ae |
6 | |
fbc0552f |
7 | use Test::More; |
b8661f30 |
8 | use SQL::Translator; |
2ab84c11 |
9 | use SQL::Translator::Schema::Constants; |
2d691ec1 |
10 | use Test::SQL::Translator qw(maybe_plan); |
0494e672 |
11 | |
2d691ec1 |
12 | BEGIN { |
2e8b8523 |
13 | maybe_plan(218, "SQL::Translator::Parser::MySQL"); |
fbc0552f |
14 | SQL::Translator::Parser::MySQL->import('parse'); |
fbc0552f |
15 | } |
16 | |
251b6ff5 |
17 | { |
18 | my $tr = SQL::Translator->new; |
19 | my $data = q|create table sessions ( |
2ab84c11 |
20 | id char(32) not null default '0' primary key, |
2e8b8523 |
21 | a_session text, |
22 | ssn varchar(12) unique key, |
23 | age int key |
251b6ff5 |
24 | );|; |
25 | |
26 | my $val = parse($tr, $data); |
2ab84c11 |
27 | my $schema = $tr->schema; |
e3252059 |
28 | is( $schema->is_valid, 1, 'Schema is valid' ); |
2ab84c11 |
29 | my @tables = $schema->get_tables; |
30 | is( scalar @tables, 1, 'Right number of tables (1)' ); |
31 | my $table = shift @tables; |
32 | is( $table->name, 'sessions', 'Found "sessions" table' ); |
251b6ff5 |
33 | |
2ab84c11 |
34 | my @fields = $table->get_fields; |
2e8b8523 |
35 | is( scalar @fields, 4, 'Right number of fields (4)' ); |
2ab84c11 |
36 | my $f1 = shift @fields; |
37 | my $f2 = shift @fields; |
38 | is( $f1->name, 'id', 'First field name is "id"' ); |
39 | is( $f1->data_type, 'char', 'Type is "char"' ); |
40 | is( $f1->size, 32, 'Size is "32"' ); |
41 | is( $f1->is_nullable, 0, 'Field cannot be null' ); |
42 | is( $f1->default_value, '0', 'Default value is "0"' ); |
43 | is( $f1->is_primary_key, 1, 'Field is PK' ); |
251b6ff5 |
44 | |
2ab84c11 |
45 | is( $f2->name, 'a_session', 'Second field name is "a_session"' ); |
46 | is( $f2->data_type, 'text', 'Type is "text"' ); |
0c1c8d2a |
47 | is( $f2->size, 65_535, 'Size is "65,535"' ); |
2ab84c11 |
48 | is( $f2->is_nullable, 1, 'Field can be null' ); |
49 | is( $f2->default_value, undef, 'Default value is undefined' ); |
50 | is( $f2->is_primary_key, 0, 'Field is not PK' ); |
251b6ff5 |
51 | |
2ab84c11 |
52 | my @indices = $table->get_indices; |
2e8b8523 |
53 | is( scalar @indices, 1, 'Right number of indices (1)' ); |
251b6ff5 |
54 | |
2ab84c11 |
55 | my @constraints = $table->get_constraints; |
2e8b8523 |
56 | is( scalar @constraints, 2, 'Right number of constraints (2)' ); |
2ab84c11 |
57 | my $c = shift @constraints; |
58 | is( $c->type, PRIMARY_KEY, 'Constraint is a PK' ); |
59 | is( join(',', $c->fields), 'id', 'Constraint is on "id"' ); |
2e8b8523 |
60 | my $c2 = shift @constraints; |
61 | is( $c2->type, UNIQUE, 'Constraint is UNIQUE' ); |
62 | is( join(',', $c2->fields), 'ssn', 'Constraint is on "ssn"' ); |
251b6ff5 |
63 | } |
64 | |
65 | { |
66 | my $tr = SQL::Translator->new; |
67 | my $data = parse($tr, |
68 | q[ |
69 | CREATE TABLE check ( |
2ab84c11 |
70 | check_id int(7) unsigned zerofill NOT NULL default '0000000' |
251b6ff5 |
71 | auto_increment primary key, |
72 | successful date NOT NULL default '0000-00-00', |
73 | unsuccessful date default '0000-00-00', |
74 | i1 int(11) default '0' not null, |
75 | s1 set('a','b','c') default 'b', |
76 | e1 enum('a','b','c') default 'c', |
77 | name varchar(30) default NULL, |
78 | foo_type enum('vk','ck') NOT NULL default 'vk', |
79 | date timestamp, |
80 | time_stamp2 timestamp, |
81 | KEY (i1), |
82 | UNIQUE (date, i1), |
83 | KEY date_idx (date), |
84 | KEY name_idx (name(10)) |
85 | ) TYPE=MyISAM PACK_KEYS=1; |
86 | ] |
87 | ); |
88 | |
2ab84c11 |
89 | my $schema = $tr->schema; |
e3252059 |
90 | is( $schema->is_valid, 1, 'Schema is valid' ); |
2ab84c11 |
91 | my @tables = $schema->get_tables; |
92 | is( scalar @tables, 1, 'Right number of tables (1)' ); |
93 | my $table = shift @tables; |
94 | is( $table->name, 'check', 'Found "check" table' ); |
95 | |
96 | my @fields = $table->get_fields; |
97 | is( scalar @fields, 10, 'Right number of fields (10)' ); |
98 | my $f1 = shift @fields; |
99 | is( $f1->name, 'check_id', 'First field name is "check_id"' ); |
100 | is( $f1->data_type, 'int', 'Type is "int"' ); |
101 | is( $f1->size, 7, 'Size is "7"' ); |
102 | is( $f1->is_nullable, 0, 'Field cannot be null' ); |
103 | is( $f1->default_value, '0000000', 'Default value is "0000000"' ); |
104 | is( $f1->is_primary_key, 1, 'Field is PK' ); |
105 | is( $f1->is_auto_increment, 1, 'Field is auto inc' ); |
106 | my %extra = $f1->extra; |
107 | ok( defined $extra{'unsigned'}, 'Field is unsigned' ); |
108 | ok( defined $extra{'zerofill'}, 'Field is zerofill' ); |
109 | |
110 | my $f2 = shift @fields; |
111 | is( $f2->name, 'successful', 'Second field name is "successful"' ); |
112 | is( $f2->data_type, 'date', 'Type is "date"' ); |
113 | is( $f2->size, 0, 'Size is "0"' ); |
114 | is( $f2->is_nullable, 0, 'Field cannot be null' ); |
115 | is( $f2->default_value, '0000-00-00', 'Default value is "0000-00-00"' ); |
116 | is( $f2->is_primary_key, 0, 'Field is not PK' ); |
117 | |
118 | my $f3 = shift @fields; |
119 | is( $f3->name, 'unsuccessful', 'Third field name is "unsuccessful"' ); |
120 | is( $f3->data_type, 'date', 'Type is "date"' ); |
121 | is( $f3->size, 0, 'Size is "0"' ); |
122 | is( $f3->is_nullable, 1, 'Field can be null' ); |
123 | is( $f3->default_value, '0000-00-00', 'Default value is "0000-00-00"' ); |
124 | is( $f3->is_primary_key, 0, 'Field is not PK' ); |
125 | |
126 | my $f4 = shift @fields; |
127 | is( $f4->name, 'i1', 'Fourth field name is "i1"' ); |
128 | is( $f4->data_type, 'int', 'Type is "int"' ); |
129 | is( $f4->size, 11, 'Size is "11"' ); |
130 | is( $f4->is_nullable, 0, 'Field cannot be null' ); |
131 | is( $f4->default_value, '0', 'Default value is "0"' ); |
132 | is( $f4->is_primary_key, 0, 'Field is not PK' ); |
3a0b6f0a |
133 | |
2ab84c11 |
134 | my $f5 = shift @fields; |
135 | is( $f5->name, 's1', 'Fifth field name is "s1"' ); |
136 | is( $f5->data_type, 'set', 'Type is "set"' ); |
137 | is( $f5->size, 1, 'Size is "1"' ); |
138 | is( $f5->is_nullable, 1, 'Field can be null' ); |
139 | is( $f5->default_value, 'b', 'Default value is "b"' ); |
140 | is( $f5->is_primary_key, 0, 'Field is not PK' ); |
141 | my %f5extra = $f5->extra; |
142 | is( join(',', @{ $f5extra{'list'} || [] }), 'a,b,c', 'List is "a,b,c"' ); |
3a0b6f0a |
143 | |
2ab84c11 |
144 | my $f6 = shift @fields; |
145 | is( $f6->name, 'e1', 'Sixth field name is "e1"' ); |
146 | is( $f6->data_type, 'enum', 'Type is "enum"' ); |
147 | is( $f6->size, 1, 'Size is "1"' ); |
148 | is( $f6->is_nullable, 1, 'Field can be null' ); |
149 | is( $f6->default_value, 'c', 'Default value is "c"' ); |
150 | is( $f6->is_primary_key, 0, 'Field is not PK' ); |
151 | my %f6extra = $f6->extra; |
152 | is( join(',', @{ $f6extra{'list'} || [] }), 'a,b,c', 'List is "a,b,c"' ); |
3a0b6f0a |
153 | |
2ab84c11 |
154 | my $f7 = shift @fields; |
155 | is( $f7->name, 'name', 'Seventh field name is "name"' ); |
156 | is( $f7->data_type, 'varchar', 'Type is "varchar"' ); |
157 | is( $f7->size, 30, 'Size is "30"' ); |
158 | is( $f7->is_nullable, 1, 'Field can be null' ); |
159 | is( $f7->default_value, 'NULL', 'Default value is "NULL"' ); |
160 | is( $f7->is_primary_key, 0, 'Field is not PK' ); |
3a0b6f0a |
161 | |
2ab84c11 |
162 | my $f8 = shift @fields; |
163 | is( $f8->name, 'foo_type', 'Eighth field name is "foo_type"' ); |
164 | is( $f8->data_type, 'enum', 'Type is "enum"' ); |
165 | is( $f8->size, 2, 'Size is "2"' ); |
166 | is( $f8->is_nullable, 0, 'Field cannot be null' ); |
167 | is( $f8->default_value, 'vk', 'Default value is "vk"' ); |
168 | is( $f8->is_primary_key, 0, 'Field is not PK' ); |
169 | my %f8extra = $f8->extra; |
170 | is( join(',', @{ $f8extra{'list'} || [] }), 'vk,ck', 'List is "vk,ck"' ); |
171 | |
172 | my $f9 = shift @fields; |
173 | is( $f9->name, 'date', 'Ninth field name is "date"' ); |
174 | is( $f9->data_type, 'timestamp', 'Type is "timestamp"' ); |
175 | is( $f9->size, 0, 'Size is "0"' ); |
176 | is( $f9->is_nullable, 1, 'Field can be null' ); |
177 | is( $f9->default_value, undef, 'Default value is undefined' ); |
178 | is( $f9->is_primary_key, 0, 'Field is not PK' ); |
179 | |
180 | my $f10 = shift @fields; |
181 | is( $f10->name, 'time_stamp2', 'Tenth field name is "time_stamp2"' ); |
182 | is( $f10->data_type, 'timestamp', 'Type is "timestamp"' ); |
183 | is( $f10->size, 0, 'Size is "0"' ); |
184 | is( $f10->is_nullable, 1, 'Field can be null' ); |
185 | is( $f10->default_value, undef, 'Default value is undefined' ); |
186 | is( $f10->is_primary_key, 0, 'Field is not PK' ); |
187 | |
188 | my @indices = $table->get_indices; |
189 | is( scalar @indices, 3, 'Right number of indices (3)' ); |
190 | |
191 | my $i1 = shift @indices; |
192 | is( $i1->name, '', 'No name on index' ); |
193 | is( $i1->type, NORMAL, 'Normal index' ); |
194 | is( join(',', $i1->fields ), 'i1', 'Index is on field "i1"' ); |
195 | |
196 | my $i2 = shift @indices; |
197 | is( $i2->name, 'date_idx', 'Name is "date_idx"' ); |
198 | is( $i2->type, NORMAL, 'Normal index' ); |
199 | is( join(',', $i2->fields ), 'date', 'Index is on field "date"' ); |
200 | |
201 | my $i3 = shift @indices; |
202 | is( $i3->name, 'name_idx', 'Name is "name_idx"' ); |
203 | is( $i3->type, NORMAL, 'Normal index' ); |
204 | is( join(',', $i3->fields ), 'name(10)', 'Index is on field "name(10)"' ); |
205 | |
206 | my @constraints = $table->get_constraints; |
207 | is( scalar @constraints, 2, 'Right number of constraints (2)' ); |
208 | |
209 | my $c1 = shift @constraints; |
210 | is( $c1->type, PRIMARY_KEY, 'Constraint is a PK' ); |
211 | is( join(',', $c1->fields), 'check_id', 'Constraint is on "check_id"' ); |
212 | |
213 | my $c2 = shift @constraints; |
214 | is( $c2->type, UNIQUE, 'Constraint is UNIQUE' ); |
215 | is( join(',', $c2->fields), 'date,i1', 'Constraint is on "date, i1"' ); |
251b6ff5 |
216 | } |
217 | |
218 | { |
219 | my $tr = SQL::Translator->new; |
220 | my $data = parse($tr, |
221 | q[ |
222 | CREATE TABLE orders ( |
3a0b6f0a |
223 | order_id integer NOT NULL auto_increment, |
85a6c976 |
224 | member_id varchar(255) comment 'fk to member', |
251b6ff5 |
225 | billing_address_id int, |
226 | shipping_address_id int, |
227 | credit_card_id int, |
228 | status smallint NOT NULL, |
229 | store_id varchar(255) NOT NULL REFERENCES store, |
230 | tax decimal(8,2), |
231 | shipping_charge decimal(8,2), |
232 | price_paid decimal(8,2), |
233 | PRIMARY KEY (order_id), |
234 | KEY (status), |
235 | KEY (billing_address_id), |
236 | KEY (shipping_address_id), |
237 | KEY (member_id, store_id), |
556d0509 |
238 | FOREIGN KEY (status) REFERENCES order_status(id) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE, |
251b6ff5 |
239 | FOREIGN KEY (billing_address_id) REFERENCES address(address_id), |
240 | FOREIGN KEY (shipping_address_id) REFERENCES address(address_id) |
85a6c976 |
241 | ) TYPE=INNODB COMMENT = 'orders table comment'; |
2ab84c11 |
242 | |
243 | CREATE TABLE address ( |
244 | address_id int NOT NULL auto_increment, |
245 | recipient varchar(255) NOT NULL, |
246 | address1 varchar(255) NOT NULL, |
247 | address2 varchar(255), |
248 | city varchar(255) NOT NULL, |
249 | state varchar(255) NOT NULL, |
250 | postal_code varchar(255) NOT NULL, |
251 | phone varchar(255), |
252 | PRIMARY KEY (address_id) |
253 | ) TYPE=INNODB; |
251b6ff5 |
254 | ] |
255 | ) or die $tr->error; |
256 | |
2ab84c11 |
257 | my $schema = $tr->schema; |
e3252059 |
258 | is( $schema->is_valid, 1, 'Schema is valid' ); |
2ab84c11 |
259 | my @tables = $schema->get_tables; |
260 | is( scalar @tables, 2, 'Right number of tables (2)' ); |
261 | |
262 | my $t1 = shift @tables; |
263 | is( $t1->name, 'orders', 'Found "orders" table' ); |
85a6c976 |
264 | is( $t1->comments, 'orders table comment', 'Table comment OK' ); |
2ab84c11 |
265 | |
266 | my @fields = $t1->get_fields; |
267 | is( scalar @fields, 10, 'Right number of fields (10)' ); |
268 | |
269 | my $f1 = shift @fields; |
270 | is( $f1->name, 'order_id', 'First field name is "order_id"' ); |
271 | is( $f1->data_type, 'int', 'Type is "int"' ); |
272 | is( $f1->size, 11, 'Size is "11"' ); |
273 | is( $f1->is_nullable, 0, 'Field cannot be null' ); |
274 | is( $f1->default_value, undef, 'Default value is undefined' ); |
275 | is( $f1->is_primary_key, 1, 'Field is PK' ); |
276 | is( $f1->is_auto_increment, 1, 'Field is auto inc' ); |
277 | |
278 | my $f2 = shift @fields; |
279 | is( $f2->name, 'member_id', 'Second field name is "member_id"' ); |
280 | is( $f2->data_type, 'varchar', 'Type is "varchar"' ); |
281 | is( $f2->size, 255, 'Size is "255"' ); |
282 | is( $f2->is_nullable, 1, 'Field can be null' ); |
85a6c976 |
283 | is( $f2->comments, 'fk to member', 'Field comment OK' ); |
2ab84c11 |
284 | is( $f2->default_value, undef, 'Default value is undefined' ); |
285 | |
286 | my $f3 = shift @fields; |
287 | is( $f3->name, 'billing_address_id', |
288 | 'Third field name is "billing_address_id"' ); |
289 | is( $f3->data_type, 'int', 'Type is "int"' ); |
290 | is( $f3->size, 11, 'Size is "11"' ); |
291 | |
292 | my $f4 = shift @fields; |
293 | is( $f4->name, 'shipping_address_id', |
294 | 'Fourth field name is "shipping_address_id"' ); |
295 | is( $f4->data_type, 'int', 'Type is "int"' ); |
296 | is( $f4->size, 11, 'Size is "11"' ); |
297 | |
298 | my $f5 = shift @fields; |
299 | is( $f5->name, 'credit_card_id', 'Fifth field name is "credit_card_id"' ); |
300 | is( $f5->data_type, 'int', 'Type is "int"' ); |
301 | is( $f5->size, 11, 'Size is "11"' ); |
302 | |
303 | my $f6 = shift @fields; |
304 | is( $f6->name, 'status', 'Sixth field name is "status"' ); |
305 | is( $f6->data_type, 'smallint', 'Type is "smallint"' ); |
306 | is( $f6->size, 6, 'Size is "6"' ); |
307 | is( $f6->is_nullable, 0, 'Field cannot be null' ); |
308 | |
309 | my $f7 = shift @fields; |
310 | is( $f7->name, 'store_id', 'Seventh field name is "store_id"' ); |
311 | is( $f7->data_type, 'varchar', 'Type is "varchar"' ); |
312 | is( $f7->size, 255, 'Size is "255"' ); |
313 | is( $f7->is_nullable, 0, 'Field cannot be null' ); |
314 | is( $f7->is_foreign_key, 1, 'Field is a FK' ); |
315 | my $fk_ref = $f7->foreign_key_reference; |
316 | isa_ok( $fk_ref, 'SQL::Translator::Schema::Constraint', 'FK' ); |
317 | is( $fk_ref->reference_table, 'store', 'FK is to "store" table' ); |
318 | |
319 | my $f8 = shift @fields; |
320 | is( $f8->name, 'tax', 'Eighth field name is "tax"' ); |
321 | is( $f8->data_type, 'decimal', 'Type is "decimal"' ); |
322 | is( $f8->size, '8,2', 'Size is "8,2"' ); |
323 | |
324 | my $f9 = shift @fields; |
325 | is( $f9->name, 'shipping_charge', 'Ninth field name is "shipping_charge"' ); |
326 | is( $f9->data_type, 'decimal', 'Type is "decimal"' ); |
327 | is( $f9->size, '8,2', 'Size is "8,2"' ); |
328 | |
329 | my $f10 = shift @fields; |
330 | is( $f10->name, 'price_paid', 'Tenth field name is "price_paid"' ); |
331 | is( $f10->data_type, 'decimal', 'Type is "decimal"' ); |
332 | is( $f10->size, '8,2', 'Size is "8,2"' ); |
333 | |
334 | my @indices = $t1->get_indices; |
335 | is( scalar @indices, 4, 'Right number of indices (4)' ); |
336 | |
337 | my $i1 = shift @indices; |
338 | is( $i1->type, NORMAL, 'First index is normal' ); |
339 | is( join(',', $i1->fields), 'status', 'Index is on "status"' ); |
340 | |
341 | my $i2 = shift @indices; |
342 | is( $i2->type, NORMAL, 'Second index is normal' ); |
343 | is( join(',', $i2->fields), 'billing_address_id', |
344 | 'Index is on "billing_address_id"' ); |
345 | |
346 | my $i3 = shift @indices; |
347 | is( $i3->type, NORMAL, 'Third index is normal' ); |
348 | is( join(',', $i3->fields), 'shipping_address_id', |
349 | 'Index is on "shipping_address_id"' ); |
350 | |
351 | my $i4 = shift @indices; |
352 | is( $i4->type, NORMAL, 'Third index is normal' ); |
353 | is( join(',', $i4->fields), 'member_id,store_id', |
354 | 'Index is on "member_id,store_id"' ); |
355 | |
356 | my @constraints = $t1->get_constraints; |
357 | is( scalar @constraints, 5, 'Right number of constraints (5)' ); |
358 | |
359 | my $c1 = shift @constraints; |
360 | is( $c1->type, PRIMARY_KEY, 'Constraint is a PK' ); |
361 | is( join(',', $c1->fields), 'order_id', 'Constraint is on "order_id"' ); |
362 | |
363 | my $c2 = shift @constraints; |
364 | is( $c2->type, FOREIGN_KEY, 'Constraint is a FK' ); |
365 | is( join(',', $c2->fields), 'status', 'Constraint is on "status"' ); |
366 | is( $c2->reference_table, 'order_status', 'To table "order_status"' ); |
367 | is( join(',', $c2->reference_fields), 'id', 'To field "id"' ); |
368 | |
369 | my $c3 = shift @constraints; |
370 | is( $c3->type, FOREIGN_KEY, 'Constraint is a FK' ); |
371 | is( join(',', $c3->fields), 'billing_address_id', |
372 | 'Constraint is on "billing_address_id"' ); |
373 | is( $c3->reference_table, 'address', 'To table "address"' ); |
374 | is( join(',', $c3->reference_fields), 'address_id', |
375 | 'To field "address_id"' ); |
376 | |
377 | my $c4 = shift @constraints; |
378 | is( $c4->type, FOREIGN_KEY, 'Constraint is a FK' ); |
379 | is( join(',', $c4->fields), 'shipping_address_id', |
380 | 'Constraint is on "shipping_address_id"' ); |
381 | is( $c4->reference_table, 'address', 'To table "address"' ); |
382 | is( join(',', $c4->reference_fields), 'address_id', |
383 | 'To field "address_id"' ); |
384 | |
385 | my $c5 = shift @constraints; |
386 | is( $c5->type, FOREIGN_KEY, 'Constraint is a FK' ); |
387 | is( join(',', $c5->fields), 'store_id', 'Constraint is on "store_id"' ); |
388 | is( $c5->reference_table, 'store', 'To table "store"' ); |
c9ca0061 |
389 | is( join(',', map { $_ || '' } $c5->reference_fields), '', |
390 | 'No reference fields defined' ); |
2ab84c11 |
391 | |
392 | my $t2 = shift @tables; |
393 | is( $t2->name, 'address', 'Found "address" table' ); |
394 | |
395 | my @t2_fields = $t2->get_fields; |
396 | is( scalar @t2_fields, 8, 'Right number of fields (8)' ); |
251b6ff5 |
397 | } |
13aec984 |
398 | |
399 | # djh Tests for: |
400 | # USE database ; |
401 | # ALTER TABLE ADD FOREIGN KEY |
402 | # trailing comma on last create definition |
403 | # Ignoring INSERT statements |
404 | # |
405 | { |
406 | my $tr = SQL::Translator->new; |
407 | my $data = parse($tr, |
408 | q[ |
409 | USE database_name; |
410 | |
411 | CREATE TABLE one ( |
412 | id integer NOT NULL auto_increment, |
413 | two_id integer NOT NULL auto_increment, |
414 | some_data text, |
415 | PRIMARY KEY (id), |
416 | INDEX (two_id), |
417 | ) TYPE=INNODB; |
418 | |
419 | CREATE TABLE two ( |
420 | id int NOT NULL auto_increment, |
421 | one_id int NOT NULL auto_increment, |
422 | some_data text, |
423 | PRIMARY KEY (id), |
424 | INDEX (one_id), |
425 | FOREIGN KEY (one_id) REFERENCES one (id), |
426 | ) TYPE=INNODB; |
427 | |
428 | ALTER TABLE one ADD FOREIGN KEY (two_id) REFERENCES two (id); |
429 | |
306c3c97 |
430 | INSERT absolutely *#! any old $£ ? rubbish, even "quoted; semi-what""sits"; |
13aec984 |
431 | ] |
432 | ) or die $tr->error; |
433 | |
434 | my $schema = $tr->schema; |
435 | is( $schema->is_valid, 1, 'Schema is valid' ); |
436 | my $db_name = $schema->name; |
437 | is( $db_name, 'database_name', 'Database name extracted from USE' ); |
438 | my @tables = $schema->get_tables; |
439 | is( scalar @tables, 2, 'Right number of tables (2)' ); |
440 | my $table1 = shift @tables; |
441 | is( $table1->name, 'one', 'Found "one" table' ); |
442 | my $table2 = shift @tables; |
443 | is( $table2->name, 'two', 'Found "two" table' ); |
444 | |
445 | my @constraints = $table1->get_constraints; |
446 | is(scalar @constraints, 2, 'Right number of constraints (2) on table one'); |
447 | |
448 | my $t1c1 = shift @constraints; |
449 | is( $t1c1->type, PRIMARY_KEY, 'Constraint is a PK' ); |
450 | is( join(',', $t1c1->fields), 'id', 'Constraint is on "id"' ); |
451 | |
452 | my $t1c2 = shift @constraints; |
453 | is( $t1c2->type, FOREIGN_KEY, 'Constraint is a FK' ); |
454 | is( join(',', $t1c2->fields), 'two_id', 'Constraint is on "two_id"' ); |
455 | is( $t1c2->reference_table, 'two', 'To table "two"' ); |
456 | is( join(',', $t1c2->reference_fields), 'id', 'To field "id"' ); |
457 | |
458 | @constraints = $table2->get_constraints; |
459 | is(scalar @constraints, 2, 'Right number of constraints (2) on table two'); |
460 | |
461 | my $t2c1 = shift @constraints; |
462 | is( $t2c1->type, PRIMARY_KEY, 'Constraint is a PK' ); |
463 | is( join(',', $t2c1->fields), 'id', 'Constraint is on "id"' ); |
464 | |
465 | my $t2c2 = shift @constraints; |
466 | is( $t2c2->type, FOREIGN_KEY, 'Constraint is a FK' ); |
467 | is( join(',', $t2c2->fields), 'one_id', 'Constraint is on "one_id"' ); |
468 | is( $t2c2->reference_table, 'one', 'To table "one"' ); |
469 | is( join(',', $t2c2->reference_fields), 'id', 'To field "id"' ); |
470 | } |
471 | |
b68bfdf0 |
472 | # cch Tests for: |
473 | # comments like: /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; |
474 | # char fields with character set and collate qualifiers |
475 | # timestamp fields with on update qualifier |
476 | # charset table option |
477 | # |
478 | { |
479 | my $tr = SQL::Translator->new; |
480 | my $data = parse($tr, |
481 | q[ |
3e5e5622 |
482 | DELIMITER ;; |
483 | /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;; |
da9f2af8 |
484 | /*!50003 CREATE */ /*!50017 DEFINER=`cmdomain`@`localhost` */ |
485 | /*!50003 TRIGGER `acl_entry_insert` BEFORE INSERT ON `acl_entry` |
486 | FOR EACH ROW SET NEW.dateCreated = CONVERT_TZ(SYSDATE(),'SYSTEM','+0:00'), |
487 | NEW.dateModified = CONVERT_TZ(SYSDATE(),'SYSTEM','+0:00') */;; |
b68bfdf0 |
488 | |
3e5e5622 |
489 | DELIMITER ; |
b68bfdf0 |
490 | CREATE TABLE one ( |
491 | `op` varchar(255) character set latin1 collate latin1_bin default NULL, |
492 | `last_modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, |
493 | ) TYPE=INNODB DEFAULT CHARSET=latin1; |
494 | ] |
495 | ) or die $tr->error; |
496 | |
497 | my $schema = $tr->schema; |
498 | is( $schema->is_valid, 1, 'Schema is valid' ); |
499 | my @tables = $schema->get_tables; |
500 | is( scalar @tables, 1, 'Right number of tables (1)' ); |
501 | my $table1 = shift @tables; |
502 | is( $table1->name, 'one', 'Found "one" table' ); |
503 | |
504 | my @fields = $table1->get_fields; |
505 | is(scalar @fields, 2, 'Right number of fields (2) on table one'); |
506 | my $tableTypeFound = 0; |
507 | my $charsetFound = 0; |
508 | for my $t1_option_ref ( $table1->options ) { |
509 | my($key, $value) = %{$t1_option_ref}; |
510 | if ( $key eq 'TYPE' ) { |
511 | is($value, 'INNODB', 'Table has right table type option' ); |
512 | $tableTypeFound = 1; |
513 | } elsif ( $key eq 'CHARACTER SET' ) { |
514 | is($value, 'latin1', 'Table has right character set option' ); |
515 | $charsetFound = 1; |
516 | } |
517 | } |
518 | fail('Table did not have a type option') unless $tableTypeFound; |
519 | fail('Table did not have a character set option') unless $charsetFound; |
520 | |
521 | my $t1f1 = shift @fields; |
522 | is( $t1f1->data_type, 'varchar', 'Field is a varchar' ); |
523 | is( $t1f1->size, 255, 'Field is right size' ); |
524 | is( $t1f1->extra('character set'), 'latin1', 'Field has right character set qualifier' ); |
525 | is( $t1f1->extra('collate'), 'latin1_bin', 'Field has right collate qualifier' ); |
526 | is( $t1f1->default_value, 'NULL', 'Field has right default value' ); |
527 | |
528 | my $t1f2 = shift @fields; |
529 | is( $t1f2->data_type, 'timestamp', 'Field is a timestamp' ); |
530 | ok( !$t1f2->is_nullable, 'Field is not nullable' ); |
531 | is( $t1f2->default_value, 'CURRENT_TIMESTAMP', 'Field has right default value' ); |
532 | is( $t1f2->extra('on update'), 'CURRENT_TIMESTAMP', 'Field has right on update qualifier' ); |
533 | } |
534 | |