Commit | Line | Data |
0494e672 |
1 | #!/usr/bin/perl |
2 | # vim: set ft=perl: |
3 | # |
0494e672 |
4 | |
b8661f30 |
5 | use strict; |
44fb27ae |
6 | |
e3252059 |
7 | use Test::More tests => 180; |
b8661f30 |
8 | use SQL::Translator; |
0494e672 |
9 | use SQL::Translator::Parser::MySQL qw(parse); |
2ab84c11 |
10 | use SQL::Translator::Schema::Constants; |
0494e672 |
11 | |
251b6ff5 |
12 | { |
13 | my $tr = SQL::Translator->new; |
14 | my $data = q|create table sessions ( |
2ab84c11 |
15 | id char(32) not null default '0' primary key, |
251b6ff5 |
16 | a_session text |
17 | );|; |
18 | |
19 | my $val = parse($tr, $data); |
20 | |
2ab84c11 |
21 | my $schema = $tr->schema; |
e3252059 |
22 | is( $schema->is_valid, 1, 'Schema is valid' ); |
2ab84c11 |
23 | my @tables = $schema->get_tables; |
24 | is( scalar @tables, 1, 'Right number of tables (1)' ); |
25 | my $table = shift @tables; |
26 | is( $table->name, 'sessions', 'Found "sessions" table' ); |
251b6ff5 |
27 | |
2ab84c11 |
28 | my @fields = $table->get_fields; |
29 | is( scalar @fields, 2, 'Right number of fields (2)' ); |
30 | my $f1 = shift @fields; |
31 | my $f2 = shift @fields; |
32 | is( $f1->name, 'id', 'First field name is "id"' ); |
33 | is( $f1->data_type, 'char', 'Type is "char"' ); |
34 | is( $f1->size, 32, 'Size is "32"' ); |
35 | is( $f1->is_nullable, 0, 'Field cannot be null' ); |
36 | is( $f1->default_value, '0', 'Default value is "0"' ); |
37 | is( $f1->is_primary_key, 1, 'Field is PK' ); |
251b6ff5 |
38 | |
2ab84c11 |
39 | is( $f2->name, 'a_session', 'Second field name is "a_session"' ); |
40 | is( $f2->data_type, 'text', 'Type is "text"' ); |
0c1c8d2a |
41 | is( $f2->size, 65_535, 'Size is "65,535"' ); |
2ab84c11 |
42 | is( $f2->is_nullable, 1, 'Field can be null' ); |
43 | is( $f2->default_value, undef, 'Default value is undefined' ); |
44 | is( $f2->is_primary_key, 0, 'Field is not PK' ); |
251b6ff5 |
45 | |
2ab84c11 |
46 | my @indices = $table->get_indices; |
47 | is( scalar @indices, 0, 'Right number of indices (0)' ); |
251b6ff5 |
48 | |
2ab84c11 |
49 | my @constraints = $table->get_constraints; |
50 | is( scalar @constraints, 1, 'Right number of constraints (1)' ); |
51 | my $c = shift @constraints; |
52 | is( $c->type, PRIMARY_KEY, 'Constraint is a PK' ); |
53 | is( join(',', $c->fields), 'id', 'Constraint is on "id"' ); |
251b6ff5 |
54 | } |
55 | |
56 | { |
57 | my $tr = SQL::Translator->new; |
58 | my $data = parse($tr, |
59 | q[ |
60 | CREATE TABLE check ( |
2ab84c11 |
61 | check_id int(7) unsigned zerofill NOT NULL default '0000000' |
251b6ff5 |
62 | auto_increment primary key, |
63 | successful date NOT NULL default '0000-00-00', |
64 | unsuccessful date default '0000-00-00', |
65 | i1 int(11) default '0' not null, |
66 | s1 set('a','b','c') default 'b', |
67 | e1 enum('a','b','c') default 'c', |
68 | name varchar(30) default NULL, |
69 | foo_type enum('vk','ck') NOT NULL default 'vk', |
70 | date timestamp, |
71 | time_stamp2 timestamp, |
72 | KEY (i1), |
73 | UNIQUE (date, i1), |
74 | KEY date_idx (date), |
75 | KEY name_idx (name(10)) |
76 | ) TYPE=MyISAM PACK_KEYS=1; |
77 | ] |
78 | ); |
79 | |
2ab84c11 |
80 | my $schema = $tr->schema; |
e3252059 |
81 | is( $schema->is_valid, 1, 'Schema is valid' ); |
2ab84c11 |
82 | my @tables = $schema->get_tables; |
83 | is( scalar @tables, 1, 'Right number of tables (1)' ); |
84 | my $table = shift @tables; |
85 | is( $table->name, 'check', 'Found "check" table' ); |
86 | |
87 | my @fields = $table->get_fields; |
88 | is( scalar @fields, 10, 'Right number of fields (10)' ); |
89 | my $f1 = shift @fields; |
90 | is( $f1->name, 'check_id', 'First field name is "check_id"' ); |
91 | is( $f1->data_type, 'int', 'Type is "int"' ); |
92 | is( $f1->size, 7, 'Size is "7"' ); |
93 | is( $f1->is_nullable, 0, 'Field cannot be null' ); |
94 | is( $f1->default_value, '0000000', 'Default value is "0000000"' ); |
95 | is( $f1->is_primary_key, 1, 'Field is PK' ); |
96 | is( $f1->is_auto_increment, 1, 'Field is auto inc' ); |
97 | my %extra = $f1->extra; |
98 | ok( defined $extra{'unsigned'}, 'Field is unsigned' ); |
99 | ok( defined $extra{'zerofill'}, 'Field is zerofill' ); |
100 | |
101 | my $f2 = shift @fields; |
102 | is( $f2->name, 'successful', 'Second field name is "successful"' ); |
103 | is( $f2->data_type, 'date', 'Type is "date"' ); |
104 | is( $f2->size, 0, 'Size is "0"' ); |
105 | is( $f2->is_nullable, 0, 'Field cannot be null' ); |
106 | is( $f2->default_value, '0000-00-00', 'Default value is "0000-00-00"' ); |
107 | is( $f2->is_primary_key, 0, 'Field is not PK' ); |
108 | |
109 | my $f3 = shift @fields; |
110 | is( $f3->name, 'unsuccessful', 'Third field name is "unsuccessful"' ); |
111 | is( $f3->data_type, 'date', 'Type is "date"' ); |
112 | is( $f3->size, 0, 'Size is "0"' ); |
113 | is( $f3->is_nullable, 1, 'Field can be null' ); |
114 | is( $f3->default_value, '0000-00-00', 'Default value is "0000-00-00"' ); |
115 | is( $f3->is_primary_key, 0, 'Field is not PK' ); |
116 | |
117 | my $f4 = shift @fields; |
118 | is( $f4->name, 'i1', 'Fourth field name is "i1"' ); |
119 | is( $f4->data_type, 'int', 'Type is "int"' ); |
120 | is( $f4->size, 11, 'Size is "11"' ); |
121 | is( $f4->is_nullable, 0, 'Field cannot be null' ); |
122 | is( $f4->default_value, '0', 'Default value is "0"' ); |
123 | is( $f4->is_primary_key, 0, 'Field is not PK' ); |
3a0b6f0a |
124 | |
2ab84c11 |
125 | my $f5 = shift @fields; |
126 | is( $f5->name, 's1', 'Fifth field name is "s1"' ); |
127 | is( $f5->data_type, 'set', 'Type is "set"' ); |
128 | is( $f5->size, 1, 'Size is "1"' ); |
129 | is( $f5->is_nullable, 1, 'Field can be null' ); |
130 | is( $f5->default_value, 'b', 'Default value is "b"' ); |
131 | is( $f5->is_primary_key, 0, 'Field is not PK' ); |
132 | my %f5extra = $f5->extra; |
133 | is( join(',', @{ $f5extra{'list'} || [] }), 'a,b,c', 'List is "a,b,c"' ); |
3a0b6f0a |
134 | |
2ab84c11 |
135 | my $f6 = shift @fields; |
136 | is( $f6->name, 'e1', 'Sixth field name is "e1"' ); |
137 | is( $f6->data_type, 'enum', 'Type is "enum"' ); |
138 | is( $f6->size, 1, 'Size is "1"' ); |
139 | is( $f6->is_nullable, 1, 'Field can be null' ); |
140 | is( $f6->default_value, 'c', 'Default value is "c"' ); |
141 | is( $f6->is_primary_key, 0, 'Field is not PK' ); |
142 | my %f6extra = $f6->extra; |
143 | is( join(',', @{ $f6extra{'list'} || [] }), 'a,b,c', 'List is "a,b,c"' ); |
3a0b6f0a |
144 | |
2ab84c11 |
145 | my $f7 = shift @fields; |
146 | is( $f7->name, 'name', 'Seventh field name is "name"' ); |
147 | is( $f7->data_type, 'varchar', 'Type is "varchar"' ); |
148 | is( $f7->size, 30, 'Size is "30"' ); |
149 | is( $f7->is_nullable, 1, 'Field can be null' ); |
150 | is( $f7->default_value, 'NULL', 'Default value is "NULL"' ); |
151 | is( $f7->is_primary_key, 0, 'Field is not PK' ); |
3a0b6f0a |
152 | |
2ab84c11 |
153 | my $f8 = shift @fields; |
154 | is( $f8->name, 'foo_type', 'Eighth field name is "foo_type"' ); |
155 | is( $f8->data_type, 'enum', 'Type is "enum"' ); |
156 | is( $f8->size, 2, 'Size is "2"' ); |
157 | is( $f8->is_nullable, 0, 'Field cannot be null' ); |
158 | is( $f8->default_value, 'vk', 'Default value is "vk"' ); |
159 | is( $f8->is_primary_key, 0, 'Field is not PK' ); |
160 | my %f8extra = $f8->extra; |
161 | is( join(',', @{ $f8extra{'list'} || [] }), 'vk,ck', 'List is "vk,ck"' ); |
162 | |
163 | my $f9 = shift @fields; |
164 | is( $f9->name, 'date', 'Ninth field name is "date"' ); |
165 | is( $f9->data_type, 'timestamp', 'Type is "timestamp"' ); |
166 | is( $f9->size, 0, 'Size is "0"' ); |
167 | is( $f9->is_nullable, 1, 'Field can be null' ); |
168 | is( $f9->default_value, undef, 'Default value is undefined' ); |
169 | is( $f9->is_primary_key, 0, 'Field is not PK' ); |
170 | |
171 | my $f10 = shift @fields; |
172 | is( $f10->name, 'time_stamp2', 'Tenth field name is "time_stamp2"' ); |
173 | is( $f10->data_type, 'timestamp', 'Type is "timestamp"' ); |
174 | is( $f10->size, 0, 'Size is "0"' ); |
175 | is( $f10->is_nullable, 1, 'Field can be null' ); |
176 | is( $f10->default_value, undef, 'Default value is undefined' ); |
177 | is( $f10->is_primary_key, 0, 'Field is not PK' ); |
178 | |
179 | my @indices = $table->get_indices; |
180 | is( scalar @indices, 3, 'Right number of indices (3)' ); |
181 | |
182 | my $i1 = shift @indices; |
183 | is( $i1->name, '', 'No name on index' ); |
184 | is( $i1->type, NORMAL, 'Normal index' ); |
185 | is( join(',', $i1->fields ), 'i1', 'Index is on field "i1"' ); |
186 | |
187 | my $i2 = shift @indices; |
188 | is( $i2->name, 'date_idx', 'Name is "date_idx"' ); |
189 | is( $i2->type, NORMAL, 'Normal index' ); |
190 | is( join(',', $i2->fields ), 'date', 'Index is on field "date"' ); |
191 | |
192 | my $i3 = shift @indices; |
193 | is( $i3->name, 'name_idx', 'Name is "name_idx"' ); |
194 | is( $i3->type, NORMAL, 'Normal index' ); |
195 | is( join(',', $i3->fields ), 'name(10)', 'Index is on field "name(10)"' ); |
196 | |
197 | my @constraints = $table->get_constraints; |
198 | is( scalar @constraints, 2, 'Right number of constraints (2)' ); |
199 | |
200 | my $c1 = shift @constraints; |
201 | is( $c1->type, PRIMARY_KEY, 'Constraint is a PK' ); |
202 | is( join(',', $c1->fields), 'check_id', 'Constraint is on "check_id"' ); |
203 | |
204 | my $c2 = shift @constraints; |
205 | is( $c2->type, UNIQUE, 'Constraint is UNIQUE' ); |
206 | is( join(',', $c2->fields), 'date,i1', 'Constraint is on "date, i1"' ); |
251b6ff5 |
207 | } |
208 | |
209 | { |
210 | my $tr = SQL::Translator->new; |
211 | my $data = parse($tr, |
212 | q[ |
213 | CREATE TABLE orders ( |
3a0b6f0a |
214 | order_id integer NOT NULL auto_increment, |
251b6ff5 |
215 | member_id varchar(255), |
216 | billing_address_id int, |
217 | shipping_address_id int, |
218 | credit_card_id int, |
219 | status smallint NOT NULL, |
220 | store_id varchar(255) NOT NULL REFERENCES store, |
221 | tax decimal(8,2), |
222 | shipping_charge decimal(8,2), |
223 | price_paid decimal(8,2), |
224 | PRIMARY KEY (order_id), |
225 | KEY (status), |
226 | KEY (billing_address_id), |
227 | KEY (shipping_address_id), |
228 | KEY (member_id, store_id), |
229 | FOREIGN KEY (status) REFERENCES order_status(id), |
230 | FOREIGN KEY (billing_address_id) REFERENCES address(address_id), |
231 | FOREIGN KEY (shipping_address_id) REFERENCES address(address_id) |
232 | ) TYPE=INNODB; |
2ab84c11 |
233 | |
234 | CREATE TABLE address ( |
235 | address_id int NOT NULL auto_increment, |
236 | recipient varchar(255) NOT NULL, |
237 | address1 varchar(255) NOT NULL, |
238 | address2 varchar(255), |
239 | city varchar(255) NOT NULL, |
240 | state varchar(255) NOT NULL, |
241 | postal_code varchar(255) NOT NULL, |
242 | phone varchar(255), |
243 | PRIMARY KEY (address_id) |
244 | ) TYPE=INNODB; |
251b6ff5 |
245 | ] |
246 | ) or die $tr->error; |
247 | |
2ab84c11 |
248 | my $schema = $tr->schema; |
e3252059 |
249 | is( $schema->is_valid, 1, 'Schema is valid' ); |
2ab84c11 |
250 | my @tables = $schema->get_tables; |
251 | is( scalar @tables, 2, 'Right number of tables (2)' ); |
252 | |
253 | my $t1 = shift @tables; |
254 | is( $t1->name, 'orders', 'Found "orders" table' ); |
255 | |
256 | my @fields = $t1->get_fields; |
257 | is( scalar @fields, 10, 'Right number of fields (10)' ); |
258 | |
259 | my $f1 = shift @fields; |
260 | is( $f1->name, 'order_id', 'First field name is "order_id"' ); |
261 | is( $f1->data_type, 'int', 'Type is "int"' ); |
262 | is( $f1->size, 11, 'Size is "11"' ); |
263 | is( $f1->is_nullable, 0, 'Field cannot be null' ); |
264 | is( $f1->default_value, undef, 'Default value is undefined' ); |
265 | is( $f1->is_primary_key, 1, 'Field is PK' ); |
266 | is( $f1->is_auto_increment, 1, 'Field is auto inc' ); |
267 | |
268 | my $f2 = shift @fields; |
269 | is( $f2->name, 'member_id', 'Second field name is "member_id"' ); |
270 | is( $f2->data_type, 'varchar', 'Type is "varchar"' ); |
271 | is( $f2->size, 255, 'Size is "255"' ); |
272 | is( $f2->is_nullable, 1, 'Field can be null' ); |
273 | is( $f2->default_value, undef, 'Default value is undefined' ); |
274 | |
275 | my $f3 = shift @fields; |
276 | is( $f3->name, 'billing_address_id', |
277 | 'Third field name is "billing_address_id"' ); |
278 | is( $f3->data_type, 'int', 'Type is "int"' ); |
279 | is( $f3->size, 11, 'Size is "11"' ); |
280 | |
281 | my $f4 = shift @fields; |
282 | is( $f4->name, 'shipping_address_id', |
283 | 'Fourth field name is "shipping_address_id"' ); |
284 | is( $f4->data_type, 'int', 'Type is "int"' ); |
285 | is( $f4->size, 11, 'Size is "11"' ); |
286 | |
287 | my $f5 = shift @fields; |
288 | is( $f5->name, 'credit_card_id', 'Fifth field name is "credit_card_id"' ); |
289 | is( $f5->data_type, 'int', 'Type is "int"' ); |
290 | is( $f5->size, 11, 'Size is "11"' ); |
291 | |
292 | my $f6 = shift @fields; |
293 | is( $f6->name, 'status', 'Sixth field name is "status"' ); |
294 | is( $f6->data_type, 'smallint', 'Type is "smallint"' ); |
295 | is( $f6->size, 6, 'Size is "6"' ); |
296 | is( $f6->is_nullable, 0, 'Field cannot be null' ); |
297 | |
298 | my $f7 = shift @fields; |
299 | is( $f7->name, 'store_id', 'Seventh field name is "store_id"' ); |
300 | is( $f7->data_type, 'varchar', 'Type is "varchar"' ); |
301 | is( $f7->size, 255, 'Size is "255"' ); |
302 | is( $f7->is_nullable, 0, 'Field cannot be null' ); |
303 | is( $f7->is_foreign_key, 1, 'Field is a FK' ); |
304 | my $fk_ref = $f7->foreign_key_reference; |
305 | isa_ok( $fk_ref, 'SQL::Translator::Schema::Constraint', 'FK' ); |
306 | is( $fk_ref->reference_table, 'store', 'FK is to "store" table' ); |
307 | |
308 | my $f8 = shift @fields; |
309 | is( $f8->name, 'tax', 'Eighth field name is "tax"' ); |
310 | is( $f8->data_type, 'decimal', 'Type is "decimal"' ); |
311 | is( $f8->size, '8,2', 'Size is "8,2"' ); |
312 | |
313 | my $f9 = shift @fields; |
314 | is( $f9->name, 'shipping_charge', 'Ninth field name is "shipping_charge"' ); |
315 | is( $f9->data_type, 'decimal', 'Type is "decimal"' ); |
316 | is( $f9->size, '8,2', 'Size is "8,2"' ); |
317 | |
318 | my $f10 = shift @fields; |
319 | is( $f10->name, 'price_paid', 'Tenth field name is "price_paid"' ); |
320 | is( $f10->data_type, 'decimal', 'Type is "decimal"' ); |
321 | is( $f10->size, '8,2', 'Size is "8,2"' ); |
322 | |
323 | my @indices = $t1->get_indices; |
324 | is( scalar @indices, 4, 'Right number of indices (4)' ); |
325 | |
326 | my $i1 = shift @indices; |
327 | is( $i1->type, NORMAL, 'First index is normal' ); |
328 | is( join(',', $i1->fields), 'status', 'Index is on "status"' ); |
329 | |
330 | my $i2 = shift @indices; |
331 | is( $i2->type, NORMAL, 'Second index is normal' ); |
332 | is( join(',', $i2->fields), 'billing_address_id', |
333 | 'Index is on "billing_address_id"' ); |
334 | |
335 | my $i3 = shift @indices; |
336 | is( $i3->type, NORMAL, 'Third index is normal' ); |
337 | is( join(',', $i3->fields), 'shipping_address_id', |
338 | 'Index is on "shipping_address_id"' ); |
339 | |
340 | my $i4 = shift @indices; |
341 | is( $i4->type, NORMAL, 'Third index is normal' ); |
342 | is( join(',', $i4->fields), 'member_id,store_id', |
343 | 'Index is on "member_id,store_id"' ); |
344 | |
345 | my @constraints = $t1->get_constraints; |
346 | is( scalar @constraints, 5, 'Right number of constraints (5)' ); |
347 | |
348 | my $c1 = shift @constraints; |
349 | is( $c1->type, PRIMARY_KEY, 'Constraint is a PK' ); |
350 | is( join(',', $c1->fields), 'order_id', 'Constraint is on "order_id"' ); |
351 | |
352 | my $c2 = shift @constraints; |
353 | is( $c2->type, FOREIGN_KEY, 'Constraint is a FK' ); |
354 | is( join(',', $c2->fields), 'status', 'Constraint is on "status"' ); |
355 | is( $c2->reference_table, 'order_status', 'To table "order_status"' ); |
356 | is( join(',', $c2->reference_fields), 'id', 'To field "id"' ); |
357 | |
358 | my $c3 = shift @constraints; |
359 | is( $c3->type, FOREIGN_KEY, 'Constraint is a FK' ); |
360 | is( join(',', $c3->fields), 'billing_address_id', |
361 | 'Constraint is on "billing_address_id"' ); |
362 | is( $c3->reference_table, 'address', 'To table "address"' ); |
363 | is( join(',', $c3->reference_fields), 'address_id', |
364 | 'To field "address_id"' ); |
365 | |
366 | my $c4 = shift @constraints; |
367 | is( $c4->type, FOREIGN_KEY, 'Constraint is a FK' ); |
368 | is( join(',', $c4->fields), 'shipping_address_id', |
369 | 'Constraint is on "shipping_address_id"' ); |
370 | is( $c4->reference_table, 'address', 'To table "address"' ); |
371 | is( join(',', $c4->reference_fields), 'address_id', |
372 | 'To field "address_id"' ); |
373 | |
374 | my $c5 = shift @constraints; |
375 | is( $c5->type, FOREIGN_KEY, 'Constraint is a FK' ); |
376 | is( join(',', $c5->fields), 'store_id', 'Constraint is on "store_id"' ); |
377 | is( $c5->reference_table, 'store', 'To table "store"' ); |
378 | is( join(',', $c5->reference_fields), '', 'No reference fields defined' ); |
379 | |
380 | my $t2 = shift @tables; |
381 | is( $t2->name, 'address', 'Found "address" table' ); |
382 | |
383 | my @t2_fields = $t2->get_fields; |
384 | is( scalar @t2_fields, 8, 'Right number of fields (8)' ); |
251b6ff5 |
385 | } |