Pg.pm 5.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Bug Tracking System.
#
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s): Andrew Dunstan <andrew@dunslane.net>,
#                 Edward J. Sabol <edwardjsabol@iname.com>
22
#                 Max Kanat-Alexander <mkanat@bugzilla.org>
23 24 25 26 27 28 29 30 31 32 33

package Bugzilla::DB::Schema::Pg;

###############################################################################
#
# DB::Schema implementation for PostgreSQL
#
###############################################################################

use strict;
use base qw(Bugzilla::DB::Schema);
34
use Storable qw(dclone);
35 36 37 38 39 40

#------------------------------------------------------------------------------
sub _initialize {

    my $self = shift;

41
    $self = $self->SUPER::_initialize(@_);
42

43 44 45 46 47
    # Remove FULLTEXT index types from the schemas.
    foreach my $table (keys %{ $self->{schema} }) {
        if ($self->{schema}{$table}{INDEXES}) {
            foreach my $index (@{ $self->{schema}{$table}{INDEXES} }) {
                if (ref($index) eq 'HASH') {
48 49
                    delete($index->{TYPE}) if (exists $index->{TYPE} 
                        && $index->{TYPE} eq 'FULLTEXT');
50 51 52 53
                }
            }
            foreach my $index (@{ $self->{abstract_schema}{$table}{INDEXES} }) {
                if (ref($index) eq 'HASH') {
54 55
                    delete($index->{TYPE}) if (exists $index->{TYPE} 
                        && $index->{TYPE} eq 'FULLTEXT');
56 57 58 59 60
                }
            }
        }
    }

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    $self->{db_specific} = {

        BOOLEAN =>      'smallint',
        FALSE =>        '0', 
        TRUE =>         '1',

        INT1 =>         'integer',
        INT2 =>         'integer',
        INT3 =>         'integer',
        INT4 =>         'integer',

        SMALLSERIAL =>  'serial unique',
        MEDIUMSERIAL => 'serial unique',
        INTSERIAL =>    'serial unique',

        TINYTEXT =>     'varchar(255)',
        MEDIUMTEXT =>   'text',
78
        LONGTEXT =>     'text',
79 80 81

        LONGBLOB =>     'bytea',

82
        DATETIME =>     'timestamp(0) without time zone',
83 84 85 86 87 88 89 90

    };

    $self->_adjust_schema;

    return $self;

} #eosub--_initialize
91 92
#--------------------------------------------------------------------

93 94
sub get_rename_column_ddl {
    my ($self, $table, $old_name, $new_name) = @_;
95 96 97 98 99
    if (lc($old_name) eq lc($new_name)) {
        # if the only change is a case change, return an empty list, since Pg
        # is case-insensitive and will return an error about a duplicate name
        return ();
    }
100 101 102 103 104 105 106 107 108 109
    my @sql = ("ALTER TABLE $table RENAME COLUMN $old_name TO $new_name");
    my $def = $self->get_column_abstract($table, $old_name);
    if ($def->{TYPE} =~ /SERIAL/i) {
        # We have to rename the series also, and fix the default of the series.
        push(@sql, "ALTER TABLE ${table}_${old_name}_seq 
                      RENAME TO ${table}_${new_name}_seq");
        push(@sql, "ALTER TABLE $table ALTER COLUMN $new_name 
                    SET DEFAULT NEXTVAL('${table}_${new_name}_seq')");
    }
    return @sql;
110
}
111

112 113 114 115 116 117 118 119 120 121
sub get_rename_table_sql {
    my ($self, $old_name, $new_name) = @_;
    if (lc($old_name) eq lc($new_name)) {
        # if the only change is a case change, return an empty list, since Pg
        # is case-insensitive and will return an error about a duplicate name
        return ();
    }
    return ("ALTER TABLE $old_name RENAME TO $new_name");
}

122 123 124 125 126 127
sub get_set_serial_sql {
    my ($self, $table, $column, $value) = @_;
    return ("SELECT setval('${table}_${column}_seq', $value, false)
               FROM $table");
}

128 129
sub _get_alter_type_sql {
    my ($self, $table, $column, $new_def, $old_def) = @_;
130 131
    my @statements;

132 133 134
    my $type = $new_def->{TYPE};
    $type = $self->{db_specific}->{$type} 
        if exists $self->{db_specific}->{$type};
135

136 137 138
    if ($type =~ /serial/i && $old_def->{TYPE} !~ /serial/i) {
        die("You cannot specify a DEFAULT on a SERIAL-type column.") 
            if $new_def->{DEFAULT};
139 140 141
        $type =~ s/serial/integer/i;
    }

142 143 144
    # On Pg, you don't need UNIQUE if you're a PK--it creates
    # two identical indexes otherwise.
    $type =~ s/unique//i if $new_def->{PRIMARYKEY};
145

146 147
    push(@statements, "ALTER TABLE $table ALTER COLUMN $column
                              TYPE $type");
148

149 150 151
    if ($new_def->{TYPE} =~ /serial/i && $old_def->{TYPE} !~ /serial/i) {
        push(@statements, "CREATE SEQUENCE ${table}_${column}_seq");
        push(@statements, "SELECT setval('${table}_${column}_seq',
152 153
                                         MAX($table.$column))
                             FROM $table");
154 155
        push(@statements, "ALTER TABLE $table ALTER COLUMN $column 
                           SET DEFAULT nextval('${table}_${column}_seq')");
156 157
    }

158 159 160 161 162 163 164 165 166
    # If this column is no longer SERIAL, we need to drop the sequence
    # that went along with it.
    if ($old_def->{TYPE} =~ /serial/i && $new_def->{TYPE} !~ /serial/i) {
        push(@statements, "ALTER TABLE $table ALTER COLUMN $column 
                           DROP DEFAULT");
        # XXX Pg actually won't let us drop the sequence, even though it's
        #     no longer in use. So we harmlessly leave behind a sequence
        #     that does nothing.
        #push(@statements, "DROP SEQUENCE ${table}_${column}_seq");
167 168 169 170 171
    }

    return @statements;
}

172
1;