Put double quotes and backticks around table identifiers to test rules.
[dbsrgits/SQL-Translator.git] / t / 02mysql-parser.t
1 #!/usr/bin/perl
2 # vim: set ft=perl:
3 #
4
5 use strict;
6
7 use Test::More;
8 use SQL::Translator;
9 use SQL::Translator::Schema::Constants;
10 use SQL::Translator::Utils qw//;
11 use Test::SQL::Translator qw(maybe_plan);
12
13 BEGIN {
14     maybe_plan(292, "SQL::Translator::Parser::MySQL");
15     SQL::Translator::Parser::MySQL->import('parse');
16 }
17
18 {
19     my $tr = SQL::Translator->new;
20     my $data = q|create table "sessions" (
21         id char(32) not null default '0' primary key,
22         a_session text,
23         ssn varchar(12) unique key,
24         age int key
25     );|;
26
27     my $val = parse($tr, $data);
28     my $schema = $tr->schema;
29     is( $schema->is_valid, 1, 'Schema is valid' );
30     my @tables = $schema->get_tables;
31     is( scalar @tables, 1, 'Right number of tables (1)' );
32     my $table  = shift @tables;
33     is( $table->name, 'sessions', 'Found "sessions" table' );
34
35     my @fields = $table->get_fields;
36     is( scalar @fields, 4, 'Right number of fields (4)' );
37     my $f1 = shift @fields;
38     my $f2 = shift @fields;
39     is( $f1->name, 'id', 'First field name is "id"' );
40     is( $f1->data_type, 'char', 'Type is "char"' );
41     is( $f1->size, 32, 'Size is "32"' );
42     is( $f1->is_nullable, 0, 'Field cannot be null' );
43     is( $f1->default_value, '0', 'Default value is "0"' );
44     is( $f1->is_primary_key, 1, 'Field is PK' );
45
46     is( $f2->name, 'a_session', 'Second field name is "a_session"' );
47     is( $f2->data_type, 'text', 'Type is "text"' );
48     is( $f2->size, 65_535, 'Size is "65,535"' );
49     is( $f2->is_nullable, 1, 'Field can be null' );
50     is( $f2->default_value, undef, 'Default value is undefined' );
51     is( $f2->is_primary_key, 0, 'Field is not PK' );
52
53     my @indices = $table->get_indices;
54     is( scalar @indices, 1, 'Right number of indices (1)' );
55
56     my @constraints = $table->get_constraints;
57     is( scalar @constraints, 2, 'Right number of constraints (2)' );
58     my $c = shift @constraints;
59     is( $c->type, PRIMARY_KEY, 'Constraint is a PK' );
60     is( join(',', $c->fields), 'id', 'Constraint is on "id"' );
61     my $c2 = shift @constraints;
62     is( $c2->type, UNIQUE, 'Constraint is UNIQUE' );
63     is( join(',', $c2->fields), 'ssn', 'Constraint is on "ssn"' );
64 }
65
66 {
67     my $tr = SQL::Translator->new;
68     my $data = parse($tr, 
69         q[
70             CREATE TABLE `check` (
71               check_id int(7) unsigned zerofill NOT NULL default '0000000' 
72                 auto_increment primary key,
73               successful date NOT NULL default '0000-00-00',
74               unsuccessful date default '0000-00-00',
75               i1 int(11) default '0' not null,
76               s1 set('a','b','c') default 'b',
77               e1 enum('a','b','c') default 'c',
78               name varchar(30) default NULL,
79               foo_type enum('vk','ck') NOT NULL default 'vk',
80               date timestamp,
81               time_stamp2 timestamp,
82               KEY (i1),
83               UNIQUE (date, i1),
84               KEY date_idx (date),
85               KEY name_idx (name(10))
86             ) TYPE=MyISAM PACK_KEYS=1;
87         ]
88     );
89     
90     my $schema = $tr->schema;
91     is( $schema->is_valid, 1, 'Schema is valid' );
92     my @tables = $schema->get_tables;
93     is( scalar @tables, 1, 'Right number of tables (1)' );
94     my $table  = shift @tables;
95     is( $table->name, 'check', 'Found "check" table' );
96
97     my @fields = $table->get_fields;
98     is( scalar @fields, 10, 'Right number of fields (10)' );
99     my $f1 = shift @fields;
100     is( $f1->name, 'check_id', 'First field name is "check_id"' );
101     is( $f1->data_type, 'int', 'Type is "int"' );
102     is( $f1->size, 7, 'Size is "7"' );
103     is( $f1->is_nullable, 0, 'Field cannot be null' );
104     is( $f1->default_value, '0000000', 'Default value is "0000000"' );
105     is( $f1->is_primary_key, 1, 'Field is PK' );
106     is( $f1->is_auto_increment, 1, 'Field is auto inc' );
107     my %extra = $f1->extra;
108     ok( defined $extra{'unsigned'}, 'Field is unsigned' );
109     ok( defined $extra{'zerofill'}, 'Field is zerofill' );
110
111     my $f2 = shift @fields;
112     is( $f2->name, 'successful', 'Second field name is "successful"' );
113     is( $f2->data_type, 'date', 'Type is "date"' );
114     is( $f2->size, 0, 'Size is "0"' );
115     is( $f2->is_nullable, 0, 'Field cannot be null' );
116     is( $f2->default_value, '0000-00-00', 'Default value is "0000-00-00"' );
117     is( $f2->is_primary_key, 0, 'Field is not PK' );
118
119     my $f3 = shift @fields;
120     is( $f3->name, 'unsuccessful', 'Third field name is "unsuccessful"' );
121     is( $f3->data_type, 'date', 'Type is "date"' );
122     is( $f3->size, 0, 'Size is "0"' );
123     is( $f3->is_nullable, 1, 'Field can be null' );
124     is( $f3->default_value, '0000-00-00', 'Default value is "0000-00-00"' );
125     is( $f3->is_primary_key, 0, 'Field is not PK' );
126
127     my $f4 = shift @fields;
128     is( $f4->name, 'i1', 'Fourth field name is "i1"' );
129     is( $f4->data_type, 'int', 'Type is "int"' );
130     is( $f4->size, 11, 'Size is "11"' );
131     is( $f4->is_nullable, 0, 'Field cannot be null' );
132     is( $f4->default_value, '0', 'Default value is "0"' );
133     is( $f4->is_primary_key, 0, 'Field is not PK' );
134
135     my $f5 = shift @fields;
136     is( $f5->name, 's1', 'Fifth field name is "s1"' );
137     is( $f5->data_type, 'set', 'Type is "set"' );
138     is( $f5->size, 1, 'Size is "1"' );
139     is( $f5->is_nullable, 1, 'Field can be null' );
140     is( $f5->default_value, 'b', 'Default value is "b"' );
141     is( $f5->is_primary_key, 0, 'Field is not PK' );
142     my %f5extra = $f5->extra;
143     is( join(',', @{ $f5extra{'list'} || [] }), 'a,b,c', 'List is "a,b,c"' );
144
145     my $f6 = shift @fields;
146     is( $f6->name, 'e1', 'Sixth field name is "e1"' );
147     is( $f6->data_type, 'enum', 'Type is "enum"' );
148     is( $f6->size, 1, 'Size is "1"' );
149     is( $f6->is_nullable, 1, 'Field can be null' );
150     is( $f6->default_value, 'c', 'Default value is "c"' );
151     is( $f6->is_primary_key, 0, 'Field is not PK' );
152     my %f6extra = $f6->extra;
153     is( join(',', @{ $f6extra{'list'} || [] }), 'a,b,c', 'List is "a,b,c"' );
154
155     my $f7 = shift @fields;
156     is( $f7->name, 'name', 'Seventh field name is "name"' );
157     is( $f7->data_type, 'varchar', 'Type is "varchar"' );
158     is( $f7->size, 30, 'Size is "30"' );
159     is( $f7->is_nullable, 1, 'Field can be null' );
160     is( $f7->default_value, 'NULL', 'Default value is "NULL"' );
161     is( $f7->is_primary_key, 0, 'Field is not PK' );
162
163     my $f8 = shift @fields;
164     is( $f8->name, 'foo_type', 'Eighth field name is "foo_type"' );
165     is( $f8->data_type, 'enum', 'Type is "enum"' );
166     is( $f8->size, 2, 'Size is "2"' );
167     is( $f8->is_nullable, 0, 'Field cannot be null' );
168     is( $f8->default_value, 'vk', 'Default value is "vk"' );
169     is( $f8->is_primary_key, 0, 'Field is not PK' );
170     my %f8extra = $f8->extra;
171     is( join(',', @{ $f8extra{'list'} || [] }), 'vk,ck', 'List is "vk,ck"' );
172
173     my $f9 = shift @fields;
174     is( $f9->name, 'date', 'Ninth field name is "date"' );
175     is( $f9->data_type, 'timestamp', 'Type is "timestamp"' );
176     is( $f9->size, 0, 'Size is "0"' );
177     is( $f9->is_nullable, 1, 'Field can be null' );
178     is( $f9->default_value, undef, 'Default value is undefined' );
179     is( $f9->is_primary_key, 0, 'Field is not PK' );
180
181     my $f10 = shift @fields;
182     is( $f10->name, 'time_stamp2', 'Tenth field name is "time_stamp2"' );
183     is( $f10->data_type, 'timestamp', 'Type is "timestamp"' );
184     is( $f10->size, 0, 'Size is "0"' );
185     is( $f10->is_nullable, 1, 'Field can be null' );
186     is( $f10->default_value, undef, 'Default value is undefined' );
187     is( $f10->is_primary_key, 0, 'Field is not PK' );
188
189     my @indices = $table->get_indices;
190     is( scalar @indices, 3, 'Right number of indices (3)' );
191
192     my $i1 = shift @indices;
193     is( $i1->name, '', 'No name on index' );
194     is( $i1->type, NORMAL, 'Normal index' );
195     is( join(',', $i1->fields ), 'i1', 'Index is on field "i1"' );
196
197     my $i2 = shift @indices;
198     is( $i2->name, 'date_idx', 'Name is "date_idx"' );
199     is( $i2->type, NORMAL, 'Normal index' );
200     is( join(',', $i2->fields ), 'date', 'Index is on field "date"' );
201
202     my $i3 = shift @indices;
203     is( $i3->name, 'name_idx', 'Name is "name_idx"' );
204     is( $i3->type, NORMAL, 'Normal index' );
205     is( join(',', $i3->fields ), 'name(10)', 'Index is on field "name(10)"' );
206
207     my @constraints = $table->get_constraints;
208     is( scalar @constraints, 2, 'Right number of constraints (2)' );
209
210     my $c1 = shift @constraints;
211     is( $c1->type, PRIMARY_KEY, 'Constraint is a PK' );
212     is( join(',', $c1->fields), 'check_id', 'Constraint is on "check_id"' );
213
214     my $c2 = shift @constraints;
215     is( $c2->type, UNIQUE, 'Constraint is UNIQUE' );
216     is( join(',', $c2->fields), 'date,i1', 'Constraint is on "date, i1"' );
217 }
218
219 {
220     my $tr = SQL::Translator->new;
221     my $data = parse($tr, 
222         q[
223             CREATE TABLE orders (
224               order_id                  integer NOT NULL auto_increment,
225               member_id                 varchar(255) comment 'fk to member',
226               billing_address_id        int,
227               shipping_address_id       int,
228               credit_card_id            int,
229               status                    smallint NOT NULL,
230               store_id                  varchar(255) NOT NULL REFERENCES store,
231               tax                       decimal(8,2),
232               shipping_charge           decimal(8,2),
233               price_paid                decimal(8,2),
234               PRIMARY KEY (order_id),
235               KEY (status),
236               KEY (billing_address_id),
237               KEY (shipping_address_id),
238               KEY (member_id, store_id),
239               FOREIGN KEY (status)              REFERENCES order_status(id) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE,
240               FOREIGN KEY (billing_address_id)  REFERENCES address(address_id),
241               FOREIGN KEY (shipping_address_id) REFERENCES address(address_id)
242             ) TYPE=INNODB COMMENT = 'orders table comment';
243
244             CREATE TABLE address (
245               address_id                int NOT NULL auto_increment,
246               recipient                 varchar(255) NOT NULL,
247               address1                  varchar(255) NOT NULL,
248               address2                  varchar(255),
249               city                      varchar(255) NOT NULL,
250               state                     varchar(255) NOT NULL,
251               postal_code               varchar(255) NOT NULL,
252               phone                     varchar(255),
253               PRIMARY KEY (address_id)
254             ) TYPE=INNODB;
255         ]
256     ) or die $tr->error;
257
258     my $schema = $tr->schema;
259     is( $schema->is_valid, 1, 'Schema is valid' );
260     my @tables = $schema->get_tables;
261     is( scalar @tables, 2, 'Right number of tables (2)' );
262
263     my $t1  = shift @tables;
264     is( $t1->name, 'orders', 'Found "orders" table' );
265     is( $t1->comments, 'orders table comment', 'Table comment OK' );
266
267     my @fields = $t1->get_fields;
268     is( scalar @fields, 10, 'Right number of fields (10)' );
269
270     my $f1 = shift @fields;
271     is( $f1->name, 'order_id', 'First field name is "order_id"' );
272     is( $f1->data_type, 'int', 'Type is "int"' );
273     is( $f1->size, 11, 'Size is "11"' );
274     is( $f1->is_nullable, 0, 'Field cannot be null' );
275     is( $f1->default_value, undef, 'Default value is undefined' );
276     is( $f1->is_primary_key, 1, 'Field is PK' );
277     is( $f1->is_auto_increment, 1, 'Field is auto inc' );
278
279     my $f2 = shift @fields;
280     is( $f2->name, 'member_id', 'Second field name is "member_id"' );
281     is( $f2->data_type, 'varchar', 'Type is "varchar"' );
282     is( $f2->size, 255, 'Size is "255"' );
283     is( $f2->is_nullable, 1, 'Field can be null' );
284     is( $f2->comments, 'fk to member', 'Field comment OK' );
285     is( $f2->default_value, undef, 'Default value is undefined' );
286
287     my $f3 = shift @fields;
288     is( $f3->name, 'billing_address_id', 
289         'Third field name is "billing_address_id"' );
290     is( $f3->data_type, 'int', 'Type is "int"' );
291     is( $f3->size, 11, 'Size is "11"' );
292
293     my $f4 = shift @fields;
294     is( $f4->name, 'shipping_address_id', 
295         'Fourth field name is "shipping_address_id"' );
296     is( $f4->data_type, 'int', 'Type is "int"' );
297     is( $f4->size, 11, 'Size is "11"' );
298
299     my $f5 = shift @fields;
300     is( $f5->name, 'credit_card_id', 'Fifth field name is "credit_card_id"' );
301     is( $f5->data_type, 'int', 'Type is "int"' );
302     is( $f5->size, 11, 'Size is "11"' );
303
304     my $f6 = shift @fields;
305     is( $f6->name, 'status', 'Sixth field name is "status"' );
306     is( $f6->data_type, 'smallint', 'Type is "smallint"' );
307     is( $f6->size, 6, 'Size is "6"' );
308     is( $f6->is_nullable, 0, 'Field cannot be null' );
309
310     my $f7 = shift @fields;
311     is( $f7->name, 'store_id', 'Seventh field name is "store_id"' );
312     is( $f7->data_type, 'varchar', 'Type is "varchar"' );
313     is( $f7->size, 255, 'Size is "255"' );
314     is( $f7->is_nullable, 0, 'Field cannot be null' );
315     is( $f7->is_foreign_key, 1, 'Field is a FK' );
316     my $fk_ref = $f7->foreign_key_reference;
317     isa_ok( $fk_ref, 'SQL::Translator::Schema::Constraint', 'FK' );
318     is( $fk_ref->reference_table, 'store', 'FK is to "store" table' );
319
320     my $f8 = shift @fields;
321     is( $f8->name, 'tax', 'Eighth field name is "tax"' );
322     is( $f8->data_type, 'decimal', 'Type is "decimal"' );
323     is( $f8->size, '8,2', 'Size is "8,2"' );
324
325     my $f9 = shift @fields;
326     is( $f9->name, 'shipping_charge', 'Ninth field name is "shipping_charge"' );
327     is( $f9->data_type, 'decimal', 'Type is "decimal"' );
328     is( $f9->size, '8,2', 'Size is "8,2"' );
329
330     my $f10 = shift @fields;
331     is( $f10->name, 'price_paid', 'Tenth field name is "price_paid"' );
332     is( $f10->data_type, 'decimal', 'Type is "decimal"' );
333     is( $f10->size, '8,2', 'Size is "8,2"' );
334
335     my @indices = $t1->get_indices;
336     is( scalar @indices, 4, 'Right number of indices (4)' );
337
338     my $i1 = shift @indices;
339     is( $i1->type, NORMAL, 'First index is normal' );
340     is( join(',', $i1->fields), 'status', 'Index is on "status"' );
341
342     my $i2 = shift @indices;
343     is( $i2->type, NORMAL, 'Second index is normal' );
344     is( join(',', $i2->fields), 'billing_address_id', 
345         'Index is on "billing_address_id"' );
346
347     my $i3 = shift @indices;
348     is( $i3->type, NORMAL, 'Third index is normal' );
349     is( join(',', $i3->fields), 'shipping_address_id', 
350         'Index is on "shipping_address_id"' );
351
352     my $i4 = shift @indices;
353     is( $i4->type, NORMAL, 'Third index is normal' );
354     is( join(',', $i4->fields), 'member_id,store_id', 
355         'Index is on "member_id,store_id"' );
356
357     my @constraints = $t1->get_constraints;
358     is( scalar @constraints, 5, 'Right number of constraints (5)' );
359
360     my $c1 = shift @constraints;
361     is( $c1->type, PRIMARY_KEY, 'Constraint is a PK' );
362     is( join(',', $c1->fields), 'order_id', 'Constraint is on "order_id"' );
363
364     my $c2 = shift @constraints;
365     is( $c2->type, FOREIGN_KEY, 'Constraint is a FK' );
366     is( join(',', $c2->fields), 'status', 'Constraint is on "status"' );
367     is( $c2->reference_table, 'order_status', 'To table "order_status"' );
368     is( join(',', $c2->reference_fields), 'id', 'To field "id"' );
369
370     my $c3 = shift @constraints;
371     is( $c3->type, FOREIGN_KEY, 'Constraint is a FK' );
372     is( join(',', $c3->fields), 'billing_address_id', 
373         'Constraint is on "billing_address_id"' );
374     is( $c3->reference_table, 'address', 'To table "address"' );
375     is( join(',', $c3->reference_fields), 'address_id', 
376         'To field "address_id"' );
377
378     my $c4 = shift @constraints;
379     is( $c4->type, FOREIGN_KEY, 'Constraint is a FK' );
380     is( join(',', $c4->fields), 'shipping_address_id', 
381         'Constraint is on "shipping_address_id"' );
382     is( $c4->reference_table, 'address', 'To table "address"' );
383     is( join(',', $c4->reference_fields), 'address_id', 
384         'To field "address_id"' );
385
386     my $c5 = shift @constraints;
387     is( $c5->type, FOREIGN_KEY, 'Constraint is a FK' );
388     is( join(',', $c5->fields), 'store_id', 'Constraint is on "store_id"' );
389     is( $c5->reference_table, 'store', 'To table "store"' );
390     is( join(',', map { $_ || '' } $c5->reference_fields), '', 
391         'No reference fields defined' );
392
393     my $t2  = shift @tables;
394     is( $t2->name, 'address', 'Found "address" table' );
395
396     my @t2_fields = $t2->get_fields;
397     is( scalar @t2_fields, 8, 'Right number of fields (8)' );
398 }
399
400 # djh Tests for:
401 #    USE database ;
402 #    ALTER TABLE ADD FOREIGN KEY
403 #    trailing comma on last create definition
404 #    Ignoring INSERT statements
405 #
406 {
407     my $tr = SQL::Translator->new;
408     my $data = parse($tr, 
409         q[
410             USE database_name;
411
412             CREATE TABLE one (
413               id                     integer NOT NULL auto_increment,
414               two_id                 integer NOT NULL auto_increment,
415               some_data              text,
416               PRIMARY KEY (id),
417               INDEX (two_id),
418             ) TYPE=INNODB;
419
420             CREATE TABLE two (
421               id                     int NOT NULL auto_increment,
422               one_id                 int NOT NULL auto_increment,
423               some_data              text,
424               PRIMARY KEY (id),
425               INDEX (one_id),
426               FOREIGN KEY (one_id) REFERENCES one (id),
427             ) TYPE=INNODB;
428
429             ALTER TABLE one ADD FOREIGN KEY (two_id) REFERENCES two (id);
430
431             INSERT absolutely *#! any old $£ ? rubbish, even "quoted; semi-what""sits";
432         ]
433     ) or die $tr->error;
434
435     my $schema = $tr->schema;
436     is( $schema->is_valid, 1, 'Schema is valid' );
437     my $db_name = $schema->name;
438     is( $db_name, 'database_name', 'Database name extracted from USE' );
439     my @tables = $schema->get_tables;
440     is( scalar @tables, 2, 'Right number of tables (2)' );
441     my $table1 = shift @tables;
442     is( $table1->name, 'one', 'Found "one" table' );
443     my $table2 = shift @tables;
444     is( $table2->name, 'two', 'Found "two" table' );
445
446     my @constraints = $table1->get_constraints;
447     is(scalar @constraints, 2, 'Right number of constraints (2) on table one');
448
449     my $t1c1 = shift @constraints;
450     is( $t1c1->type, PRIMARY_KEY, 'Constraint is a PK' );
451     is( join(',', $t1c1->fields), 'id', 'Constraint is on "id"' );
452
453     my $t1c2 = shift @constraints;
454     is( $t1c2->type, FOREIGN_KEY, 'Constraint is a FK' );
455     is( join(',', $t1c2->fields), 'two_id', 'Constraint is on "two_id"' );
456     is( $t1c2->reference_table, 'two', 'To table "two"' );
457     is( join(',', $t1c2->reference_fields), 'id', 'To field "id"' );
458
459     @constraints = $table2->get_constraints;
460     is(scalar @constraints, 2, 'Right number of constraints (2) on table two');
461
462     my $t2c1 = shift @constraints;
463     is( $t2c1->type, PRIMARY_KEY, 'Constraint is a PK' );
464     is( join(',', $t2c1->fields), 'id', 'Constraint is on "id"' );
465
466     my $t2c2 = shift @constraints;
467     is( $t2c2->type, FOREIGN_KEY, 'Constraint is a FK' );
468     is( join(',', $t2c2->fields), 'one_id', 'Constraint is on "one_id"' );
469     is( $t2c2->reference_table, 'one', 'To table "one"' );
470     is( join(',', $t2c2->reference_fields), 'id', 'To field "id"' );
471 }
472
473 # cch Tests for:
474 #    comments like: /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
475 #    char fields with character set and collate qualifiers
476 #    timestamp fields with on update qualifier
477 #    charset table option
478 #
479 {
480     my $tr = SQL::Translator->new(parser_args => {mysql_parser_version => 50003});
481     my $data = parse($tr, 
482         q[
483                 DELIMITER ;;
484             /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;;
485                         /*!50003 CREATE */ /*!50017 DEFINER=`cmdomain`@`localhost` */
486                         /*!50003 TRIGGER `acl_entry_insert` BEFORE INSERT ON `acl_entry`
487                                 FOR EACH ROW SET NEW.dateCreated = CONVERT_TZ(SYSDATE(),'SYSTEM','+0:00'),
488                                 NEW.dateModified = CONVERT_TZ(SYSDATE(),'SYSTEM','+0:00') */;;
489
490                         DELIMITER ;
491             CREATE TABLE one (
492               `op` varchar(255) character set latin1 collate latin1_bin default NULL,
493               `last_modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
494             ) TYPE=INNODB DEFAULT CHARSET=latin1;
495
496                         /*!50001 CREATE ALGORITHM=UNDEFINED */
497                         /*!50013 DEFINER=`cmdomain`@`localhost` SQL SECURITY DEFINER */
498                         /*! VIEW `vs_asset` AS 
499                                 select `a`.`asset_id` AS `asset_id`,`a`.`fq_name` AS `fq_name`,
500                                 `cfgmgmt_mig`.`ap_extract_folder`(`a`.`fq_name`) AS `folder_name`,
501                                 `cfgmgmt_mig`.`ap_extract_asset`(`a`.`fq_name`) AS `asset_name`,
502                                 `a`.`annotation` AS `annotation`,`a`.`asset_type` AS `asset_type`,
503                                 `a`.`foreign_asset_id` AS `foreign_asset_id`,
504                                 `a`.`foreign_asset_id2` AS `foreign_asset_id2`,`a`.`dateCreated` AS `date_created`,
505                                 `a`.`dateModified` AS `date_modified`,`a`.`container_id` AS `container_id`,
506                                 `a`.`creator_id` AS `creator_id`,`a`.`modifier_id` AS `modifier_id`,
507                                 `m`.`user_id` AS `user_access` 
508                                 from (`asset` `a` join `M_ACCESS_CONTROL` `m` on((`a`.`acl_id` = `m`.`acl_id`))) */;
509                         DELIMITER ;;
510                         /*!50003 CREATE*/ /*!50020 DEFINER=`cmdomain`@`localhost`*/ /*!50003 FUNCTION `ap_from_millitime_nullable`( millis_since_1970 BIGINT ) RETURNS timestamp
511                         DETERMINISTIC
512                                 BEGIN
513                                 DECLARE rval TIMESTAMP;
514                                     IF ( millis_since_1970 = 0 )
515                                     THEN
516                                         SET rval = NULL;
517                                     ELSE
518                                         SET rval = FROM_UNIXTIME( millis_since_1970 / 1000 );
519                                     END IF;
520                                     RETURN rval;
521                                 END */;;
522                         /*!50003 CREATE*/ /*!50020 DEFINER=`cmdomain`@`localhost`*/ /*!50003 PROCEDURE `sp_update_security_acl`(IN t_acl_id INTEGER)
523                         BEGIN
524                                 DECLARE hasMoreRows BOOL DEFAULT TRUE;
525                                 DECLARE t_group_id INT;
526                                 DECLARE t_user_id INT ;
527                                 DECLARE t_user_name VARCHAR (512) ;
528                                 DECLARE t_message VARCHAR (512) ;
529
530                                 DROP TABLE IF EXISTS group_acl;
531                                 DROP TABLE IF EXISTS user_group;
532                                 DELETE FROM M_ACCESS_CONTROL WHERE acl_id = t_acl_id;
533
534                                 CREATE TEMPORARY TABLE group_acl SELECT DISTINCT p.id group_id, d.acl_id acl_id
535                                         FROM  asset d, acl_entry e, alterpoint_principal p
536                                         WHERE d.acl_id = e.acl
537                                         AND p.id = e.principal AND d.acl_id = t_acl_id;
538
539                                 CREATE TEMPORARY TABLE user_group  SELECT a.id user_id, a.name user_name, c.id group_id
540                                         FROM alterpoint_principal a, groups_for_user b, alterpoint_principal c
541                                         WHERE a.id = b.user_ref AND b.elt = c.id;
542
543                                 INSERT INTO M_ACCESS_CONTROL SELECT DISTINCT group_acl.group_id, group_acl.acl_id, user_group.user_id, user_group.user_name
544                                         FROM group_acl, user_group
545                                         WHERE group_acl.group_id = user_group.group_id ;
546                         END */;;
547         ]
548     ) or die $tr->error;
549
550     my $schema = $tr->schema;
551     is( $schema->is_valid, 1, 'Schema is valid' );
552     my @tables = $schema->get_tables;
553     is( scalar @tables, 1, 'Right number of tables (1)' );
554     my $table1 = shift @tables;
555     is( $table1->name, 'one', 'Found "one" table' );
556
557     my @fields = $table1->get_fields;
558     is(scalar @fields, 2, 'Right number of fields (2) on table one');
559     my $tableTypeFound = 0;
560     my $charsetFound = 0;
561         for my $t1_option_ref ( $table1->options ) {
562                 my($key, $value) = %{$t1_option_ref};
563                 if ( $key eq 'TYPE' ) {
564                         is($value, 'INNODB', 'Table has right table type option' );
565                         $tableTypeFound = 1;
566                 } elsif ( $key eq 'CHARACTER SET' ) {
567                         is($value, 'latin1', 'Table has right character set option' );
568                         $charsetFound = 1;
569                 }
570         }
571         fail('Table did not have a type option') unless $tableTypeFound;
572         fail('Table did not have a character set option') unless $charsetFound;
573
574     my $t1f1 = shift @fields;
575     is( $t1f1->data_type, 'varchar', 'Field is a varchar' );
576     is( $t1f1->size, 255, 'Field is right size' );
577     is( $t1f1->extra('character set'), 'latin1', 'Field has right character set qualifier' );
578     is( $t1f1->extra('collate'), 'latin1_bin', 'Field has right collate qualifier' );
579     is( $t1f1->default_value, 'NULL', 'Field has right default value' );
580
581     my $t1f2 = shift @fields;
582     is( $t1f2->data_type, 'timestamp', 'Field is a timestamp' );
583     ok( !$t1f2->is_nullable, 'Field is not nullable' );
584     is_deeply(
585       $t1f2->default_value,
586       \'CURRENT_TIMESTAMP',
587       'Field has right default value' 
588     );
589     is( $t1f2->extra('on update'), 'CURRENT_TIMESTAMP', 'Field has right on update qualifier' );
590     
591     my @views = $schema->get_views;
592     is( scalar @views, 1, 'Right number of views (1)' );
593     my $view1 = shift @views;
594     is( $view1->name, 'vs_asset', 'Found "vs_asset" view' );
595         like($view1->sql, qr/ALGORITHM=UNDEFINED/, "Detected algorithm");
596         like($view1->sql, qr/vs_asset/, "Detected view vs_asset");
597         unlike($view1->sql, qr/cfgmgmt_mig/, "Did not detect cfgmgmt_mig");
598     
599     my @procs = $schema->get_procedures;
600     is( scalar @procs, 2, 'Right number of procedures (2)' );
601     my $proc1 = shift @procs;
602     is( $proc1->name, 'ap_from_millitime_nullable', 'Found "ap_from_millitime_nullable" procedure' );
603         like($proc1->sql, qr/CREATE FUNCTION ap_from_millitime_nullable/, "Detected procedure ap_from_millitime_nullable");
604     my $proc2 = shift @procs;
605     is( $proc2->name, 'sp_update_security_acl', 'Found "sp_update_security_acl" procedure' );
606         like($proc2->sql, qr/CREATE PROCEDURE sp_update_security_acl/, "Detected procedure sp_update_security_acl");
607 }
608
609 # Tests for collate table option
610 {
611     my $tr = SQL::Translator->new(parser_args => {mysql_parser_version => 50003});
612     my $data = parse($tr, 
613         q[
614           CREATE TABLE test ( id int ) DEFAULT CHARACTER SET latin1 COLLATE latin1_bin;
615          ] ); 
616
617     my $schema = $tr->schema;
618     is( $schema->is_valid, 1, 'Schema is valid' );
619     my @tables = $schema->get_tables;
620     is( scalar @tables, 1, 'Right number of tables (1)' );
621     my $table1 = shift @tables;
622     is( $table1->name, 'test', 'Found "test" table' );
623
624
625     my $collate = "Not found!";
626     my $charset = "Not found!";
627     for my $t1_option_ref ( $table1->options ) {
628       my($key, $value) = %{$t1_option_ref};
629       $collate = $value if $key eq 'COLLATE';
630       $charset = $value if $key eq 'CHARACTER SET';
631     }
632     is($collate, 'latin1_bin', "Collate found");
633     is($charset, 'latin1', "Character set found");
634 }
635
636 # Test the mysql version parser (probably needs to migrate to t/utils.t)
637 my $parse_as = {
638     perl => {
639         '3.23.2'    => 3.023002,
640         '4'         => 4.000000,
641         '50003'     => 5.000003,
642         '5.01.0'    => 5.001000,
643         '5.1'       => 5.001000,
644     },
645     mysql => {
646         '3.23.2'    => 32302,
647         '4'         => 40000,
648         '50003'     => 50003,
649         '5.01.0'    => 50100,
650         '5.1'       => 50100,
651     },
652 };
653
654 for my $target (keys %$parse_as) {
655     for my $str (keys %{$parse_as->{$target}}) {
656         cmp_ok (
657             SQL::Translator::Utils::parse_mysql_version ($str, $target),
658             '==',
659             $parse_as->{$target}{$str},
660             "'$str' parsed as $target version '$parse_as->{$target}{$str}'",
661         );
662     }
663 }
664
665 eval { SQL::Translator::Utils::parse_mysql_version ('bogus5.1') };
666 ok ($@, 'Exception thrown on invalid version string');
667
668 {
669     my $tr = SQL::Translator->new;
670     my $data = q|create table merge_example (
671        id int(11) NOT NULL auto_increment,
672        shape_field geometry NOT NULL,
673        PRIMARY KEY (id),
674        SPATIAL KEY shape_field (shape_field)
675     ) ENGINE=MRG_MyISAM UNION=(`sometable_0`,`sometable_1`,`sometable_2`);|;
676
677     my $val = parse($tr, $data);
678     my $schema = $tr->schema;
679     is( $schema->is_valid, 1, 'Schema is valid' );
680     my @tables = $schema->get_tables;
681     is( scalar @tables, 1, 'Right number of tables (1)' );
682     my $table  = shift @tables;
683     is( $table->name, 'merge_example', 'Found "merge_example" table' );
684
685     my $tableTypeFound = 0;
686     my $unionFound = 0;
687     for my $t_option_ref ( $table->options ) {
688       my($key, $value) = %{$t_option_ref};
689       if ( $key eq 'ENGINE' ) {
690         is($value, 'MRG_MyISAM', 'Table has right table engine option' );
691         $tableTypeFound = 1;
692       } elsif ( $key eq 'UNION' ) {
693         is_deeply($value, [ 'sometable_0','sometable_1','sometable_2' ],
694           "UNION option has correct set");
695         $unionFound = 1;
696       }
697     }
698
699     fail('Table did not have a type option') unless $tableTypeFound;
700     fail('Table did not have a union option') unless $unionFound;
701
702     my @fields = $table->get_fields;
703     is( scalar @fields, 2, 'Right number of fields (2)' );
704     my $f1 = shift @fields;
705     my $f2 = shift @fields;
706     is( $f1->name, 'id', 'First field name is "id"' );
707     is( $f1->data_type, 'int', 'Type is "int"' );
708     is( $f1->size, 11, 'Size is "11"' );
709     is( $f1->is_nullable, 0, 'Field cannot be null' );
710     is( $f1->is_primary_key, 1, 'Field is PK' );
711
712     is( $f2->name, 'shape_field', 'Second field name is "shape_field"' );
713     is( $f2->data_type, 'geometry', 'Type is "geometry"' );
714     is( $f2->is_nullable, 0, 'Field cannot be null' );
715     is( $f2->is_primary_key, 0, 'Field is not PK' );
716
717     my @indices = $table->get_indices;
718     is( scalar @indices, 1, 'Right number of indices (1)' );
719     my $i1 = shift @indices;
720     is( $i1->name, 'shape_field', 'No name on index' );
721     is( $i1->type, SPATIAL, 'Spatial index' );
722
723     my @constraints = $table->get_constraints;
724     is( scalar @constraints, 1, 'Right number of constraints (1)' );
725     my $c = shift @constraints;
726     is( $c->type, PRIMARY_KEY, 'Constraint is a PK' );
727     is( join(',', $c->fields), 'id', 'Constraint is on "id"' );
728 }
729
730 {
731     my @data = (
732         q|create table quote (
733             id int(11) NOT NULL auto_increment,
734             PRIMARY KEY (id)
735         ) ENGINE="innodb";|,
736         q|create table quote (
737             id int(11) NOT NULL auto_increment,
738             PRIMARY KEY (id)
739         ) ENGINE='innodb';|,
740         q|create table quote (
741             id int(11) NOT NULL auto_increment,
742             PRIMARY KEY (id)
743         ) ENGINE=innodb;|,
744     );
745     for my $data (@data) {
746         my $tr = SQL::Translator->new;
747
748         my $val = parse($tr, $data);
749         my $schema = $tr->schema;
750         is( $schema->is_valid, 1, 'Schema is valid' );
751         my @tables = $schema->get_tables;
752         is( scalar @tables, 1, 'Right number of tables (1)' );
753         my $table  = shift @tables;
754         is( $table->name, 'quote', 'Found "quote" table' );
755
756         my $tableTypeFound = 0;
757         for my $t_option_ref ( $table->options ) {
758         my($key, $value) = %{$t_option_ref};
759         if ( $key eq 'ENGINE' ) {
760             is($value, 'innodb', 'Table has right table engine option' );
761             $tableTypeFound = 1;
762         }
763         }
764
765         fail('Table did not have a type option') unless $tableTypeFound;
766
767         my @fields = $table->get_fields;
768         my $f1 = shift @fields;
769         is( $f1->name, 'id', 'First field name is "id"' );
770         is( $f1->data_type, 'int', 'Type is "int"' );
771         is( $f1->size, 11, 'Size is "11"' );
772         is( $f1->is_nullable, 0, 'Field cannot be null' );
773         is( $f1->is_primary_key, 1, 'Field is PK' );
774     }
775 }
776