9 use DBIC::SqlMakerTest;
12 my $schema = DBICTest->init_schema();
14 # Check the defined unique constraints
16 [ sort $schema->source('CD')->unique_constraint_names ],
17 [ qw/cd_artist_title primary/ ],
18 'CD source has an automatically named unique constraint'
21 [ sort $schema->source('Producer')->unique_constraint_names ],
22 [ qw/primary prod_name/ ],
23 'Producer source has a named unique constraint'
26 [ sort $schema->source('Track')->unique_constraint_names ],
27 [ qw/primary track_cd_position track_cd_title/ ],
28 'Track source has three unique constraints'
31 [ sort $schema->source('Tag')->unique_constraint_names ],
32 [ qw/primary tagid_cd tagid_cd_tag tags_tagid_tag tags_tagid_tag_cd/ ],
33 'Tag source has five unique constraints (from add_unique_constraings)'
37 my $title = 'UNIQUE Constraint';
39 my $cd1 = $schema->resultset('CD')->find_or_create({
45 my $cd2 = $schema->resultset('CD')->find(
50 { key => 'cd_artist_title' }
53 is($cd2->get_column('artist'), $cd1->get_column('artist'), 'find by specific key: artist is correct');
54 is($cd2->title, $cd1->title, 'title is correct');
55 is($cd2->year, $cd1->year, 'year is correct');
57 my $cd3 = $schema->resultset('CD')->find($artistid, $title, { key => 'cd_artist_title' });
59 is($cd3->get_column('artist'), $cd1->get_column('artist'), 'find by specific key, ordered columns: artist is correct');
60 is($cd3->title, $cd1->title, 'title is correct');
61 is($cd3->year, $cd1->year, 'year is correct');
63 my $cd4 = $schema->resultset('CD')->update_or_create(
71 ok(! $cd4->is_changed, 'update_or_create without key: row is clean');
72 is($cd4->cdid, $cd2->cdid, 'cdid is correct');
73 is($cd4->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
74 is($cd4->title, $cd2->title, 'title is correct');
75 is($cd4->year, 2007, 'updated year is correct');
77 my $cd5 = $schema->resultset('CD')->update_or_create(
83 { key => 'cd_artist_title' }
86 ok(! $cd5->is_changed, 'update_or_create by specific key: row is clean');
87 is($cd5->cdid, $cd2->cdid, 'cdid is correct');
88 is($cd5->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
89 is($cd5->title, $cd2->title, 'title is correct');
90 is($cd5->year, 2007, 'updated year is correct');
92 my $cd6 = $schema->resultset('CD')->update_or_create(
102 ok(! $cd6->is_changed, 'update_or_create by PK: row is clean');
103 is($cd6->cdid, $cd2->cdid, 'cdid is correct');
104 is($cd6->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
105 is($cd6->title, $cd2->title, 'title is correct');
106 is($cd6->year, 2005, 'updated year is correct');
108 my $cd7 = $schema->resultset('CD')->find_or_create(
114 { key => 'cd_artist_title' }
117 is($cd7->cdid, $cd1->cdid, 'find_or_create by specific key: cdid is correct');
118 is($cd7->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
119 is($cd7->title, $cd1->title, 'title is correct');
120 is($cd7->year, $cd1->year, 'year is correct');
122 my $artist = $schema->resultset('Artist')->find($artistid);
123 my $cd8 = $artist->find_or_create_related('cds',
128 { key => 'cd_artist_title' }
131 is($cd8->cdid, $cd1->cdid, 'find_or_create related by specific key: cdid is correct');
132 is($cd8->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
133 is($cd8->title, $cd1->title, 'title is correct');
134 is($cd8->year, $cd1->year, 'year is correct');
136 # Add an extra row to potentially confuse the query
137 $schema->resultset('CD')->create ({
142 my $cd9 = $artist->cds->update_or_create(
148 { key => 'cd_artist_title' }
151 ok(! $cd9->is_changed, 'update_or_create by specific key: row is clean');
152 is($cd9->cdid, $cd1->cdid, 'cdid is correct');
153 is($cd9->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
154 is($cd9->title, $cd1->title, 'title is correct');
155 is($cd9->year, 2021, 'year is correct');
157 # Table with two unique constraints, and we're satisying one of them
158 my $track = $schema->resultset('Track')->find(
163 { order_by => 'position' }
166 is($track->get_column('cd'), 1, 'track cd is correct');
167 is($track->get_column('position'), 3, 'track position is correct');
169 # Test a table with a unique constraint but no primary key
170 my $row = $schema->resultset('NoPrimaryKey')->update_or_create(
179 ok(! $row->is_changed, 'update_or_create on table without primary key: row is clean');
180 is($row->foo, 1, 'foo is correct');
181 is($row->bar, 2, 'bar is correct');
182 is($row->baz, 3, 'baz is correct');
184 # Test a unique condition with extra information in the where attr
186 my $artist = $schema->resultset('Artist')->find({ artistid => 1 });
187 my $cd = $artist->cds->find_or_new(
190 title => 'Not The Real Title',
196 ok($cd->in_storage, 'find correctly grepped the key across a relationship');
197 is($cd->cdid, 1, 'cdid is correct');
202 my $cd1 = $schema->resultset('CD')->update_or_new(
205 title => "SuperHits $$",
208 { key => 'cd_artist_title' }
211 is($cd1->in_storage, 0, 'CD is not in storage yet after update_or_new');
213 ok($cd1->in_storage, 'CD got added to strage after update_or_new && insert');
215 my $cd2 = $schema->resultset('CD')->update_or_new(
218 title => "SuperHits $$",
221 { key => 'cd_artist_title' }
223 ok($cd2->in_storage, 'Updating year using update_or_new was successful');
224 is($cd2->id, $cd1->id, 'Got the same CD using update_or_new');
227 # make sure the ident condition is assembled sanely
229 my $artist = $schema->resultset('Artist')->find(1);
232 my $old_debugobj = $schema->storage->debugobj;
233 my $old_debug = $schema->storage->debug;
234 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)),
235 $schema->storage->debug(1);
237 $artist->discard_changes;
242 'SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me WHERE me.artistid = ?',
246 $schema->storage->debug($old_debug);
247 $schema->storage->debugobj($old_debugobj);
252 eval <<'MOD' or die $@;
253 package # hide from PAUSE
254 DBICTest::Schema::UniqueConstraintWarningTest;
256 use base qw/DBIx::Class::Core/;
258 __PACKAGE__->table('dummy');
260 __PACKAGE__->add_column(qw/ foo bar /);
262 __PACKAGE__->add_unique_constraint(
263 constraint1 => [qw/ foo /],
264 constraint2 => [qw/ bar /],
269 } qr/\Qadd_unique_constraint() does not accept multiple constraints, use add_unique_constraints() instead\E/,
270 'add_unique_constraint throws when more than one constraint specified';
272 # make sure NULL is not considered condition-deterministic
273 my $art_rs = $schema->resultset('Artist')->search({}, { order_by => 'artistid' });
274 $art_rs->create ({ artistid => $_ + 640, name => "Outranked $_" }) for (1..2);
277 $art_rs->find ({ artistid => 642, rank => 13, charfield => undef })->name,
279 'Correct artist retrieved with find'
283 $art_rs->search({ charfield => undef })->find ({ artistid => 642, rank => 13 })->name,
285 'Correct artist retrieved with find'