10 my $schema = DBICTest->init_schema();
12 # Check the defined unique constraints
14 [ sort $schema->source('CD')->unique_constraint_names ],
15 [ qw/cd_artist_title primary/ ],
16 'CD source has an automatically named unique constraint'
19 [ sort $schema->source('Producer')->unique_constraint_names ],
20 [ qw/primary prod_name/ ],
21 'Producer source has a named unique constraint'
24 [ sort $schema->source('Track')->unique_constraint_names ],
25 [ qw/primary track_cd_position track_cd_title/ ],
26 'Track source has three unique constraints'
29 [ sort $schema->source('Tag')->unique_constraint_names ],
30 [ qw/primary tagid_cd tagid_cd_tag tags_tagid_tag tags_tagid_tag_cd/ ],
31 'Tag source has five unique constraints (from add_unique_constraings)'
35 my $title = 'UNIQUE Constraint';
37 my $cd1 = $schema->resultset('CD')->find_or_create({
43 my $cd2 = $schema->resultset('CD')->find(
48 { key => 'cd_artist_title' }
51 is($cd2->get_column('artist'), $cd1->get_column('artist'), 'find by specific key: artist is correct');
52 is($cd2->title, $cd1->title, 'title is correct');
53 is($cd2->year, $cd1->year, 'year is correct');
55 my $cd3 = $schema->resultset('CD')->find($artistid, $title, { key => 'cd_artist_title' });
57 is($cd3->get_column('artist'), $cd1->get_column('artist'), 'find by specific key, ordered columns: artist is correct');
58 is($cd3->title, $cd1->title, 'title is correct');
59 is($cd3->year, $cd1->year, 'year is correct');
61 my $cd4 = $schema->resultset('CD')->update_or_create(
69 ok(! $cd4->is_changed, 'update_or_create without key: row is clean');
70 is($cd4->cdid, $cd2->cdid, 'cdid is correct');
71 is($cd4->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
72 is($cd4->title, $cd2->title, 'title is correct');
73 is($cd4->year, 2007, 'updated year is correct');
75 my $cd5 = $schema->resultset('CD')->update_or_create(
81 { key => 'cd_artist_title' }
84 ok(! $cd5->is_changed, 'update_or_create by specific key: row is clean');
85 is($cd5->cdid, $cd2->cdid, 'cdid is correct');
86 is($cd5->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
87 is($cd5->title, $cd2->title, 'title is correct');
88 is($cd5->year, 2007, 'updated year is correct');
90 my $cd6 = $schema->resultset('CD')->update_or_create(
100 ok(! $cd6->is_changed, 'update_or_create by PK: row is clean');
101 is($cd6->cdid, $cd2->cdid, 'cdid is correct');
102 is($cd6->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
103 is($cd6->title, $cd2->title, 'title is correct');
104 is($cd6->year, 2005, 'updated year is correct');
106 my $cd7 = $schema->resultset('CD')->find_or_create(
112 { key => 'cd_artist_title' }
115 is($cd7->cdid, $cd1->cdid, 'find_or_create by specific key: cdid is correct');
116 is($cd7->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
117 is($cd7->title, $cd1->title, 'title is correct');
118 is($cd7->year, $cd1->year, 'year is correct');
120 my $artist = $schema->resultset('Artist')->find($artistid);
121 my $cd8 = $artist->find_or_create_related('cds',
126 { key => 'cd_artist_title' }
129 is($cd8->cdid, $cd1->cdid, 'find_or_create related by specific key: cdid is correct');
130 is($cd8->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
131 is($cd8->title, $cd1->title, 'title is correct');
132 is($cd8->year, $cd1->year, 'year is correct');
134 # Add an extra row to potentially confuse the query
135 $schema->resultset('CD')->create ({
140 my $cd9 = $artist->cds->update_or_create(
146 { key => 'cd_artist_title' }
149 ok(! $cd9->is_changed, 'update_or_create by specific key: row is clean');
150 is($cd9->cdid, $cd1->cdid, 'cdid is correct');
151 is($cd9->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
152 is($cd9->title, $cd1->title, 'title is correct');
153 is($cd9->year, 2021, 'year is correct');
155 # Table with two unique constraints, and we're satisying one of them
156 my $track = $schema->resultset('Track')->find(
161 { order_by => 'position' }
164 is($track->get_column('cd'), 1, 'track cd is correct');
165 is($track->get_column('position'), 3, 'track position is correct');
167 # Test a table with a unique constraint but no primary key
168 my $row = $schema->resultset('NoPrimaryKey')->update_or_create(
177 ok(! $row->is_changed, 'update_or_create on table without primary key: row is clean');
178 is($row->foo, 1, 'foo is correct');
179 is($row->bar, 2, 'bar is correct');
180 is($row->baz, 3, 'baz is correct');
182 # Test a unique condition with extra information in the where attr
184 my $artist = $schema->resultset('Artist')->find({ artistid => 1 });
185 my $cd = $artist->cds->find_or_new(
188 title => 'Not The Real Title',
194 ok($cd->in_storage, 'find correctly grepped the key across a relationship');
195 is($cd->cdid, 1, 'cdid is correct');
200 my $cd1 = $schema->resultset('CD')->update_or_new(
203 title => "SuperHits $$",
206 { key => 'cd_artist_title' }
209 is($cd1->in_storage, 0, 'CD is not in storage yet after update_or_new');
211 ok($cd1->in_storage, 'CD got added to strage after update_or_new && insert');
213 my $cd2 = $schema->resultset('CD')->update_or_new(
216 title => "SuperHits $$",
219 { key => 'cd_artist_title' }
221 ok($cd2->in_storage, 'Updating year using update_or_new was successful');
222 is($cd2->id, $cd1->id, 'Got the same CD using update_or_new');
225 # make sure the ident condition is assembled sanely
227 my $artist = $schema->resultset('Artist')->find(1);
229 $schema->is_executed_sql_bind( sub { $artist->discard_changes }, [
231 'SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me WHERE me.artistid = ?',
232 [ { dbic_colname => "me.artistid", sqlt_datatype => "integer" } => 1 ],
234 ], 'Expected query on discard_changes');
239 eval <<'MOD' or die $@;
240 package # hide from PAUSE
241 DBICTest::Schema::UniqueConstraintWarningTest;
243 use base qw/DBIx::Class::Core/;
245 __PACKAGE__->table('dummy');
247 __PACKAGE__->add_column(qw/ foo bar /);
249 __PACKAGE__->add_unique_constraint(
250 constraint1 => [qw/ foo /],
251 constraint2 => [qw/ bar /],
256 } qr/\Qadd_unique_constraint() does not accept multiple constraints, use add_unique_constraints() instead\E/,
257 'add_unique_constraint throws when more than one constraint specified';
259 # make sure NULL is not considered condition-deterministic
260 my $art_rs = $schema->resultset('Artist')->search({}, { order_by => 'artistid' });
261 $art_rs->create ({ artistid => $_ + 640, name => "Outranked $_" }) for (1..2);
264 $art_rs->find ({ artistid => 642, rank => 13, charfield => undef })->name,
266 'Correct artist retrieved with find'
270 $art_rs->search({ charfield => undef })->find ({ artistid => 642, rank => 13 })->name,
272 'Correct artist retrieved with find'