Pg.pm 4.55 KB
Newer Older
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
# -*- 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>

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

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

use strict;

use base qw(Bugzilla::DB::Schema);

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

    my $self = shift;

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

42 43 44 45 46
    # 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') {
47 48
                    delete($index->{TYPE}) if (exists $index->{TYPE} 
                        && $index->{TYPE} eq 'FULLTEXT');
49 50 51 52
                }
            }
            foreach my $index (@{ $self->{abstract_schema}{$table}{INDEXES} }) {
                if (ref($index) eq 'HASH') {
53 54
                    delete($index->{TYPE}) if (exists $index->{TYPE} 
                        && $index->{TYPE} eq 'FULLTEXT');
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
    $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',
        TEXT =>         'text',

        LONGBLOB =>     'bytea',

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

    };

    $self->_adjust_schema;

    return $self;

} #eosub--_initialize
90 91 92 93
#--------------------------------------------------------------------

# Overridden because Pg has such weird ALTER TABLE problems.
sub get_add_column_ddl {
94
    my ($self, $table, $column, $definition, $init_value) = @_;
95 96 97 98 99 100 101 102 103 104

    my @statements;
    my $specific = $self->{db_specific};

    my $type = $definition->{TYPE};
    $type = $specific->{$type} if exists $specific->{$type};
    push(@statements, "ALTER TABLE $table ADD COLUMN $column $type");

    my $default = $definition->{DEFAULT};
    if (defined $default) {
105 106
        # Replace any abstract default value (such as 'TRUE' or 'FALSE')
        # with its database-specific implementation.
107 108 109 110 111
        $default = $specific->{$default} if exists $specific->{$default};
        push(@statements, "ALTER TABLE $table ALTER COLUMN $column "
                         . " SET DEFAULT $default");
    }

112 113 114 115
    if (defined $init_value) {
        push(@statements, "UPDATE $table SET $column = $init_value");
    }

116
    if ($definition->{NOTNULL}) {
117 118 119 120
        # Handle rows that were NULL when we added the column.
        # We *must* have a DEFAULT. This check is usually handled
        # at a higher level than this code, but I figure it can't
        # hurt to have it here.
121 122
        die "NOT NULL columns must have a DEFAULT or an init_value."
            unless (exists $definition->{DEFAULT} || defined $init_value);
123
        push(@statements, "UPDATE $table SET $column = $default");
124 125 126 127 128
        push(@statements, "ALTER TABLE $table ALTER COLUMN $column "
                         . " SET NOT NULL");
    }

    if ($definition->{PRIMARYKEY}) {
129 130
         push(@statements, "ALTER TABLE $table ALTER COLUMN "
                         . " ADD PRIMARY KEY ($column)");
131 132 133 134 135
    }

    return @statements;
}

136 137 138 139 140 141
sub get_rename_column_ddl {
    my ($self, $table, $old_name, $new_name) = @_;

    return ("ALTER TABLE $table RENAME COLUMN $old_name TO $new_name");
}

142
1;