File: db-object-loader-8.t

package info (click to toggle)
librose-db-object-perl 1%3A0.815-1%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,048 kB
  • sloc: perl: 79,670; sql: 28; makefile: 7
file content (177 lines) | stat: -rwxr-xr-x 3,848 bytes parent folder | download | duplicates (6)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/perl -w

use strict;

use Test::More tests => 2 + (1 * 1);

BEGIN 
{
  require 't/test-lib.pl';
  use_ok('Rose::DB');
  use_ok('Rose::DB::Object::Loader');
}

our %Have;

our @Tables = qw(attribute_types datatypes);
our $Include_Tables = join('|', @Tables);

#
# Tests
#

my $i = 1;

foreach my $db_type (qw(mysql))
{
  SKIP:
  {
    skip("$db_type tests", 1)  unless($Have{$db_type});
  }

  next  unless($Have{$db_type});

  $i++;


  my $class_prefix = ucfirst($db_type);

  #$Rose::DB::Object::Metadata::Debug = 1;

  my $db = Rose::DB->new($db_type);

  my $loader = 
    Rose::DB::Object::Loader->new(
      db           => $db,
      class_prefix => $class_prefix);

  # This call used to die prior to 0.7663
  my @classes = $loader->make_classes(include_tables => $Include_Tables);

  is(scalar @classes, 4, "make_classes - $db_type");

  #foreach my $class (@classes)
  #{
  #  if($class->can('meta'))
  #  {
  #    print $class->meta->perl_class_definition;
  #  }
  #  else
  #  {
  #    print $class->perl_class_definition;
  #  }
  #}

  #$DB::single = 1;
  #$Rose::DB::Object::Debug = 1;
}

BEGIN
{
  our %Have;

  my $dbh;

  #
  # MySQL
  #

  eval 
  {
    my $db = Rose::DB->new('mysql_admin');
    $dbh = $db->retain_dbh or die Rose::DB->error;

    die "MySQL version too old"  unless($db->database_version >= 4_000_000);

    # Drop existing tables, ignoring errors
    {
      local $dbh->{'RaiseError'} = 0;
      local $dbh->{'PrintError'} = 0;

      $dbh->do('ALTER TABLE attribute_types DROP FOREIGN KEY attribute_types_ibfk_1');
      $dbh->do('ALTER TABLE datatypes DROP FOREIGN KEY datatypes_ibfk_1');
      $dbh->do('DROP TABLE attribute_types CASCADE');
      $dbh->do('DROP TABLE datatypes CASCADE');
    }

    # Foreign key stuff requires InnoDB support
    $dbh->do(<<"EOF");
CREATE TABLE attribute_types
(
  id           BIGINT(20) UNSIGNED NOT NULL auto_increment,
  name         VARCHAR(255) NOT NULL,
  table_name   VARCHAR(255) NOT NULL,
  datatype_id  BIGINT(20) UNSIGNED NOT NULL,

  PRIMARY KEY (id),
  KEY name (name),
  KEY datatype_id (datatype_id)
)
ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1
EOF

    # MySQL will silently ignore the "ENGINE=InnoDB" part and create
    # a MyISAM table instead.  MySQL is evil!  Now we have to manually
    # check to make sure an InnoDB table was really created.
    my $db_name = $db->database;
    my $sth = $dbh->prepare("SHOW TABLE STATUS FROM `$db_name` LIKE ?");
    $sth->execute('attribute_types');
    my $info = $sth->fetchrow_hashref;

    no warnings 'uninitialized';
    unless(lc $info->{'Type'} eq 'innodb' || lc $info->{'Engine'} eq 'innodb')
    {
      die "Missing InnoDB support";
    }
  };

  if(!$@ && $dbh)
  {
    $Have{'mysql'} = 1;

    $dbh->do(<<"EOF");
CREATE TABLE datatypes
(
  id      BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  name    VARCHAR(255) NOT NULL,
  format  VARCHAR(255) NOT NULL default '.*',

  PRIMARY KEY (id),
  UNIQUE KEY name (name)
)
ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1
EOF

$dbh->do(<<"EOF");
ALTER TABLE attribute_types ADD CONSTRAINT attribute_types_ibfk_1 
  FOREIGN KEY (datatype_id) REFERENCES datatypes (id)
EOF

$dbh->do(<<"EOF");
ALTER TABLE datatypes ADD CONSTRAINT datatypes_ibfk_1 
  FOREIGN KEY (id) REFERENCES attribute_types (datatype_id) 
  ON DELETE CASCADE ON UPDATE NO ACTION
EOF

    $dbh->disconnect;
  }
}

END
{
  # Delete test table

  if($Have{'mysql'})
  {
    # MySQL
    my $dbh = Rose::DB->new('mysql_admin')->retain_dbh()
      or die Rose::DB->error;

    $dbh->do('ALTER TABLE attribute_types DROP FOREIGN KEY attribute_types_ibfk_1');
    $dbh->do('ALTER TABLE datatypes DROP FOREIGN KEY datatypes_ibfk_1');
    $dbh->do('DROP TABLE attribute_types CASCADE');
    $dbh->do('DROP TABLE datatypes CASCADE');

    $dbh->disconnect;
  }
}