10 use SQL::Translator::Schema::Constants;
12 require_ok( 'SQL::Translator' );
13 require_ok( 'SQL::Translator::Schema' );
19 my $schema = SQL::Translator::Schema->new(
23 isa_ok( $schema, 'SQL::Translator::Schema' );
25 is( $schema->name, 'foo', 'Schema name is "foo"' );
26 is( $schema->name('bar'), 'bar', 'Schema name changed to "bar"' );
28 is( $schema->database, 'MySQL', 'Schema database is "MySQL"' );
29 is( $schema->database('PostgreSQL'), 'PostgreSQL',
30 'Schema database changed to "PostgreSQL"' );
32 is( $schema->is_valid, undef, 'Schema not valid...' );
33 like( $schema->error, qr/no tables/i, '...because there are no tables' );
38 my $foo_table = $schema->add_table(name => 'foo') or warn $schema->error;
39 isa_ok( $foo_table, 'SQL::Translator::Schema::Table', 'Table "foo"' );
41 my $bar_table = SQL::Translator::Schema::Table->new( name => 'bar' ) or
42 warn SQL::Translator::Schema::Table->error;
43 $bar_table = $schema->add_table( $bar_table );
44 isa_ok( $bar_table, 'SQL::Translator::Schema::Table', 'Table "bar"' );
45 is( $bar_table->name, 'bar', 'Add table "bar"' );
47 $schema = $bar_table->schema( $schema );
48 isa_ok( $schema, 'SQL::Translator::Schema', 'Schema' );
50 is( $bar_table->name('foo'), undef,
51 q[Can't change name of table "bar" to "foo"...]);
52 like( $bar_table->error, qr/can't use table name/i,
53 q[...because "foo" exists] );
55 my $redundant_table = $schema->add_table(name => 'foo');
56 is( $redundant_table, undef, qq[Can't create another "foo" table...] );
57 like( $schema->error, qr/can't use table name/i,
58 '... because "foo" exists' );
60 $redundant_table = $schema->add_table(name => '');
61 is( $redundant_table, undef, qq[Can't add an anonymous table...] );
62 like( $schema->error, qr/No table name/i,
63 '... because it has no name ' );
65 $redundant_table = SQL::Translator::Schema::Table->new(name => '');
66 is( $redundant_table, undef, qq[Can't create an anonymous table] );
67 like( SQL::Translator::Schema::Table->error, qr/No table name/i,
68 '... because it has no name ' );
71 # $schema-> drop_table
73 my $dropped_table = $schema->drop_table($foo_table->name, cascade => 1);
74 isa_ok($dropped_table, 'SQL::Translator::Schema::Table', 'Dropped table "foo"' );
75 $schema->add_table($foo_table);
76 my $dropped_table2 = $schema->drop_table($foo_table, cascade => 1);
77 isa_ok($dropped_table2, 'SQL::Translator::Schema::Table', 'Dropped table "foo" by object' );
78 my $dropped_table3 = $schema->drop_table($foo_table->name, cascade => 1);
79 like( $schema->error, qr/doesn't exist/, qq[Can't drop non-existant table "foo"] );
81 $schema->add_table($foo_table);
85 is( $foo_table->name, 'foo', 'Table name is "foo"' );
86 is( "$foo_table", 'foo', 'Table stringifies to "foo"' );
87 is( $foo_table->is_valid, undef, 'Table "foo" is not yet valid' );
89 my $fields = $foo_table->get_fields;
90 is( scalar @{ $fields || [] }, 0, 'Table "foo" has no fields' );
91 like( $foo_table->error, qr/no fields/i, 'Error for no fields' );
93 is( $foo_table->comments, undef, 'No comments' );
98 my $person_table = $schema->add_table(
102 is( $person_table->name, 'person', 'Table name is "person"' );
103 is( $person_table->is_valid, undef, 'Table is not yet valid' );
104 is( $person_table->comments, 'foo', 'Comments = "foo"' );
105 is( join(',', $person_table->comments('bar')), 'foo,bar',
106 'Table comments = "foo,bar"' );
107 is( $person_table->comments, "foo\nbar", 'Table comments = "foo,bar"' );
112 my $f1 = $person_table->add_field(name => 'foo') or
113 warn $person_table->error;
114 isa_ok( $f1, 'SQL::Translator::Schema::Field', 'Field' );
115 is( $f1->name, 'foo', 'Field name is "foo"' );
116 is( $f1->full_name, 'person.foo', 'Field full_name is "person.foo"' );
117 is( "$f1", 'foo', 'Field stringifies to "foo"' );
118 is( $f1->data_type, '', 'Field data type is blank' );
119 is( $f1->size, 0, 'Field size is "0"' );
120 is( $f1->is_primary_key, '0', 'Field is_primary_key is false' );
121 is( $f1->is_nullable, 1, 'Field can be NULL' );
122 is( $f1->default_value, undef, 'Field default is undefined' );
123 is( $f1->comments, '', 'No comments' );
124 is( $f1->table, 'person', 'Field table is person' );
125 is( $f1->schema->database, 'PostgreSQL', 'Field schema shortcut works' );
127 my $f2 = SQL::Translator::Schema::Field->new (
130 ) or warn SQL::Translator::Schema::Field->error;
131 $f2 = $person_table->add_field( $f2 );
132 isa_ok( $f1, 'SQL::Translator::Schema::Field', 'f2' );
133 is( $f2->name, 'f2', 'Add field "f2"' );
134 is( $f2->is_nullable(0), 0, 'Field cannot be NULL' );
135 is( $f2->is_nullable(''), 0, 'Field cannot be NULL' );
136 is( $f2->is_nullable('0'), 0, 'Field cannot be NULL' );
137 is( $f2->default_value(''), '', 'Field default is empty string' );
138 is( $f2->comments, 'foo', 'Field comment = "foo"' );
139 is( join(',', $f2->comments('bar')), 'foo,bar',
140 'Field comment = "foo,bar"' );
141 is( $f2->comments, "foo\nbar", 'Field comment = "foo,bar"' );
143 $person_table = $f2->table( $person_table );
144 isa_ok( $person_table, 'SQL::Translator::Schema::Table', 'person_table' );
146 is( $f2->name('foo'), undef, q[Can't set field name of "f2" to "foo"...] );
147 like( $f2->error, qr/can't use field name/i, '...because name exists' );
149 my $redundant_field = $person_table->add_field(name => 'f2');
150 is( $redundant_field, undef, qq[Didn't create another "f2" field...] );
151 like( $person_table->error, qr/can't use field/i,
152 '... because it exists' );
154 $redundant_field = $person_table->add_field(name => '');
155 is( $redundant_field, undef, qq[Didn't add a "" field...] );
156 like( $person_table->error, qr/No field name/i,
157 '... because it has no name' );
159 $redundant_field = SQL::Translator::Schema::Field->new(name => '');
160 is( $redundant_field, undef, qq[Didn't create a "" field...] );
161 like( SQL::Translator::Schema::Field->error, qr/No field name/i,
162 '... because it has no name' );
164 my @fields = $person_table->get_fields;
165 is( scalar @fields, 2, 'Table "foo" has 2 fields' );
167 is( $fields[0]->name, 'foo', 'First field is "foo"' );
168 is( $fields[1]->name, 'f2', 'Second field is "f2"' );
169 is( join(",",$person_table->field_names), 'foo,f2',
170 'field_names is "foo,f2"' );
172 my $ci_field = $person_table->get_field('FOO', 'case_insensitive');
173 is( $ci_field->name, 'foo', 'Got field case-insensitively' );
175 # $table-> drop_field
177 my $dropped_field = $person_table->drop_field($f2->name, cascade => 1);
178 isa_ok($dropped_field, 'SQL::Translator::Schema::Field', 'Dropped field "f2"' );
179 $person_table->add_field($f2);
180 my $dropped_field2 = $person_table->drop_field($f2, cascade => 1);
181 isa_ok($dropped_field2, 'SQL::Translator::Schema::Field', 'Dropped field "f2" by object' );
182 my $dropped_field3 = $person_table->drop_field($f2->name, cascade => 1);
183 like( $person_table->error, qr/doesn't exist/, qq[Can't drop non-existant field "f2"] );
185 $person_table->add_field($f2);
190 is( $f1->name('person_name'), 'person_name',
191 'Field name is "person_name"' );
192 is( $f1->data_type('varchar'), 'varchar', 'Field data type is "varchar"' );
193 is( $f1->size('30'), '30', 'Field size is "30"' );
194 is( $f1->is_primary_key(0), '0', 'Field is_primary_key is negative' );
196 $f1->extra( foo => 'bar' );
197 $f1->extra( { baz => 'quux' } );
198 my %extra = $f1->extra;
199 is( $extra{'foo'}, 'bar', 'Field extra "foo" is "bar"' );
200 is( $extra{'baz'}, 'quux', 'Field extra "baz" is "quux"' );
203 # New field with args
205 my $age = $person_table->add_field(
207 data_type => 'float',
210 is( $age->name, 'age', 'Field name is "age"' );
211 is( $age->data_type, 'float', 'Field data type is "float"' );
212 is( $age->size, '10,2', 'Field size is "10,2"' );
213 is( $age->size(10,2), '10,2', 'Field size still "10,2"' );
214 is( $age->size([10,2]), '10,2', 'Field size still "10,2"' );
215 is( $age->size(qw[ 10 2 ]), '10,2', 'Field size still "10,2"' );
216 is( join(':', $age->size), '10:2', 'Field size returns array' );
221 my @indices = $person_table->get_indices;
222 is( scalar @indices, 0, 'No indices' );
223 like( $person_table->error, qr/no indices/i, 'Error for no indices' );
224 my $index1 = $person_table->add_index( name => "foo" )
225 or warn $person_table->error;
226 isa_ok( $index1, 'SQL::Translator::Schema::Index', 'Index' );
227 is( $index1->name, 'foo', 'Index name is "foo"' );
229 is( $index1->is_valid, undef, 'Index name is not valid...' );
230 like( $index1->error, qr/no fields/i, '...because it has no fields' );
232 is( join(':', $index1->fields('foo,bar')), 'foo:bar',
233 'Index accepts fields');
235 is( $index1->is_valid, undef, 'Index name is not valid...' );
236 like( $index1->error, qr/does not exist in table/i,
237 '...because it used fields not in the table' );
239 is( join(':', $index1->fields(qw[foo age])), 'foo:age',
240 'Index accepts fields');
241 is( $index1->is_valid, 1, 'Index name is now valid' );
243 is( $index1->type, NORMAL, 'Index type is "normal"' );
245 my $index2 = SQL::Translator::Schema::Index->new( name => "bar" )
246 or warn SQL::Translator::Schema::Index->error;
247 $index2 = $person_table->add_index( $index2 );
248 isa_ok( $index2, 'SQL::Translator::Schema::Index', 'Index' );
249 is( $index2->name, 'bar', 'Index name is "bar"' );
251 my $indices = $person_table->get_indices;
252 is( scalar @$indices, 2, 'Two indices' );
253 is( $indices->[0]->name, 'foo', '"foo" index' );
254 is( $indices->[1]->name, 'bar', '"bar" index' );
257 # $table-> drop_index
259 my $dropped_index = $person_table->drop_index($index1->name);
260 isa_ok($dropped_index, 'SQL::Translator::Schema::Index', 'Dropped index "foo"' );
261 $person_table->add_index($index1);
262 my $dropped_index2 = $person_table->drop_index($index1);
263 isa_ok($dropped_index2, 'SQL::Translator::Schema::Index', 'Dropped index "foo" by object' );
264 is($dropped_index2->name, $index1->name, 'Dropped correct index "foo"');
265 my $dropped_index3 = $person_table->drop_index($index1->name);
266 like( $person_table->error, qr/doesn't exist/, qq[Can't drop non-existant index "foo"] );
268 $person_table->add_index($index1);
273 my @constraints = $person_table->get_constraints;
274 is( scalar @constraints, 0, 'No constraints' );
275 like( $person_table->error, qr/no constraints/i,
276 'Error for no constraints' );
277 my $constraint1 = $person_table->add_constraint( name => 'foo' )
278 or warn $person_table->error;
279 isa_ok( $constraint1, 'SQL::Translator::Schema::Constraint', 'Constraint' );
280 is( $constraint1->name, 'foo', 'Constraint name is "foo"' );
282 $fields = join(',', $constraint1->fields('age') );
283 is( $fields, 'age', 'Constraint field = "age"' );
285 $fields = $constraint1->fields;
286 ok( ref $fields[0] && $fields[0]->isa("SQL::Translator::Schema::Field"),
287 'Constraint fields returns a SQL::Translator::Schema::Field' );
289 $fields = join(',', $constraint1->fields('age,age') );
290 is( $fields, 'age', 'Constraint field = "age"' );
292 $fields = join(',', $constraint1->fields('age', 'name') );
293 is( $fields, 'age,name', 'Constraint field = "age,name"' );
295 $fields = join(',', $constraint1->fields( 'age,name,age' ) );
296 is( $fields, 'age,name', 'Constraint field = "age,name"' );
298 $fields = join(',', $constraint1->fields( 'age, name' ) );
299 is( $fields, 'age,name', 'Constraint field = "age,name"' );
301 $fields = join(',', $constraint1->fields( [ 'age', 'name' ] ) );
302 is( $fields, 'age,name', 'Constraint field = "age,name"' );
304 $fields = join(',', $constraint1->fields( qw[ age name ] ) );
305 is( $fields, 'age,name', 'Constraint field = "age,name"' );
307 $fields = join(',', $constraint1->field_names );
308 is( $fields, 'age,name', 'Constraint field_names = "age,name"' );
310 is( $constraint1->match_type, '', 'Constraint match type is empty' );
311 is( $constraint1->match_type('foo'), undef,
312 'Constraint match type rejects bad arg...' );
313 like( $constraint1->error, qr/invalid match type/i,
314 '...because it is invalid');
315 is( $constraint1->match_type('FULL'), 'full',
316 'Constraint match type = "full"' );
318 my $constraint2 = SQL::Translator::Schema::Constraint->new( name => 'bar' );
319 $constraint2 = $person_table->add_constraint( $constraint2 );
320 isa_ok( $constraint2, 'SQL::Translator::Schema::Constraint', 'Constraint' );
321 is( $constraint2->name, 'bar', 'Constraint name is "bar"' );
323 my $constraint3 = $person_table->add_constraint(
325 expression => 'foo bar',
326 ) or die $person_table->error;
327 isa_ok( $constraint3, 'SQL::Translator::Schema::Constraint', 'Constraint' );
328 is( $constraint3->type, CHECK_C, 'Constraint type is "CHECK"' );
329 is( $constraint3->expression, 'foo bar',
330 'Constraint expression is "foo bar"' );
332 my $constraints = $person_table->get_constraints;
333 is( scalar @$constraints, 3, 'Three constraints' );
334 is( $constraints->[0]->name, 'foo', '"foo" constraint' );
335 is( $constraints->[1]->name, 'bar', '"bar" constraint' );
338 # $table-> drop_constraint
340 my $dropped_con = $person_table->drop_constraint($constraint1->name);
341 isa_ok($dropped_con, 'SQL::Translator::Schema::Constraint', 'Dropped constraint "foo"' );
342 $person_table->add_constraint($constraint1);
343 my $dropped_con2 = $person_table->drop_constraint($constraint1);
344 isa_ok($dropped_con2, 'SQL::Translator::Schema::Constraint', 'Dropped constraint "foo" by object' );
345 is($dropped_con2->name, $constraint1->name, 'Dropped correct constraint "foo"');
346 my $dropped_con3 = $person_table->drop_constraint($constraint1->name);
347 like( $person_table->error, qr/doesn't exist/, qq[Can't drop non-existant constraint "foo"] );
349 $person_table->add_constraint($constraint1);
354 my $view = $schema->add_view( name => 'view1' ) or warn $schema->error;
355 isa_ok( $view, 'SQL::Translator::Schema::View', 'View' );
356 my $view_sql = 'select * from table';
357 is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' );
359 my $view2 = SQL::Translator::Schema::View->new(name => 'view2') or
360 warn SQL::Translator::Schema::View->error;
361 my $check_view = $schema->add_view( $view2 );
362 is( $check_view->name, 'view2', 'Add view "view2"' );
364 my $redundant_view = $schema->add_view(name => 'view2');
365 is( $redundant_view, undef, qq[Didn't create another "view2" view...] );
366 like( $schema->error, qr/can't create view/i, '... because it exists' );
369 # $schema-> drop_view
371 my $dropped_view = $schema->drop_view($view->name);
372 isa_ok($dropped_view, 'SQL::Translator::Schema::View', 'Dropped view "view1"' );
373 $schema->add_view($view);
374 my $dropped_view2 = $schema->drop_view($view);
375 isa_ok($dropped_view2, 'SQL::Translator::Schema::View', 'Dropped view "view1" by object' );
376 is($dropped_view2->name, $view->name, 'Dropped correct view "view1"');
377 my $dropped_view3 = $schema->drop_view($view->name);
378 like( $schema->error, qr/doesn't exist/, qq[Can't drop non-existant view "view1"] );
380 $schema->add_view($view);
385 my $bad_table = $schema->get_table;
386 like( $schema->error, qr/no table/i, 'Error on no arg to get_table' );
388 $bad_table = $schema->get_table('baz');
389 like( $schema->error, qr/does not exist/i,
390 'Error on bad arg to get_table' );
392 my $bad_view = $schema->get_view;
393 like( $schema->error, qr/no view/i, 'Error on no arg to get_view' );
395 $bad_view = $schema->get_view('bar');
396 like( $schema->error, qr/does not exist/i,
397 'Error on bad arg to get_view' );
399 my $good_table = $schema->get_table('foo');
400 isa_ok( $good_table, 'SQL::Translator::Schema::Table', 'Table "foo"' );
402 my $good_view = $schema->get_view('view1');
403 isa_ok( $good_view, 'SQL::Translator::Schema::View', 'View "view1"' );
404 is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' );
409 my @tables = $schema->get_tables;
410 is( scalar @tables, 3, 'Found 2 tables' );
412 my @views = $schema->get_views;
413 is( scalar @views, 2, 'Found 1 view' );
418 # Test ability to introspect some values
421 my $s = SQL::Translator::Schema->new(
423 database => 'PostgreSQL',
425 my $t = $s->add_table( name => 'person' ) or warn $s->error;
426 my $f = $t->add_field( name => 'person_id' ) or warn $t->error;
427 $f->data_type('serial');
429 my $c = $t->add_constraint(
431 fields => 'person_id',
434 is( $f->is_primary_key, 1, 'Field is PK' );
435 is( $f->is_auto_increment, 1, 'Field is auto inc' );
439 # FK constraint validity
442 my $s = SQL::Translator::Schema->new;
443 my $t = $s->add_table( name => 'person' ) or warn $s->error;
444 my $c = $t->add_constraint or warn $t->error;
446 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
447 like( $c->error, qr/no type/i, '...because it has no type' );
449 is( $c->type( FOREIGN_KEY ), FOREIGN_KEY, 'Constraint type now a FK' );
451 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
452 like( $c->error, qr/no fields/i, '...because it has no fields' );
454 is( join('', $c->fields('foo')), 'foo', 'Fields now = "foo"' );
456 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
457 like( $c->error, qr/non-existent field/i,
458 q[...because field "foo" doesn't exist] );
460 my $fk = $t->add_field( name => 'pet_id' );
461 is( $fk->name, 'pet_id', 'Added field "pet_id"' );
462 is( join('', $c->fields('pet_id')), 'pet_id', 'Fields now = "pet_id"' );
464 $t->add_field( name => 'f1' );
465 $t->add_field( name => 'f2' );
466 is( join(',', $c->fields('f1,f2')), 'f1,f2', 'Fields now = "f1,f2"' );
467 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
468 like( $c->error, qr/only one field/i,
469 q[...because too many fields for FK] );
473 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
474 like( $c->error, qr/no reference table/i,
475 q[...because there's no reference table] );
477 is( $c->reference_table('foo'), 'foo', 'Reference table now = "foo"' );
478 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
479 like( $c->error, qr/no table named/i,
480 q[...because reference table "foo" doesn't exist] );
482 my $t2 = $s->add_table( name => 'pet' );
483 is( $t2->name, 'pet', 'Added "pet" table' );
485 is( $c->reference_table('pet'), 'pet', 'Reference table now = "pet"' );
487 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
488 like( $c->error, qr/no reference fields/i,
489 q[...because there're no reference fields]);
491 is( join('', $c->reference_fields('pet_id')), 'pet_id',
492 'Reference fields = "pet_id"' );
494 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
495 like( $c->error, qr/non-existent field/i,
496 q[...because there's no "pet_id" field in "pet"]);
498 my $pet_id = $t2->add_field( name => 'pet_id' );
499 is( $pet_id->name, 'pet_id', 'Added field "pet_id"' );
501 is( $c->is_valid, 1, 'Constraint now valid' );
505 # $table->primary_key test
508 my $s = SQL::Translator::Schema->new;
509 my $t = $s->add_table( name => 'person' );
511 is( $t->primary_key, undef, 'No primary key' );
513 is( $t->primary_key('person_id'), undef,
514 q[Can't make PK on "person_id"...] );
515 like( $t->error, qr/invalid field/i, "...because it doesn't exist" );
517 $t->add_field( name => 'person_id' );
518 my $c = $t->primary_key('person_id');
520 isa_ok( $c, 'SQL::Translator::Schema::Constraint', 'Constraint' );
522 is( join('', $c->fields), 'person_id', 'Constraint now on "person_id"' );
524 $t->add_field( name => 'name' );
525 $c = $t->primary_key('name');
526 is( join(',', $c->fields), 'person_id,name',
527 'Constraint now on "person_id" and "name"' );
529 is( scalar @{ $t->get_constraints }, 1, 'Found 1 constraint' );
536 my $s = SQL::Translator::Schema->new;
537 my $t1 = $s->add_table( name => 'person' );
538 my $t2 = $s->add_table( name => 'pet' );
539 $t1->add_field( name => 'id' );
540 my $c1 = $t1->primary_key( 'id' ) or warn $t1->error;
541 is( $c1->type, PRIMARY_KEY, 'Made "person_id" PK on "person"' );
543 $t2->add_field( name => 'person_id' );
544 my $c2 = $t2->add_constraint(
546 fields => 'person_id',
547 reference_table => 'person',
550 is( join('', $c2->reference_fields), 'id', 'FK found PK "person.id"' );
557 my $s = SQL::Translator::Schema->new( name => 'ViewTest' );
558 my $name = 'foo_view';
559 my $sql = 'select name, age from person';
560 my $fields = 'name, age';
561 my $v = $s->add_view(
568 isa_ok( $v, 'SQL::Translator::Schema::View', 'View' );
569 isa_ok( $v->schema, 'SQL::Translator::Schema', 'Schema' );
570 is( $v->schema->name, 'ViewTest', qq[Schema name is "'ViewTest'"] );
571 is( $v->name, $name, qq[Name is "$name"] );
572 is( $v->sql, $sql, qq[Name is "$sql"] );
573 is( join(':', $v->fields), 'name:age', qq[Fields are "$fields"] );
575 my @views = $s->get_views;
576 is( scalar @views, 1, 'Number of views is 1' );
578 my $v1 = $s->get_view( $name );
579 isa_ok( $v1, 'SQL::Translator::Schema::View', 'View' );
580 is( $v1->name, $name, qq[Name is "$name"] );
587 my $s = SQL::Translator::Schema->new(name => 'TrigTest');
588 $s->add_table(name=>'foo') or die "Couldn't create table: ", $s->error;
589 my $name = 'foo_trigger';
590 my $perform_action_when = 'after';
591 my $database_events = 'insert';
592 my $on_table = 'foo';
593 my $action = 'update modified=timestamp();';
594 my $t = $s->add_trigger(
596 perform_action_when => $perform_action_when,
597 database_events => $database_events,
598 on_table => $on_table,
602 isa_ok( $t, 'SQL::Translator::Schema::Trigger', 'Trigger' );
603 isa_ok( $t->schema, 'SQL::Translator::Schema', 'Schema' );
604 is( $t->schema->name, 'TrigTest', qq[Schema name is "'TrigTest'"] );
605 is( $t->name, $name, qq[Name is "$name"] );
606 is( $t->perform_action_when, $perform_action_when,
607 qq[Perform action when is "$perform_action_when"] );
608 is( join(',', $t->database_events), $database_events,
609 qq[Database event is "$database_events"] );
610 isa_ok( $t->table, 'SQL::Translator::Schema::Table', qq[table is a Table"] );
611 is( $t->action, $action, qq[Action is "$action"] );
613 my @triggs = $s->get_triggers;
614 is( scalar @triggs, 1, 'Number of triggers is 1' );
616 my $t1 = $s->get_trigger( $name );
617 isa_ok( $t1, 'SQL::Translator::Schema::Trigger', 'Trigger' );
618 is( $t1->name, $name, qq[Name is "$name"] );
622 my $s2 = SQL::Translator::Schema->new(name => 'TrigTest2');
623 $s2->add_table(name=>'foo') or die "Couldn't create table: ", $s2->error;
624 my $t2 = $s2->add_trigger(
625 name => 'foo_trigger',
626 perform_action_when => 'after',
627 database_events => [qw/insert update/],
629 action => 'update modified=timestamp();',
631 isa_ok( $t2, 'SQL::Translator::Schema::Trigger', 'Trigger' );
632 isa_ok( $t2->schema, 'SQL::Translator::Schema', 'Schema' );
633 is( $t2->schema->name, 'TrigTest2', qq[Schema name is "'TrigTest2'"] );
634 is( $t2->name, 'foo_trigger', qq[Name is "foo_trigger"] );
636 [$t2->database_events],
638 "Database events are [qw/insert update/] "
640 isa_ok($t2->database_events,'ARRAY','Database events');
643 # Trigger equal tests
648 'Compare two Triggers with database_event and database_events'
651 $t1->database_events($database_events);
652 $t2->database_events($database_events);
653 is($t1->equals($t2),1,'Compare two Triggers with database_event');
655 $t2->database_events('');
656 $t1->database_events([qw/update insert/]);
657 $t2->database_events([qw/insert update/]);
658 is($t1->equals($t2),1,'Compare two Triggers with database_events');
661 # $schema-> drop_trigger
663 my $dropped_trig = $s->drop_trigger($t->name);
664 isa_ok($dropped_trig, 'SQL::Translator::Schema::Trigger', 'Dropped trigger "foo_trigger"' );
666 my $dropped_trig2 = $s->drop_trigger($t);
667 isa_ok($dropped_trig2, 'SQL::Translator::Schema::Trigger', 'Dropped trigger "foo_trigger" by object' );
668 is($dropped_trig2->name, $t->name, 'Dropped correct trigger "foo_trigger"');
669 my $dropped_trig3 = $s->drop_trigger($t->name);
670 like( $s->error, qr/doesn't exist/, qq[Can't drop non-existant trigger "foo_trigger"] );
679 my $s = SQL::Translator::Schema->new( name => 'ProcTest' );
680 my $name = 'foo_proc';
681 my $sql = 'select foo from bar';
682 my $parameters = 'foo, bar';
684 my $comments = 'Go Sox!';
685 my $p = $s->add_procedure(
688 parameters => $parameters,
690 comments => $comments,
693 isa_ok( $p, 'SQL::Translator::Schema::Procedure', 'Procedure' );
694 isa_ok( $p->schema, 'SQL::Translator::Schema', 'Schema' );
695 is( $p->schema->name, 'ProcTest', qq[Schema name is "'ProcTest'"] );
696 is( $p->name, $name, qq[Name is "$name"] );
697 is( $p->sql, $sql, qq[SQL is "$sql"] );
698 is( join(',', $p->parameters), 'foo,bar', qq[Params = 'foo,bar'] );
699 is( $p->comments, $comments, qq[Comments = "$comments"] );
701 my @procs = $s->get_procedures;
702 is( scalar @procs, 1, 'Number of procedures is 1' );
704 my $p1 = $s->get_procedure( $name );
705 isa_ok( $p1, 'SQL::Translator::Schema::Procedure', 'Procedure' );
706 is( $p1->name, $name, qq[Name is "$name"] );
709 # $schema-> drop_procedure
711 my $dropped_proc = $s->drop_procedure($p->name);
712 isa_ok($dropped_proc, 'SQL::Translator::Schema::Procedure', 'Dropped procedure "foo_proc"' );
713 $s->add_procedure($p);
714 my $dropped_proc2 = $s->drop_procedure($p);
715 isa_ok($dropped_proc2, 'SQL::Translator::Schema::Procedure', 'Dropped procedure "foo_proc" by object' );
716 is($dropped_proc2->name, $p->name, 'Dropped correct procedure "foo_proc"');
717 my $dropped_proc3 = $s->drop_procedure($p->name);
718 like( $s->error, qr/doesn't exist/, qq[Can't drop non-existant procedure "foo_proc"] );
720 $s->add_procedure($p);
727 my $s = SQL::Translator::Schema->new;
728 my $t = $s->add_table( name => 'person' );
729 my $f3 = $t->add_field( name => 'age', order => 3 );
730 my $f1 = $t->add_field( name => 'person_id', order => 1 );
731 my $f2 = $t->add_field( name => 'name', order => 2 );
732 my $f4 = $t->add_field( name => 'gender' );
733 my $f5 = $t->add_field( name => 'alias' );
735 is( $f1->order, 1, 'Field order is passed, order is 1' );
736 is( $f2->order, 2, 'Field order is passed, order is 2' );
737 is( $f3->order, 3, 'Field order is passed, order is 3' );
738 is( $f4->order, 4, 'Field order is not passed, order is 4' );
739 is( $f5->order, 5, 'Field order is not passed, order is 5' );
741 my $t2 = $s->add_table( name => 'place' );
742 $f2 = $t2->add_field( name => 'name', order => 2 );
744 throws_ok { my $f22 = $t2->add_field( name => 'name2', order => 2 ) }
745 qr/\QRequested order '2' for column 'name2' conflicts with already existing column 'name'/;
747 throws_ok { $f1 = $t2->add_field( name => 'location' ) }
748 qr/field order incomplete/;
756 my $s = SQL::Translator::Schema->new;
757 my $t1 = $s->add_table( name => 'person' );
758 $t1->add_field( name => 'id' );
759 $t1->primary_key( 'id' );
760 $t1->add_field( name => 'name' );
762 ok( $t1->is_data, 'Person table has data' );
763 ok( !$t1->is_trivial_link, 'Person table is not trivial' );
765 my $t2 = $s->add_table( name => 'pet' );
766 $t2->add_field( name => 'id' );
767 $t2->primary_key( 'id' );
768 $t2->add_field( name => 'name' );
770 ok( $t2->is_data, 'Pet table has data' );
771 ok( !$t1->is_trivial_link, 'Pet table is trivial' );
773 my $t3 = $s->add_table( name => 'person_pet' );
774 $t3->add_field( name => 'id' );
775 my $f1 = $t3->add_field( name => 'person_id' );
776 my $f2 = $t3->add_field( name => 'pet_id' );
777 $t3->primary_key( 'id' );
781 fields => 'person_id',
782 reference_table => $t1,
788 reference_table => $t2,
791 ok( $f1->is_foreign_key, "person_id is foreign key" );
792 ok( $f2->is_foreign_key, "pet_id is foreign key" );
794 ok( !$t3->is_data, 'Link table has no data' );
795 ok( $t3->is_trivial_link, 'Link table is trivial' );
796 is( $t3->can_link($t1, $t2)->[0], 'one2one', 'Link table can link' );
798 my $t4 = $s->add_table( name => 'fans' );
799 my $f3 = $t4->add_field( name => 'fan_id' );
800 my $f4 = $t4->add_field( name => 'idol_id' );
801 $t4->primary_key( 'fan_id', 'idol_id' );
805 name => 'fan_fan_fk',
807 reference_table => $t1,
812 name => 'fan_idol_fk',
814 reference_table => $t1,
817 ok( $f3->is_foreign_key, "fan_id is foreign key" );
818 ok( $f4->is_foreign_key, "idol_id is foreign key" );
820 ok( !$t4->is_data, 'Self-link table has no data' );
821 ok( !$t4->is_trivial_link, 'Self-link table is not trivial' );
822 is( $t4->can_link($t1, $t1)->[0], 'many2many', 'Self-link table can link' );
823 ok( !$t4->can_link($t1, $t2)->[0], 'Self-link table can\'t link other' );