Broke up the prefetch tests into their own directory
[dbsrgits/DBIx-Class.git] / t / prefetch / rows_bug.t
1 # Test to ensure we get a consistent result set wether or not we use the
2 # prefetch option in combination rows (LIMIT).
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use lib qw(t/lib);
8 use DBICTest;
9
10 plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 2);
11
12 my $schema = DBICTest->init_schema();
13 my $no_prefetch = $schema->resultset('Artist')->search(
14   undef,
15   { rows => 3 }
16 );
17
18 my $use_prefetch = $schema->resultset('Artist')->search(
19   undef,
20   {
21     prefetch => 'cds',
22     rows     => 3
23   }
24 );
25
26 my $no_prefetch_count  = 0;
27 my $use_prefetch_count = 0;
28
29 is($no_prefetch->count, $use_prefetch->count, '$no_prefetch->count == $use_prefetch->count');
30
31 TODO: {
32   local $TODO = "This is a difficult bug to fix, workaround is not to use prefetch with rows";
33   $no_prefetch_count++  while $no_prefetch->next;
34   $use_prefetch_count++ while $use_prefetch->next;
35   is(
36     $no_prefetch_count,
37     $use_prefetch_count,
38     "manual row count confirms consistency"
39     . " (\$no_prefetch_count == $no_prefetch_count, "
40     . " \$use_prefetch_count == $use_prefetch_count)"
41   );
42 }
43
44 __END__
45 The fix is to, when using prefetch, take the query and put it into a subquery
46 joined to the tables we're prefetching from. This might result in the same
47 table being joined once in the main subquery and once in the main query. This
48 may actually resolve other, unknown edgecase bugs. It is also the right way
49 to do prefetching. Optimizations can come later.
50
51 This means that:
52   $foo_rs->search(
53     { ... },
54     {
55       prefetch => 'bar',
56       ...
57     },
58   );
59
60 becomes:
61   my $temp = $foo_rs->search(
62     { ... },
63     {
64       join => 'bar',
65       ...
66     },
67   );
68   $foo_rs->storage->schema->resultset('foo')->search(
69     undef,
70     {
71       from => [
72         { me => $temp->as_query },
73       ],
74       prefetch => 'bar',
75     },
76   );
77
78 Problem:
79   * The prefetch->join change needs to happen ONLY IF there are conditions
80     that depend on bar being joined.