8 use DBIC::SqlMakerTest;
11 my $schema = DBICTest->init_schema();
13 # Check the defined unique constraints
15 [ sort $schema->source('CD')->unique_constraint_names ],
16 [ qw/cd_artist_title primary/ ],
17 'CD source has an automatically named unique constraint'
20 [ sort $schema->source('Producer')->unique_constraint_names ],
21 [ qw/primary prod_name/ ],
22 'Producer source has a named unique constraint'
25 [ sort $schema->source('Track')->unique_constraint_names ],
26 [ qw/primary track_cd_position track_cd_title/ ],
27 'Track source has three unique constraints'
30 [ sort $schema->source('Tag')->unique_constraint_names ],
31 [ qw/primary tagid_cd tagid_cd_tag tags_tagid_tag tags_tagid_tag_cd/ ],
32 'Tag source has five unique constraints (from add_unique_constraings)'
36 my $title = 'UNIQUE Constraint';
38 my $cd1 = $schema->resultset('CD')->find_or_create({
44 my $cd2 = $schema->resultset('CD')->find(
49 { key => 'cd_artist_title' }
52 is($cd2->get_column('artist'), $cd1->get_column('artist'), 'find by specific key: artist is correct');
53 is($cd2->title, $cd1->title, 'title is correct');
54 is($cd2->year, $cd1->year, 'year is correct');
56 my $cd3 = $schema->resultset('CD')->find($artistid, $title, { key => 'cd_artist_title' });
58 is($cd3->get_column('artist'), $cd1->get_column('artist'), 'find by specific key, ordered columns: artist is correct');
59 is($cd3->title, $cd1->title, 'title is correct');
60 is($cd3->year, $cd1->year, 'year is correct');
62 my $cd4 = $schema->resultset('CD')->update_or_create(
70 ok(! $cd4->is_changed, 'update_or_create without key: row is clean');
71 is($cd4->cdid, $cd2->cdid, 'cdid is correct');
72 is($cd4->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
73 is($cd4->title, $cd2->title, 'title is correct');
74 is($cd4->year, 2007, 'updated year is correct');
76 my $cd5 = $schema->resultset('CD')->update_or_create(
82 { key => 'cd_artist_title' }
85 ok(! $cd5->is_changed, 'update_or_create by specific key: row is clean');
86 is($cd5->cdid, $cd2->cdid, 'cdid is correct');
87 is($cd5->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
88 is($cd5->title, $cd2->title, 'title is correct');
89 is($cd5->year, 2007, 'updated year is correct');
91 my $cd6 = $schema->resultset('CD')->update_or_create(
101 ok(! $cd6->is_changed, 'update_or_create by PK: row is clean');
102 is($cd6->cdid, $cd2->cdid, 'cdid is correct');
103 is($cd6->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
104 is($cd6->title, $cd2->title, 'title is correct');
105 is($cd6->year, 2005, 'updated year is correct');
107 my $cd7 = $schema->resultset('CD')->find_or_create(
113 { key => 'cd_artist_title' }
116 is($cd7->cdid, $cd1->cdid, 'find_or_create by specific key: cdid is correct');
117 is($cd7->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
118 is($cd7->title, $cd1->title, 'title is correct');
119 is($cd7->year, $cd1->year, 'year is correct');
121 my $artist = $schema->resultset('Artist')->find($artistid);
122 my $cd8 = $artist->find_or_create_related('cds',
127 { key => 'cd_artist_title' }
130 is($cd8->cdid, $cd1->cdid, 'find_or_create related by specific key: cdid is correct');
131 is($cd8->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
132 is($cd8->title, $cd1->title, 'title is correct');
133 is($cd8->year, $cd1->year, 'year is correct');
135 my $cd9 = $artist->cds->update_or_create(
141 { key => 'cd_artist_title' }
144 ok(! $cd9->is_changed, 'update_or_create by specific key: row is clean');
145 is($cd9->cdid, $cd1->cdid, 'cdid is correct');
146 is($cd9->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
147 is($cd9->title, $cd1->title, 'title is correct');
148 is($cd9->year, 2021, 'year is correct');
150 # Table with two unique constraints, and we're satisying one of them
151 my $track = $schema->resultset('Track')->find(
156 { order_by => 'position' }
159 is($track->get_column('cd'), 1, 'track cd is correct');
160 is($track->get_column('position'), 3, 'track position is correct');
162 # Test a table with a unique constraint but no primary key
163 my $row = $schema->resultset('NoPrimaryKey')->update_or_create(
172 ok(! $row->is_changed, 'update_or_create on table without primary key: row is clean');
173 is($row->foo, 1, 'foo is correct');
174 is($row->bar, 2, 'bar is correct');
175 is($row->baz, 3, 'baz is correct');
177 # Test a unique condition with extra information in the where attr
179 my $artist = $schema->resultset('Artist')->find({ artistid => 1 });
180 my $cd = $artist->cds->find_or_new(
183 title => 'Not The Real Title',
189 ok($cd->in_storage, 'find correctly grepped the key across a relationship');
190 is($cd->cdid, 1, 'cdid is correct');
195 my $cd1 = $schema->resultset('CD')->update_or_new(
198 title => "SuperHits $$",
201 { key => 'cd_artist_title' }
204 is($cd1->in_storage, 0, 'CD is not in storage yet after update_or_new');
206 ok($cd1->in_storage, 'CD got added to strage after update_or_new && insert');
208 my $cd2 = $schema->resultset('CD')->update_or_new(
211 title => "SuperHits $$",
214 { key => 'cd_artist_title' }
216 ok($cd2->in_storage, 'Updating year using update_or_new was successful');
217 is($cd2->id, $cd1->id, 'Got the same CD using update_or_new');
220 # make sure the ident condition is assembled sanely
222 my $artist = $schema->resultset('Artist')->next;
225 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)),
226 $schema->storage->debug(1);
228 $artist->discard_changes;
233 'SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me WHERE me.artistid = ?',
237 $schema->storage->debug(0);
238 $schema->storage->debugobj(undef);
243 eval <<'MOD' or die $@;
244 package # hide from PAUSE
245 DBICTest::Schema::UniqueConstraintWarningTest;
247 use base qw/DBIx::Class::Core/;
249 __PACKAGE__->table('dummy');
251 __PACKAGE__->add_column(qw/ foo bar /);
253 __PACKAGE__->add_unique_constraint(
254 constraint1 => [qw/ foo /],
255 constraint2 => [qw/ bar /],
260 } qr/\Qadd_unique_constraint() does not accept multiple constraints, use add_unique_constraints() instead\E/,
261 'add_unique_constraint throws when more than one constraint specified';