File: MyBase.pm

package info (click to toggle)
libdbix-class-perl 0.082843-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,320 kB
  • sloc: perl: 27,215; sql: 322; sh: 29; makefile: 16
file content (74 lines) | stat: -rw-r--r-- 1,463 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package # hide from PAUSE
    MyBase;

use warnings;
use strict;

use DBI;

use lib 't/lib';
use DBICTest;

use base qw(DBIx::Class::CDBICompat);

our $dbh;

my $err;
if (! $ENV{DBICTEST_MYSQL_DSN} ) {
  $err = 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test';
}
elsif ( ! DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_mysql') ) {
  $err = 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_mysql')
}

if ($err) {
  my $t = eval { Test::Builder->new };
  if ($t and ! $t->current_test) {
    $t->skip_all ($err);
  }
  else {
    die "$err\n";
  }
}

my @connect = (@ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/}, { PrintError => 0});
# this is only so we grab a lock on mysql
{
  my $x = DBICTest::Schema->connect(@connect);
}

$dbh = DBI->connect(@connect) or die DBI->errstr;
my @table;

END { $dbh->do("DROP TABLE $_") foreach @table }

__PACKAGE__->connection(@connect);

sub set_table {
  my $class = shift;
  $class->table($class->create_test_table);
}

sub create_test_table {
  my $self   = shift;
  my $table  = $self->next_available_table;
  my $create = sprintf "CREATE TABLE $table ( %s )", $self->create_sql;
  push @table, $table;
  $dbh->do($create);
  return $table;
}

sub next_available_table {
  my $self   = shift;
  my @tables = sort @{
    $dbh->selectcol_arrayref(
      qq{
    SHOW TABLES
  }
    )
    };
  my $table = $tables[-1] || "aaa";
  return "z$table";
}

1;