Applied Dave Howorth's MySQL parser patches
[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 Test::SQL::Translator qw(maybe_plan);
11
12 BEGIN {
13     maybe_plan(199, "SQL::Translator::Parser::MySQL");
14     SQL::Translator::Parser::MySQL->import('parse');
15 }
16
17 {
18     my $tr = SQL::Translator->new;
19     my $data = q|create table sessions (
20         id char(32) not null default '0' primary key,
21         a_session text
22     );|;
23
24     my $val = parse($tr, $data);
25
26     my $schema = $tr->schema;
27     is( $schema->is_valid, 1, 'Schema is valid' );
28     my @tables = $schema->get_tables;
29     is( scalar @tables, 1, 'Right number of tables (1)' );
30     my $table  = shift @tables;
31     is( $table->name, 'sessions', 'Found "sessions" table' );
32
33     my @fields = $table->get_fields;
34     is( scalar @fields, 2, 'Right number of fields (2)' );
35     my $f1 = shift @fields;
36     my $f2 = shift @fields;
37     is( $f1->name, 'id', 'First field name is "id"' );
38     is( $f1->data_type, 'char', 'Type is "char"' );
39     is( $f1->size, 32, 'Size is "32"' );
40     is( $f1->is_nullable, 0, 'Field cannot be null' );
41     is( $f1->default_value, '0', 'Default value is "0"' );
42     is( $f1->is_primary_key, 1, 'Field is PK' );
43
44     is( $f2->name, 'a_session', 'Second field name is "a_session"' );
45     is( $f2->data_type, 'text', 'Type is "text"' );
46     is( $f2->size, 65_535, 'Size is "65,535"' );
47     is( $f2->is_nullable, 1, 'Field can be null' );
48     is( $f2->default_value, undef, 'Default value is undefined' );
49     is( $f2->is_primary_key, 0, 'Field is not PK' );
50
51     my @indices = $table->get_indices;
52     is( scalar @indices, 0, 'Right number of indices (0)' );
53
54     my @constraints = $table->get_constraints;
55     is( scalar @constraints, 1, 'Right number of constraints (1)' );
56     my $c = shift @constraints;
57     is( $c->type, PRIMARY_KEY, 'Constraint is a PK' );
58     is( join(',', $c->fields), 'id', 'Constraint is on "id"' );
59 }
60
61 {
62     my $tr = SQL::Translator->new;
63     my $data = parse($tr, 
64         q[
65             CREATE TABLE check (
66               check_id int(7) unsigned zerofill NOT NULL default '0000000' 
67                 auto_increment primary key,
68               successful date NOT NULL default '0000-00-00',
69               unsuccessful date default '0000-00-00',
70               i1 int(11) default '0' not null,
71               s1 set('a','b','c') default 'b',
72               e1 enum('a','b','c') default 'c',
73               name varchar(30) default NULL,
74               foo_type enum('vk','ck') NOT NULL default 'vk',
75               date timestamp,
76               time_stamp2 timestamp,
77               KEY (i1),
78               UNIQUE (date, i1),
79               KEY date_idx (date),
80               KEY name_idx (name(10))
81             ) TYPE=MyISAM PACK_KEYS=1;
82         ]
83     );
84     
85     my $schema = $tr->schema;
86     is( $schema->is_valid, 1, 'Schema is valid' );
87     my @tables = $schema->get_tables;
88     is( scalar @tables, 1, 'Right number of tables (1)' );
89     my $table  = shift @tables;
90     is( $table->name, 'check', 'Found "check" table' );
91
92     my @fields = $table->get_fields;
93     is( scalar @fields, 10, 'Right number of fields (10)' );
94     my $f1 = shift @fields;
95     is( $f1->name, 'check_id', 'First field name is "check_id"' );
96     is( $f1->data_type, 'int', 'Type is "int"' );
97     is( $f1->size, 7, 'Size is "7"' );
98     is( $f1->is_nullable, 0, 'Field cannot be null' );
99     is( $f1->default_value, '0000000', 'Default value is "0000000"' );
100     is( $f1->is_primary_key, 1, 'Field is PK' );
101     is( $f1->is_auto_increment, 1, 'Field is auto inc' );
102     my %extra = $f1->extra;
103     ok( defined $extra{'unsigned'}, 'Field is unsigned' );
104     ok( defined $extra{'zerofill'}, 'Field is zerofill' );
105
106     my $f2 = shift @fields;
107     is( $f2->name, 'successful', 'Second field name is "successful"' );
108     is( $f2->data_type, 'date', 'Type is "date"' );
109     is( $f2->size, 0, 'Size is "0"' );
110     is( $f2->is_nullable, 0, 'Field cannot be null' );
111     is( $f2->default_value, '0000-00-00', 'Default value is "0000-00-00"' );
112     is( $f2->is_primary_key, 0, 'Field is not PK' );
113
114     my $f3 = shift @fields;
115     is( $f3->name, 'unsuccessful', 'Third field name is "unsuccessful"' );
116     is( $f3->data_type, 'date', 'Type is "date"' );
117     is( $f3->size, 0, 'Size is "0"' );
118     is( $f3->is_nullable, 1, 'Field can be null' );
119     is( $f3->default_value, '0000-00-00', 'Default value is "0000-00-00"' );
120     is( $f3->is_primary_key, 0, 'Field is not PK' );
121
122     my $f4 = shift @fields;
123     is( $f4->name, 'i1', 'Fourth field name is "i1"' );
124     is( $f4->data_type, 'int', 'Type is "int"' );
125     is( $f4->size, 11, 'Size is "11"' );
126     is( $f4->is_nullable, 0, 'Field cannot be null' );
127     is( $f4->default_value, '0', 'Default value is "0"' );
128     is( $f4->is_primary_key, 0, 'Field is not PK' );
129
130     my $f5 = shift @fields;
131     is( $f5->name, 's1', 'Fifth field name is "s1"' );
132     is( $f5->data_type, 'set', 'Type is "set"' );
133     is( $f5->size, 1, 'Size is "1"' );
134     is( $f5->is_nullable, 1, 'Field can be null' );
135     is( $f5->default_value, 'b', 'Default value is "b"' );
136     is( $f5->is_primary_key, 0, 'Field is not PK' );
137     my %f5extra = $f5->extra;
138     is( join(',', @{ $f5extra{'list'} || [] }), 'a,b,c', 'List is "a,b,c"' );
139
140     my $f6 = shift @fields;
141     is( $f6->name, 'e1', 'Sixth field name is "e1"' );
142     is( $f6->data_type, 'enum', 'Type is "enum"' );
143     is( $f6->size, 1, 'Size is "1"' );
144     is( $f6->is_nullable, 1, 'Field can be null' );
145     is( $f6->default_value, 'c', 'Default value is "c"' );
146     is( $f6->is_primary_key, 0, 'Field is not PK' );
147     my %f6extra = $f6->extra;
148     is( join(',', @{ $f6extra{'list'} || [] }), 'a,b,c', 'List is "a,b,c"' );
149
150     my $f7 = shift @fields;
151     is( $f7->name, 'name', 'Seventh field name is "name"' );
152     is( $f7->data_type, 'varchar', 'Type is "varchar"' );
153     is( $f7->size, 30, 'Size is "30"' );
154     is( $f7->is_nullable, 1, 'Field can be null' );
155     is( $f7->default_value, 'NULL', 'Default value is "NULL"' );
156     is( $f7->is_primary_key, 0, 'Field is not PK' );
157
158     my $f8 = shift @fields;
159     is( $f8->name, 'foo_type', 'Eighth field name is "foo_type"' );
160     is( $f8->data_type, 'enum', 'Type is "enum"' );
161     is( $f8->size, 2, 'Size is "2"' );
162     is( $f8->is_nullable, 0, 'Field cannot be null' );
163     is( $f8->default_value, 'vk', 'Default value is "vk"' );
164     is( $f8->is_primary_key, 0, 'Field is not PK' );
165     my %f8extra = $f8->extra;
166     is( join(',', @{ $f8extra{'list'} || [] }), 'vk,ck', 'List is "vk,ck"' );
167
168     my $f9 = shift @fields;
169     is( $f9->name, 'date', 'Ninth field name is "date"' );
170     is( $f9->data_type, 'timestamp', 'Type is "timestamp"' );
171     is( $f9->size, 0, 'Size is "0"' );
172     is( $f9->is_nullable, 1, 'Field can be null' );
173     is( $f9->default_value, undef, 'Default value is undefined' );
174     is( $f9->is_primary_key, 0, 'Field is not PK' );
175
176     my $f10 = shift @fields;
177     is( $f10->name, 'time_stamp2', 'Tenth field name is "time_stamp2"' );
178     is( $f10->data_type, 'timestamp', 'Type is "timestamp"' );
179     is( $f10->size, 0, 'Size is "0"' );
180     is( $f10->is_nullable, 1, 'Field can be null' );
181     is( $f10->default_value, undef, 'Default value is undefined' );
182     is( $f10->is_primary_key, 0, 'Field is not PK' );
183
184     my @indices = $table->get_indices;
185     is( scalar @indices, 3, 'Right number of indices (3)' );
186
187     my $i1 = shift @indices;
188     is( $i1->name, '', 'No name on index' );
189     is( $i1->type, NORMAL, 'Normal index' );
190     is( join(',', $i1->fields ), 'i1', 'Index is on field "i1"' );
191
192     my $i2 = shift @indices;
193     is( $i2->name, 'date_idx', 'Name is "date_idx"' );
194     is( $i2->type, NORMAL, 'Normal index' );
195     is( join(',', $i2->fields ), 'date', 'Index is on field "date"' );
196
197     my $i3 = shift @indices;
198     is( $i3->name, 'name_idx', 'Name is "name_idx"' );
199     is( $i3->type, NORMAL, 'Normal index' );
200     is( join(',', $i3->fields ), 'name(10)', 'Index is on field "name(10)"' );
201
202     my @constraints = $table->get_constraints;
203     is( scalar @constraints, 2, 'Right number of constraints (2)' );
204
205     my $c1 = shift @constraints;
206     is( $c1->type, PRIMARY_KEY, 'Constraint is a PK' );
207     is( join(',', $c1->fields), 'check_id', 'Constraint is on "check_id"' );
208
209     my $c2 = shift @constraints;
210     is( $c2->type, UNIQUE, 'Constraint is UNIQUE' );
211     is( join(',', $c2->fields), 'date,i1', 'Constraint is on "date, i1"' );
212 }
213
214 {
215     my $tr = SQL::Translator->new;
216     my $data = parse($tr, 
217         q[
218             CREATE TABLE orders (
219               order_id                  integer NOT NULL auto_increment,
220               member_id                 varchar(255),
221               billing_address_id        int,
222               shipping_address_id       int,
223               credit_card_id            int,
224               status                    smallint NOT NULL,
225               store_id                  varchar(255) NOT NULL REFERENCES store,
226               tax                       decimal(8,2),
227               shipping_charge           decimal(8,2),
228               price_paid                decimal(8,2),
229               PRIMARY KEY (order_id),
230               KEY (status),
231               KEY (billing_address_id),
232               KEY (shipping_address_id),
233               KEY (member_id, store_id),
234               FOREIGN KEY (status)              REFERENCES order_status(id) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE,
235               FOREIGN KEY (billing_address_id)  REFERENCES address(address_id),
236               FOREIGN KEY (shipping_address_id) REFERENCES address(address_id)
237             ) TYPE=INNODB;
238
239             CREATE TABLE address (
240               address_id                int NOT NULL auto_increment,
241               recipient                 varchar(255) NOT NULL,
242               address1                  varchar(255) NOT NULL,
243               address2                  varchar(255),
244               city                      varchar(255) NOT NULL,
245               state                     varchar(255) NOT NULL,
246               postal_code               varchar(255) NOT NULL,
247               phone                     varchar(255),
248               PRIMARY KEY (address_id)
249             ) TYPE=INNODB;
250         ]
251     ) or die $tr->error;
252
253     my $schema = $tr->schema;
254     is( $schema->is_valid, 1, 'Schema is valid' );
255     my @tables = $schema->get_tables;
256     is( scalar @tables, 2, 'Right number of tables (2)' );
257
258     my $t1  = shift @tables;
259     is( $t1->name, 'orders', 'Found "orders" table' );
260
261     my @fields = $t1->get_fields;
262     is( scalar @fields, 10, 'Right number of fields (10)' );
263
264     my $f1 = shift @fields;
265     is( $f1->name, 'order_id', 'First field name is "order_id"' );
266     is( $f1->data_type, 'int', 'Type is "int"' );
267     is( $f1->size, 11, 'Size is "11"' );
268     is( $f1->is_nullable, 0, 'Field cannot be null' );
269     is( $f1->default_value, undef, 'Default value is undefined' );
270     is( $f1->is_primary_key, 1, 'Field is PK' );
271     is( $f1->is_auto_increment, 1, 'Field is auto inc' );
272
273     my $f2 = shift @fields;
274     is( $f2->name, 'member_id', 'Second field name is "member_id"' );
275     is( $f2->data_type, 'varchar', 'Type is "varchar"' );
276     is( $f2->size, 255, 'Size is "255"' );
277     is( $f2->is_nullable, 1, 'Field can be null' );
278     is( $f2->default_value, undef, 'Default value is undefined' );
279
280     my $f3 = shift @fields;
281     is( $f3->name, 'billing_address_id', 
282         'Third field name is "billing_address_id"' );
283     is( $f3->data_type, 'int', 'Type is "int"' );
284     is( $f3->size, 11, 'Size is "11"' );
285
286     my $f4 = shift @fields;
287     is( $f4->name, 'shipping_address_id', 
288         'Fourth field name is "shipping_address_id"' );
289     is( $f4->data_type, 'int', 'Type is "int"' );
290     is( $f4->size, 11, 'Size is "11"' );
291
292     my $f5 = shift @fields;
293     is( $f5->name, 'credit_card_id', 'Fifth field name is "credit_card_id"' );
294     is( $f5->data_type, 'int', 'Type is "int"' );
295     is( $f5->size, 11, 'Size is "11"' );
296
297     my $f6 = shift @fields;
298     is( $f6->name, 'status', 'Sixth field name is "status"' );
299     is( $f6->data_type, 'smallint', 'Type is "smallint"' );
300     is( $f6->size, 6, 'Size is "6"' );
301     is( $f6->is_nullable, 0, 'Field cannot be null' );
302
303     my $f7 = shift @fields;
304     is( $f7->name, 'store_id', 'Seventh field name is "store_id"' );
305     is( $f7->data_type, 'varchar', 'Type is "varchar"' );
306     is( $f7->size, 255, 'Size is "255"' );
307     is( $f7->is_nullable, 0, 'Field cannot be null' );
308     is( $f7->is_foreign_key, 1, 'Field is a FK' );
309     my $fk_ref = $f7->foreign_key_reference;
310     isa_ok( $fk_ref, 'SQL::Translator::Schema::Constraint', 'FK' );
311     is( $fk_ref->reference_table, 'store', 'FK is to "store" table' );
312
313     my $f8 = shift @fields;
314     is( $f8->name, 'tax', 'Eighth field name is "tax"' );
315     is( $f8->data_type, 'decimal', 'Type is "decimal"' );
316     is( $f8->size, '8,2', 'Size is "8,2"' );
317
318     my $f9 = shift @fields;
319     is( $f9->name, 'shipping_charge', 'Ninth field name is "shipping_charge"' );
320     is( $f9->data_type, 'decimal', 'Type is "decimal"' );
321     is( $f9->size, '8,2', 'Size is "8,2"' );
322
323     my $f10 = shift @fields;
324     is( $f10->name, 'price_paid', 'Tenth field name is "price_paid"' );
325     is( $f10->data_type, 'decimal', 'Type is "decimal"' );
326     is( $f10->size, '8,2', 'Size is "8,2"' );
327
328     my @indices = $t1->get_indices;
329     is( scalar @indices, 4, 'Right number of indices (4)' );
330
331     my $i1 = shift @indices;
332     is( $i1->type, NORMAL, 'First index is normal' );
333     is( join(',', $i1->fields), 'status', 'Index is on "status"' );
334
335     my $i2 = shift @indices;
336     is( $i2->type, NORMAL, 'Second index is normal' );
337     is( join(',', $i2->fields), 'billing_address_id', 
338         'Index is on "billing_address_id"' );
339
340     my $i3 = shift @indices;
341     is( $i3->type, NORMAL, 'Third index is normal' );
342     is( join(',', $i3->fields), 'shipping_address_id', 
343         'Index is on "shipping_address_id"' );
344
345     my $i4 = shift @indices;
346     is( $i4->type, NORMAL, 'Third index is normal' );
347     is( join(',', $i4->fields), 'member_id,store_id', 
348         'Index is on "member_id,store_id"' );
349
350     my @constraints = $t1->get_constraints;
351     is( scalar @constraints, 5, 'Right number of constraints (5)' );
352
353     my $c1 = shift @constraints;
354     is( $c1->type, PRIMARY_KEY, 'Constraint is a PK' );
355     is( join(',', $c1->fields), 'order_id', 'Constraint is on "order_id"' );
356
357     my $c2 = shift @constraints;
358     is( $c2->type, FOREIGN_KEY, 'Constraint is a FK' );
359     is( join(',', $c2->fields), 'status', 'Constraint is on "status"' );
360     is( $c2->reference_table, 'order_status', 'To table "order_status"' );
361     is( join(',', $c2->reference_fields), 'id', 'To field "id"' );
362
363     my $c3 = shift @constraints;
364     is( $c3->type, FOREIGN_KEY, 'Constraint is a FK' );
365     is( join(',', $c3->fields), 'billing_address_id', 
366         'Constraint is on "billing_address_id"' );
367     is( $c3->reference_table, 'address', 'To table "address"' );
368     is( join(',', $c3->reference_fields), 'address_id', 
369         'To field "address_id"' );
370
371     my $c4 = shift @constraints;
372     is( $c4->type, FOREIGN_KEY, 'Constraint is a FK' );
373     is( join(',', $c4->fields), 'shipping_address_id', 
374         'Constraint is on "shipping_address_id"' );
375     is( $c4->reference_table, 'address', 'To table "address"' );
376     is( join(',', $c4->reference_fields), 'address_id', 
377         'To field "address_id"' );
378
379     my $c5 = shift @constraints;
380     is( $c5->type, FOREIGN_KEY, 'Constraint is a FK' );
381     is( join(',', $c5->fields), 'store_id', 'Constraint is on "store_id"' );
382     is( $c5->reference_table, 'store', 'To table "store"' );
383     is( join(',', map { $_ || '' } $c5->reference_fields), '', 
384         'No reference fields defined' );
385
386     my $t2  = shift @tables;
387     is( $t2->name, 'address', 'Found "address" table' );
388
389     my @t2_fields = $t2->get_fields;
390     is( scalar @t2_fields, 8, 'Right number of fields (8)' );
391 }
392
393 # djh Tests for:
394 #    USE database ;
395 #    ALTER TABLE ADD FOREIGN KEY
396 #    trailing comma on last create definition
397 #    Ignoring INSERT statements
398 #
399 {
400     my $tr = SQL::Translator->new;
401     my $data = parse($tr, 
402         q[
403             USE database_name;
404
405             CREATE TABLE one (
406               id                     integer NOT NULL auto_increment,
407               two_id                 integer NOT NULL auto_increment,
408               some_data              text,
409               PRIMARY KEY (id),
410               INDEX (two_id),
411             ) TYPE=INNODB;
412
413             CREATE TABLE two (
414               id                     int NOT NULL auto_increment,
415               one_id                 int NOT NULL auto_increment,
416               some_data              text,
417               PRIMARY KEY (id),
418               INDEX (one_id),
419               FOREIGN KEY (one_id) REFERENCES one (id),
420             ) TYPE=INNODB;
421
422             ALTER TABLE one ADD FOREIGN KEY (two_id) REFERENCES two (id);
423
424             INSERT absolutely *#! any old $£ ? rubbish ;
425         ]
426     ) or die $tr->error;
427
428     my $schema = $tr->schema;
429     is( $schema->is_valid, 1, 'Schema is valid' );
430     my $db_name = $schema->name;
431     is( $db_name, 'database_name', 'Database name extracted from USE' );
432     my @tables = $schema->get_tables;
433     is( scalar @tables, 2, 'Right number of tables (2)' );
434     my $table1 = shift @tables;
435     is( $table1->name, 'one', 'Found "one" table' );
436     my $table2 = shift @tables;
437     is( $table2->name, 'two', 'Found "two" table' );
438
439     my @constraints = $table1->get_constraints;
440     is(scalar @constraints, 2, 'Right number of constraints (2) on table one');
441
442     my $t1c1 = shift @constraints;
443     is( $t1c1->type, PRIMARY_KEY, 'Constraint is a PK' );
444     is( join(',', $t1c1->fields), 'id', 'Constraint is on "id"' );
445
446     my $t1c2 = shift @constraints;
447     is( $t1c2->type, FOREIGN_KEY, 'Constraint is a FK' );
448     is( join(',', $t1c2->fields), 'two_id', 'Constraint is on "two_id"' );
449     is( $t1c2->reference_table, 'two', 'To table "two"' );
450     is( join(',', $t1c2->reference_fields), 'id', 'To field "id"' );
451
452     @constraints = $table2->get_constraints;
453     is(scalar @constraints, 2, 'Right number of constraints (2) on table two');
454
455     my $t2c1 = shift @constraints;
456     is( $t2c1->type, PRIMARY_KEY, 'Constraint is a PK' );
457     is( join(',', $t2c1->fields), 'id', 'Constraint is on "id"' );
458
459     my $t2c2 = shift @constraints;
460     is( $t2c2->type, FOREIGN_KEY, 'Constraint is a FK' );
461     is( join(',', $t2c2->fields), 'one_id', 'Constraint is on "one_id"' );
462     is( $t2c2->reference_table, 'one', 'To table "one"' );
463     is( join(',', $t2c2->reference_fields), 'id', 'To field "id"' );
464 }
465