Intermezzo checkin
[dbsrgits/DBIx-Class.git] / t / 98rows_prefetch.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 $schema->storage->debug(1);
14 my $no_prefetch = $schema->resultset('Artist')->search(
15   undef,
16   { rows => 3 }
17 );
18
19 my $use_prefetch = $schema->resultset('Artist')->search(
20   undef,
21   {
22     prefetch => 'cds',
23     rows     => 3
24   }
25 );
26
27 my $no_prefetch_count  = 0;
28 my $use_prefetch_count = 0;
29
30 is($no_prefetch->count, $use_prefetch->count, '$no_prefetch->count == $use_prefetch->count');
31
32 TODO: {
33   local $TODO = "This is a difficult bug to fix, workaround is not to use prefetch with rows";
34   $no_prefetch_count++  while $no_prefetch->next;
35   $use_prefetch_count++ while $use_prefetch->next;
36   is(
37     $no_prefetch_count,
38     $use_prefetch_count,
39     "manual row count confirms consistency"
40     . " (\$no_prefetch_count == $no_prefetch_count, "
41     . " \$use_prefetch_count == $use_prefetch_count)"
42   );
43 }
44
45 __END__
46 The fix is to, when using prefetch, take the query and put it into a subquery
47 joined to the tables we're prefetching from. This might result in the same
48 table being joined once in the main subquery and once in the main query. This
49 may actually resolve other, unknown edgecase bugs. It is also the right way
50 to do prefetching. Optimizations can come later.
51
52 This means that:
53   $foo_rs->search(
54     { ... },
55     {
56       prefetch => 'bar',
57       ...
58     },
59   );
60
61 becomes:
62   my $temp = $foo_rs->search(
63     { ... },
64     {
65       join => 'bar',
66       ...
67     },
68   );
69   $foo_rs->storage->schema->resultset('foo')->search(
70     undef,
71     {
72       from => [
73         { me => $temp->as_query },
74       ],
75       prefetch => 'bar',
76     },
77   );
78
79 Problem:
80   * The prefetch->join change needs to happen ONLY IF there are conditions
81     that depend on bar being joined.