~ubuntu-branches/debian/sid/bugzilla/sid

« back to all changes in this revision

Viewing changes to Bugzilla/DB/Schema/Pg.pm

  • Committer: Bazaar Package Importer
  • Author(s): Raphael Bossek
  • Date: 2008-06-27 22:34:34 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20080627223434-0ib57vstn43bb4a3
Tags: 3.0.4.1-1
* Update of French, Russian and German translations. (closes: #488251)
* Added Bulgarian and Belarusian translations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- Mode: perl; indent-tabs-mode: nil -*-
2
 
#
3
 
# The contents of this file are subject to the Mozilla Public
4
 
# License Version 1.1 (the "License"); you may not use this file
5
 
# except in compliance with the License. You may obtain a copy of
6
 
# the License at http://www.mozilla.org/MPL/
7
 
#
8
 
# Software distributed under the License is distributed on an "AS
9
 
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10
 
# implied. See the License for the specific language governing
11
 
# rights and limitations under the License.
12
 
#
13
 
# The Original Code is the Bugzilla Bug Tracking System.
14
 
#
15
 
# The Initial Developer of the Original Code is Netscape Communications
16
 
# Corporation. Portions created by Netscape are
17
 
# Copyright (C) 1998 Netscape Communications Corporation. All
18
 
# Rights Reserved.
19
 
#
20
 
# Contributor(s): Andrew Dunstan <andrew@dunslane.net>,
21
 
#                 Edward J. Sabol <edwardjsabol@iname.com>
22
 
#                 Max Kanat-Alexander <mkanat@bugzilla.org>
23
 
 
24
 
package Bugzilla::DB::Schema::Pg;
25
 
 
26
 
###############################################################################
27
 
#
28
 
# DB::Schema implementation for PostgreSQL
29
 
#
30
 
###############################################################################
31
 
 
32
 
use strict;
33
 
use base qw(Bugzilla::DB::Schema);
34
 
use Storable qw(dclone);
35
 
 
36
 
#------------------------------------------------------------------------------
37
 
sub _initialize {
38
 
 
39
 
    my $self = shift;
40
 
 
41
 
    $self = $self->SUPER::_initialize(@_);
42
 
 
43
 
    # Remove FULLTEXT index types from the schemas.
44
 
    foreach my $table (keys %{ $self->{schema} }) {
45
 
        if ($self->{schema}{$table}{INDEXES}) {
46
 
            foreach my $index (@{ $self->{schema}{$table}{INDEXES} }) {
47
 
                if (ref($index) eq 'HASH') {
48
 
                    delete($index->{TYPE}) if (exists $index->{TYPE} 
49
 
                        && $index->{TYPE} eq 'FULLTEXT');
50
 
                }
51
 
            }
52
 
            foreach my $index (@{ $self->{abstract_schema}{$table}{INDEXES} }) {
53
 
                if (ref($index) eq 'HASH') {
54
 
                    delete($index->{TYPE}) if (exists $index->{TYPE} 
55
 
                        && $index->{TYPE} eq 'FULLTEXT');
56
 
                }
57
 
            }
58
 
        }
59
 
    }
60
 
 
61
 
    $self->{db_specific} = {
62
 
 
63
 
        BOOLEAN =>      'smallint',
64
 
        FALSE =>        '0', 
65
 
        TRUE =>         '1',
66
 
 
67
 
        INT1 =>         'integer',
68
 
        INT2 =>         'integer',
69
 
        INT3 =>         'integer',
70
 
        INT4 =>         'integer',
71
 
 
72
 
        SMALLSERIAL =>  'serial unique',
73
 
        MEDIUMSERIAL => 'serial unique',
74
 
        INTSERIAL =>    'serial unique',
75
 
 
76
 
        TINYTEXT =>     'varchar(255)',
77
 
        MEDIUMTEXT =>   'text',
78
 
        TEXT =>         'text',
79
 
 
80
 
        LONGBLOB =>     'bytea',
81
 
 
82
 
        DATETIME =>     'timestamp(0) without time zone',
83
 
 
84
 
    };
85
 
 
86
 
    $self->_adjust_schema;
87
 
 
88
 
    return $self;
89
 
 
90
 
} #eosub--_initialize
91
 
#--------------------------------------------------------------------
92
 
 
93
 
sub get_rename_column_ddl {
94
 
    my ($self, $table, $old_name, $new_name) = @_;
95
 
    if (lc($old_name) eq lc($new_name)) {
96
 
        # if the only change is a case change, return an empty list, since Pg
97
 
        # is case-insensitive and will return an error about a duplicate name
98
 
        return ();
99
 
    }
100
 
    my @sql = ("ALTER TABLE $table RENAME COLUMN $old_name TO $new_name");
101
 
    my $def = $self->get_column_abstract($table, $old_name);
102
 
    if ($def->{TYPE} =~ /SERIAL/i) {
103
 
        # We have to rename the series also, and fix the default of the series.
104
 
        push(@sql, "ALTER TABLE ${table}_${old_name}_seq 
105
 
                      RENAME TO ${table}_${new_name}_seq");
106
 
        push(@sql, "ALTER TABLE $table ALTER COLUMN $new_name 
107
 
                    SET DEFAULT NEXTVAL('${table}_${new_name}_seq')");
108
 
    }
109
 
    return @sql;
110
 
}
111
 
 
112
 
sub get_rename_table_sql {
113
 
    my ($self, $old_name, $new_name) = @_;
114
 
    if (lc($old_name) eq lc($new_name)) {
115
 
        # if the only change is a case change, return an empty list, since Pg
116
 
        # is case-insensitive and will return an error about a duplicate name
117
 
        return ();
118
 
    }
119
 
    return ("ALTER TABLE $old_name RENAME TO $new_name");
120
 
}
121
 
 
122
 
sub _get_alter_type_sql {
123
 
    my ($self, $table, $column, $new_def, $old_def) = @_;
124
 
    my @statements;
125
 
 
126
 
    my $type = $new_def->{TYPE};
127
 
    $type = $self->{db_specific}->{$type} 
128
 
        if exists $self->{db_specific}->{$type};
129
 
 
130
 
    if ($type =~ /serial/i && $old_def->{TYPE} !~ /serial/i) {
131
 
        die("You cannot specify a DEFAULT on a SERIAL-type column.") 
132
 
            if $new_def->{DEFAULT};
133
 
        $type =~ s/serial/integer/i;
134
 
    }
135
 
 
136
 
    # On Pg, you don't need UNIQUE if you're a PK--it creates
137
 
    # two identical indexes otherwise.
138
 
    $type =~ s/unique//i if $new_def->{PRIMARYKEY};
139
 
 
140
 
    push(@statements, "ALTER TABLE $table ALTER COLUMN $column
141
 
                              TYPE $type");
142
 
 
143
 
    if ($new_def->{TYPE} =~ /serial/i && $old_def->{TYPE} !~ /serial/i) {
144
 
        push(@statements, "CREATE SEQUENCE ${table}_${column}_seq");
145
 
        push(@statements, "SELECT setval('${table}_${column}_seq',
146
 
                                         MAX($table.$column))
147
 
                             FROM $table");
148
 
        push(@statements, "ALTER TABLE $table ALTER COLUMN $column 
149
 
                           SET DEFAULT nextval('${table}_${column}_seq')");
150
 
    }
151
 
 
152
 
    # If this column is no longer SERIAL, we need to drop the sequence
153
 
    # that went along with it.
154
 
    if ($old_def->{TYPE} =~ /serial/i && $new_def->{TYPE} !~ /serial/i) {
155
 
        push(@statements, "ALTER TABLE $table ALTER COLUMN $column 
156
 
                           DROP DEFAULT");
157
 
        # XXX Pg actually won't let us drop the sequence, even though it's
158
 
        #     no longer in use. So we harmlessly leave behind a sequence
159
 
        #     that does nothing.
160
 
        #push(@statements, "DROP SEQUENCE ${table}_${column}_seq");
161
 
    }
162
 
 
163
 
    return @statements;
164
 
}
165
 
 
166
 
1;