Pg.pm 6.57 KB
Newer Older
1 2 3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
#
5 6
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
7 8 9 10 11 12 13 14 15

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

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

16
use 5.10.1;
17
use strict;
18

19
use parent qw(Bugzilla::DB::Schema);
20
use Storable qw(dclone);
21 22 23 24 25 26

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

    my $self = shift;

27
    $self = $self->SUPER::_initialize(@_);
28

29 30 31 32 33
    # 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') {
34 35
                    delete($index->{TYPE}) if (exists $index->{TYPE} 
                        && $index->{TYPE} eq 'FULLTEXT');
36 37 38 39
                }
            }
            foreach my $index (@{ $self->{abstract_schema}{$table}{INDEXES} }) {
                if (ref($index) eq 'HASH') {
40 41
                    delete($index->{TYPE}) if (exists $index->{TYPE} 
                        && $index->{TYPE} eq 'FULLTEXT');
42 43 44 45 46
                }
            }
        }
    }

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    $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',
64
        LONGTEXT =>     'text',
65 66 67

        LONGBLOB =>     'bytea',

68
        DATETIME =>     'timestamp(0) without time zone',
69
        DATE     =>     'date',
70 71 72 73 74 75 76
    };

    $self->_adjust_schema;

    return $self;

} #eosub--_initialize
77 78
#--------------------------------------------------------------------

79 80 81 82 83 84 85 86 87 88
sub get_create_database_sql {
    my ($self, $name) = @_;
    # We only create as utf8 if we have no params (meaning we're doing
    # a new installation) or if the utf8 param is on.
    my $create_utf8 = Bugzilla->params->{'utf8'}
                      || !defined Bugzilla->params->{'utf8'};
    my $charset = $create_utf8 ? "ENCODING 'UTF8' TEMPLATE template0" : '';
    return ("CREATE DATABASE $name $charset");
}

89 90
sub get_rename_column_ddl {
    my ($self, $table, $old_name, $new_name) = @_;
91 92 93 94 95
    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 ();
    }
96 97 98
    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) {
99 100 101
        # We have to rename the series also.
        push(@sql, "ALTER SEQUENCE ${table}_${old_name}_seq 
                         RENAME TO ${table}_${new_name}_seq");
102 103
    }
    return @sql;
104
}
105

106 107 108 109 110 111 112
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 ();
    }
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

    my @sql = ("ALTER TABLE $old_name RENAME TO $new_name");

    # If there's a SERIAL column on this table, we also need to rename the
    # sequence.
    # If there is a PRIMARY KEY, we need to rename it too.
    my @columns = $self->get_table_columns($old_name);
    foreach my $column (@columns) {
        my $def = $self->get_column_abstract($old_name, $column);
        if ($def->{TYPE} =~ /SERIAL/i) {
            my $old_seq = "${old_name}_${column}_seq";
            my $new_seq = "${new_name}_${column}_seq";
            push(@sql, "ALTER SEQUENCE $old_seq RENAME TO $new_seq");
            push(@sql, "ALTER TABLE $new_name ALTER COLUMN $column
                             SET DEFAULT NEXTVAL('$new_seq')");
        }
        if ($def->{PRIMARYKEY}) {
            my $old_pk = "${old_name}_pkey";
            my $new_pk = "${new_name}_pkey";
            push(@sql, "ALTER INDEX $old_pk RENAME to $new_pk");
        }
    }

    return @sql;
137 138
}

139 140 141 142 143 144
sub get_set_serial_sql {
    my ($self, $table, $column, $value) = @_;
    return ("SELECT setval('${table}_${column}_seq', $value, false)
               FROM $table");
}

145 146
sub _get_alter_type_sql {
    my ($self, $table, $column, $new_def, $old_def) = @_;
147 148
    my @statements;

149 150 151
    my $type = $new_def->{TYPE};
    $type = $self->{db_specific}->{$type} 
        if exists $self->{db_specific}->{$type};
152

153 154 155
    if ($type =~ /serial/i && $old_def->{TYPE} !~ /serial/i) {
        die("You cannot specify a DEFAULT on a SERIAL-type column.") 
            if $new_def->{DEFAULT};
156 157
    }

158 159
    $type =~ s/\bserial\b/integer/i;

160 161 162
    # 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};
163

164 165
    push(@statements, "ALTER TABLE $table ALTER COLUMN $column
                              TYPE $type");
166

167
    if ($new_def->{TYPE} =~ /serial/i && $old_def->{TYPE} !~ /serial/i) {
168 169
        push(@statements, "CREATE SEQUENCE ${table}_${column}_seq
                                  OWNED BY $table.$column");
170
        push(@statements, "SELECT setval('${table}_${column}_seq',
171 172
                                         MAX($table.$column))
                             FROM $table");
173 174
        push(@statements, "ALTER TABLE $table ALTER COLUMN $column 
                           SET DEFAULT nextval('${table}_${column}_seq')");
175 176
    }

177 178 179 180 181
    # 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");
182 183 184
        push(@statements, "ALTER SEQUENCE ${table}_${column}_seq 
                           OWNED BY NONE");
        push(@statements, "DROP SEQUENCE ${table}_${column}_seq");
185 186 187 188 189
    }

    return @statements;
}

190
1;
191 192 193 194 195 196 197 198 199 200 201 202 203 204

=head1 B<Methods in need of POD>

=over

=item get_rename_column_ddl

=item get_rename_table_sql

=item get_create_database_sql

=item get_set_serial_sql

=back