Introducing DBIx::Class::Schema::SanityChecker
[dbsrgits/DBIx-Class.git] / t / cdbi / testlib / MyBase.pm
1 package # hide from PAUSE
2     MyBase;
3
4 use warnings;
5 use strict;
6
7 use DBI;
8 use DBICTest;
9
10 BEGIN {
11   # offset the warning from DBIx::Class::Schema on 5.8
12   # keep the ::Schema default as-is otherwise
13    DBIx::Class::_ENV_::OLD_MRO
14     and
15   ( eval <<'EOS' or die $@ );
16
17   sub setup_schema_instance {
18     my $s = shift->next::method(@_);
19     $s->schema_sanity_checker('');
20     $s;
21   }
22
23   1;
24 EOS
25 }
26
27 use base qw(DBIx::Class::CDBICompat);
28
29 my @connect = (@ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/}, { PrintError => 0});
30 # this is only so we grab a lock on mysql
31 {
32   my $x = DBICTest::Schema->connect(@connect);
33 }
34
35 our $dbh = DBI->connect(@connect) or die DBI->errstr;
36 my @table;
37
38 END {
39   $dbh->do("DROP TABLE $_") for @table;
40   undef $dbh;
41 }
42
43 __PACKAGE__->connection(@connect);
44
45 sub set_table {
46   my $class = shift;
47   $class->table($class->create_test_table);
48 }
49
50 sub create_test_table {
51   my $self   = shift;
52   my $table  = $self->next_available_table;
53   my $create = sprintf "CREATE TABLE $table ( %s )", $self->create_sql;
54   push @table, $table;
55   $dbh->do($create);
56   return $table;
57 }
58
59 sub next_available_table {
60   my $self   = shift;
61   my @tables = sort @{
62     $dbh->selectcol_arrayref(
63       qq{
64     SHOW TABLES
65   }
66     )
67     };
68   my $table = $tables[-1] || "aaa";
69   return "z$table";
70 }
71
72 1;