Add support for COLLATE table option to MySQL parser
[dbsrgits/SQL-Translator.git] / t / 02mysql-parser.t
CommitLineData
0494e672 1#!/usr/bin/perl
2# vim: set ft=perl:
3#
0494e672 4
b8661f30 5use strict;
44fb27ae 6
fbc0552f 7use Test::More;
b8661f30 8use SQL::Translator;
2ab84c11 9use SQL::Translator::Schema::Constants;
2d691ec1 10use Test::SQL::Translator qw(maybe_plan);
0494e672 11
2d691ec1 12BEGIN {
a7f49dfb 13 maybe_plan(232, "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{
d31c185b 479 my $tr = SQL::Translator->new(parser_args => {mysql_parser_version => 50003});
b68bfdf0 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;
d31c185b 494
495 /*!50001 CREATE ALGORITHM=UNDEFINED */
496 /*!50013 DEFINER=`cmdomain`@`localhost` SQL SECURITY DEFINER */
497 /*! VIEW `vs_asset` AS
498 select `a`.`asset_id` AS `asset_id`,`a`.`fq_name` AS `fq_name`,
499 `cfgmgmt_mig`.`ap_extract_folder`(`a`.`fq_name`) AS `folder_name`,
500 `cfgmgmt_mig`.`ap_extract_asset`(`a`.`fq_name`) AS `asset_name`,
501 `a`.`annotation` AS `annotation`,`a`.`asset_type` AS `asset_type`,
502 `a`.`foreign_asset_id` AS `foreign_asset_id`,
503 `a`.`foreign_asset_id2` AS `foreign_asset_id2`,`a`.`dateCreated` AS `date_created`,
504 `a`.`dateModified` AS `date_modified`,`a`.`container_id` AS `container_id`,
505 `a`.`creator_id` AS `creator_id`,`a`.`modifier_id` AS `modifier_id`,
506 `m`.`user_id` AS `user_access`
507 from (`asset` `a` join `M_ACCESS_CONTROL` `m` on((`a`.`acl_id` = `m`.`acl_id`))) */;
508 DELIMITER ;;
509 /*!50003 CREATE*/ /*!50020 DEFINER=`cmdomain`@`localhost`*/ /*!50003 FUNCTION `ap_from_millitime_nullable`( millis_since_1970 BIGINT ) RETURNS timestamp
510 DETERMINISTIC
511 BEGIN
512 DECLARE rval TIMESTAMP;
513 IF ( millis_since_1970 = 0 )
514 THEN
515 SET rval = NULL;
516 ELSE
517 SET rval = FROM_UNIXTIME( millis_since_1970 / 1000 );
518 END IF;
519 RETURN rval;
520 END */;;
521 /*!50003 CREATE*/ /*!50020 DEFINER=`cmdomain`@`localhost`*/ /*!50003 PROCEDURE `sp_update_security_acl`(IN t_acl_id INTEGER)
522 BEGIN
523 DECLARE hasMoreRows BOOL DEFAULT TRUE;
524 DECLARE t_group_id INT;
525 DECLARE t_user_id INT ;
526 DECLARE t_user_name VARCHAR (512) ;
527 DECLARE t_message VARCHAR (512) ;
528
529 DROP TABLE IF EXISTS group_acl;
530 DROP TABLE IF EXISTS user_group;
531 DELETE FROM M_ACCESS_CONTROL WHERE acl_id = t_acl_id;
532
533 CREATE TEMPORARY TABLE group_acl SELECT DISTINCT p.id group_id, d.acl_id acl_id
534 FROM asset d, acl_entry e, alterpoint_principal p
535 WHERE d.acl_id = e.acl
536 AND p.id = e.principal AND d.acl_id = t_acl_id;
537
538 CREATE TEMPORARY TABLE user_group SELECT a.id user_id, a.name user_name, c.id group_id
539 FROM alterpoint_principal a, groups_for_user b, alterpoint_principal c
540 WHERE a.id = b.user_ref AND b.elt = c.id;
541
542 INSERT INTO M_ACCESS_CONTROL SELECT DISTINCT group_acl.group_id, group_acl.acl_id, user_group.user_id, user_group.user_name
543 FROM group_acl, user_group
544 WHERE group_acl.group_id = user_group.group_id ;
545 END */;;
b68bfdf0 546 ]
547 ) or die $tr->error;
548
549 my $schema = $tr->schema;
550 is( $schema->is_valid, 1, 'Schema is valid' );
551 my @tables = $schema->get_tables;
552 is( scalar @tables, 1, 'Right number of tables (1)' );
553 my $table1 = shift @tables;
554 is( $table1->name, 'one', 'Found "one" table' );
555
556 my @fields = $table1->get_fields;
557 is(scalar @fields, 2, 'Right number of fields (2) on table one');
558 my $tableTypeFound = 0;
559 my $charsetFound = 0;
560 for my $t1_option_ref ( $table1->options ) {
561 my($key, $value) = %{$t1_option_ref};
562 if ( $key eq 'TYPE' ) {
563 is($value, 'INNODB', 'Table has right table type option' );
564 $tableTypeFound = 1;
565 } elsif ( $key eq 'CHARACTER SET' ) {
566 is($value, 'latin1', 'Table has right character set option' );
567 $charsetFound = 1;
568 }
569 }
570 fail('Table did not have a type option') unless $tableTypeFound;
571 fail('Table did not have a character set option') unless $charsetFound;
572
573 my $t1f1 = shift @fields;
574 is( $t1f1->data_type, 'varchar', 'Field is a varchar' );
575 is( $t1f1->size, 255, 'Field is right size' );
576 is( $t1f1->extra('character set'), 'latin1', 'Field has right character set qualifier' );
577 is( $t1f1->extra('collate'), 'latin1_bin', 'Field has right collate qualifier' );
578 is( $t1f1->default_value, 'NULL', 'Field has right default value' );
579
580 my $t1f2 = shift @fields;
581 is( $t1f2->data_type, 'timestamp', 'Field is a timestamp' );
582 ok( !$t1f2->is_nullable, 'Field is not nullable' );
583 is( $t1f2->default_value, 'CURRENT_TIMESTAMP', 'Field has right default value' );
584 is( $t1f2->extra('on update'), 'CURRENT_TIMESTAMP', 'Field has right on update qualifier' );
d31c185b 585
586 my @views = $schema->get_views;
587 is( scalar @views, 1, 'Right number of views (1)' );
588 my $view1 = shift @views;
589 is( $view1->name, 'vs_asset', 'Found "vs_asset" view' );
590 like($view1->sql, qr/ALGORITHM=UNDEFINED/, "Detected algorithm");
591 like($view1->sql, qr/vs_asset/, "Detected view vs_asset");
592 unlike($view1->sql, qr/cfgmgmt_mig/, "Did not detect cfgmgmt_mig");
593
594 my @procs = $schema->get_procedures;
595 is( scalar @procs, 2, 'Right number of procedures (2)' );
596 my $proc1 = shift @procs;
597 is( $proc1->name, 'ap_from_millitime_nullable', 'Found "ap_from_millitime_nullable" procedure' );
598 like($proc1->sql, qr/CREATE FUNCTION ap_from_millitime_nullable/, "Detected procedure ap_from_millitime_nullable");
599 my $proc2 = shift @procs;
600 is( $proc2->name, 'sp_update_security_acl', 'Found "sp_update_security_acl" procedure' );
601 like($proc2->sql, qr/CREATE PROCEDURE sp_update_security_acl/, "Detected procedure sp_update_security_acl");
b68bfdf0 602}
603
a7f49dfb 604# Tests for collate table option
605{
606 my $tr = SQL::Translator->new(parser_args => {mysql_parser_version => 50003});
607 my $data = parse($tr,
608 q[
609 CREATE TABLE test ( id int ) COLLATE latin1_bin;
610 ] );
4d438549 611
a7f49dfb 612 my $schema = $tr->schema;
613 is( $schema->is_valid, 1, 'Schema is valid' );
614 my @tables = $schema->get_tables;
615 is( scalar @tables, 1, 'Right number of tables (1)' );
616 my $table1 = shift @tables;
617 is( $table1->name, 'test', 'Found "test" table' );
618
619
620 my $collate = "Not found!";
621 for my $t1_option_ref ( $table1->options ) {
622 my($key, $value) = %{$t1_option_ref};
623 if ($key eq 'COLLATE') {
624 $collate = $value;
625 last;
626 }
627 }
628 is($collate, 'latin1_bin', "Collate found");
629}