roughly DTRT - please to be making less awful
[dbsrgits/DBIx-Class.git] / t / 93storage_replication.t
1 use strict;
2 use warnings;
3 use lib qw(t/lib);
4
5 use File::Copy;
6
7 use DBICTest;
8
9 use Test::More;
10 eval {use DBD::Multi};
11 plan skip_all => 'No DBD::Multi' if ($@);
12
13 plan tests => 3;
14
15 my $schema = DBICTest->init_schema();
16
17 $schema->storage_type( '::DBI::Replication' );
18
19
20 my $db_file1 = "t/var/DBIxClass.db";
21 my $db_file2 = "t/var/DBIxClass_slave1.db";
22 my $db_file3 = "t/var/DBIxClass_slave2.db";
23 my $dsn1 = $ENV{"DBICTEST_DSN"} || "dbi:SQLite:${db_file1}";
24 my $dsn2 = $ENV{"DBICTEST_DSN2"} || "dbi:SQLite:${db_file2}";
25 my $dsn3 = $ENV{"DBICTEST_DSN3"} || "dbi:SQLite:${db_file3}";
26
27 $schema->connect( [
28                    [ $dsn1, '', '', { AutoCommit => 1 } ],
29                    [ $dsn2, '', '', { priority => 10 } ],
30                    [ $dsn3, '', '', { priority => 10 } ]
31                   ]
32                 );
33
34 $schema->populate('Artist', [
35                              [ qw/artistid name/ ],
36                              [ 4, 'Ozric Tentacles']
37                             ]);
38
39 my $new_artist1 = $schema->resultset('Artist')->find(4);
40
41 isa_ok ($new_artist1, 'DBICTest::Artist');
42
43 # reconnect
44 my $schema2 = $schema->connect( [
45                                  [ $dsn1, '', '', { AutoCommit => 1 } ],
46                                  [ $dsn2, '', '', { priority => 10 } ],
47                                  [ $dsn3, '', '', { priority => 10 } ]
48                                 ]
49                               );
50
51 # try and read (should fail)
52 eval { my $new_artist2 = $schema2->resultset('Artist')->find(4); };
53 ok($@, 'read after disconnect fails because it uses slave 1 which we have neglected to "replicate" yet');
54
55 # try and read (should succede after faked synchronisation)
56 copy($db_file1, $db_file2);
57 $schema2 = $schema->connect( [
58                               [ $dsn1, '', '', { AutoCommit => 1 } ],
59                               [ $dsn2, '', '', { priority => 10 } ],
60                               [ $dsn3, '', '', { priority => 10 } ]
61                              ]
62                            );
63 my $new_artist3 = $schema2->resultset('Artist')->find(4);
64 isa_ok ($new_artist3, 'DBICTest::Artist');
65
66 unlink $db_file2;