File: schema-override.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 (160 lines) | stat: -rwxr-xr-x 3,795 bytes parent folder | download | duplicates (7)
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
#!/usr/bin/perl -w

use strict;

use Test::More tests => 8;

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

our %Have;

#
# Tests
#

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

SKIP:
{
  skip("pg tests", 7)  unless($Have{'pg'});

  my $db_pg = Rose::DB->new('pg');
  my $db_ws = Rose::DB->new('pg_with_schema');

  # Albums should take on the schema of the db handle
  my $a1 = Album->new(db => $db_pg, name => 'One', year => 2001)->save;
  my $a2 = Album->new(db => $db_ws, name => 'One', year => 2002)->save;

  is($a1->id, 1, 'flex schema 1');
  is($a2->id, 1, 'flex schema 2');

  # Album photos should NOT take on the schema of the db handle
  my $p1 = AlbumPhoto->new(db => $db_pg, album_id => 1, name => '1.1')->save;
  my $p2 = AlbumPhoto->new(db => $db_ws, album_id => 1, name => '1.2')->save;

  is($p1->id, 1, 'flex schema 1');
  is($p2->id, 2, 'flex schema 2');

  # Make sure both albums read the same album photos table
  is_deeply([ map { $_->name } sort { $a->id <=> $b->id } $a1->album_photos ], 
            [ '1.1', '1.2' ], 'single photos table 1');

  is_deeply([ map { $_->name } sort { $a->id <=> $b->id } $a2->album_photos ], 
            [ '1.1', '1.2' ], 'single photos table 2');

  $a1 = Album->new(id => $a1->id);
  $a1->load(with => 'album_photos');

  is_deeply([ map { $_->name } sort { $a->id <=> $b->id } $a1->album_photos ], 
            [ '1.1', '1.2' ], 'single photos table 3');
}

BEGIN
{
  our %Have;

  #
  # PostgreSQL
  #

  my $dbh;

  eval 
  {
    $dbh = Rose::DB->new('pg_admin')->retain_dbh()
      or die Rose::DB->error;
  };

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

    # Drop existing tables and create schema, ignoring errors
    {
      local $dbh->{'RaiseError'} = 0;
      local $dbh->{'PrintError'} = 0;
      $dbh->do('DROP TABLE Rose_db_object_private.rdbo_album_photos CASCADE');
      $dbh->do('DROP TABLE Rose_db_object_private.rdbo_albums CASCADE');
      $dbh->do('DROP TABLE rdbo_albums CASCADE');
      $dbh->do('CREATE SCHEMA Rose_db_object_private');
    }

    $dbh->do(<<"EOF");
CREATE TABLE rdbo_albums
(
  id        SERIAL PRIMARY KEY,
  name      VARCHAR(32) UNIQUE,
  artist    VARCHAR(32),
  year      INTEGER
)
EOF

    $dbh->do(<<"EOF");
CREATE TABLE Rose_db_object_private.rdbo_albums
(
  id        SERIAL PRIMARY KEY,
  name      VARCHAR(32) UNIQUE,
  artist    VARCHAR(32),
  year      INTEGER
)
EOF

    $dbh->do(<<"EOF");
CREATE TABLE Rose_db_object_private.rdbo_album_photos
(
  id        SERIAL PRIMARY KEY,
  album_id  INT REFERENCES rdbo_albums (id),
  name      VARCHAR(32)
)
EOF

    $dbh->disconnect;

    Rose::DB->default_type('pg');

    package MyCM;
    our @ISA = qw(Rose::DB::Object::ConventionManager);
    sub auto_relationship_name_one_to_many
    { 
      my($self, $table, $class) = @_;
      return $self->auto_class_to_relationship_name_plural($class);
    }

    package Album;
    our @ISA = qw(Rose::DB::Object);
    Album->meta->convention_manager('MyCM');
    Album->meta->table('rdbo_albums');
    Album->meta->auto_initialize;

    package AlbumPhoto;
    our @ISA = qw(Rose::DB::Object);
    AlbumPhoto->meta->convention_manager('MyCM');
    AlbumPhoto->meta->table('rdbo_album_photos');
    AlbumPhoto->meta->schema('Rose_db_object_private');
    AlbumPhoto->meta->auto_initialize;
  }
}

END
{
  # Delete test table

  if($Have{'pg'})
  {
    # PostgreSQL
    my $dbh = Rose::DB->new('pg_admin')->retain_dbh()
      or die Rose::DB->error;

    $dbh->do('DROP TABLE Rose_db_object_private.rdbo_album_photos CASCADE');
    $dbh->do('DROP TABLE Rose_db_object_private.rdbo_albums CASCADE');
    $dbh->do('DROP TABLE rdbo_albums CASCADE');
    $dbh->do('DROP SCHEMA Rose_db_object_private CASCADE');

    $dbh->disconnect;
  }
}