Lines 155-167
sub _buildColumnValues {
Link Here
|
155 |
|
155 |
|
156 |
my $col_values; |
156 |
my $col_values; |
157 |
my @columns = $self->schema->source($source)->columns; |
157 |
my @columns = $self->schema->source($source)->columns; |
158 |
for my $col_name( @columns ) { |
158 |
my %unique_constraints = $self->schema->source($source)->unique_constraints(); |
159 |
my $col_value = $self->_buildColumnValue({ |
159 |
|
160 |
source => $source, |
160 |
my $values_ok = 0; |
161 |
column_name => $col_name, |
161 |
my $at_least_one_constraint_failed; |
162 |
value => $value, |
162 |
|
163 |
}); |
163 |
while ( not $values_ok ) { |
164 |
$col_values->{$col_name} = $col_value if( defined( $col_value ) ); |
164 |
|
|
|
165 |
$at_least_one_constraint_failed = 0; |
166 |
# generate random values |
167 |
for my $col_name( @columns ) { |
168 |
my $col_value = $self->_buildColumnValue({ |
169 |
source => $source, |
170 |
column_name => $col_name, |
171 |
value => $value, |
172 |
}); |
173 |
$col_values->{$col_name} = $col_value if( defined( $col_value ) ); |
174 |
} |
175 |
|
176 |
# If default values are set, maybe the data exist in the DB |
177 |
# But no need to wait for another value |
178 |
last if exists( $default_value->{$source} ); |
179 |
|
180 |
if ( scalar keys %unique_constraints > 0 ) { |
181 |
|
182 |
# verify the data would respect each unique constraint |
183 |
foreach my $constraint (keys %unique_constraints) { |
184 |
|
185 |
my $condition; |
186 |
my @constraint_columns = $unique_constraints{$constraint}; |
187 |
# loop through all constraint columns and build the condition |
188 |
foreach my $constraint_column ( @constraint_columns ) { |
189 |
# build the filter |
190 |
$condition->{ $constraint_column } = |
191 |
$col_values->{ $constraint_column }; |
192 |
} |
193 |
|
194 |
my $count = $self->schema |
195 |
->resultset( $source ) |
196 |
->search( $condition ) |
197 |
->count(); |
198 |
if ( $count > 0 ) { |
199 |
$at_least_one_constraint_failed = 1; |
200 |
# no point checking more stuff, exit the loop |
201 |
last; |
202 |
} |
203 |
} |
204 |
|
205 |
if ( $at_least_one_constraint_failed ) { |
206 |
$values_ok = 0; |
207 |
} else { |
208 |
$values_ok = 1; |
209 |
} |
210 |
|
211 |
} else { |
212 |
$values_ok = 1; |
213 |
} |
165 |
} |
214 |
} |
166 |
return $col_values; |
215 |
return $col_values; |
167 |
} |
216 |
} |
168 |
- |
|
|