summaryrefslogtreecommitdiffstats
path: root/Bugzilla/DB
diff options
context:
space:
mode:
authormkanat%kerio.com <>2006-02-22 19:50:36 +0100
committermkanat%kerio.com <>2006-02-22 19:50:36 +0100
commit7a67cd30a31905047966bdedd418e389138326f0 (patch)
treedd1a64700a1e438d579c49466348f44ad24a1449 /Bugzilla/DB
parent2c265ad22dfc62dad765e493d1a2d0b646f617b3 (diff)
downloadbugzilla-7a67cd30a31905047966bdedd418e389138326f0.tar.gz
bugzilla-7a67cd30a31905047966bdedd418e389138326f0.tar.xz
Bug 328063: [PostgreSQL] Bugzilla::DB::Pg does not support adding a new SERIAL-type column to a table
Patch By Max Kanat-Alexander <mkanat@bugzilla.org> r=LpSolit, a=justdave
Diffstat (limited to 'Bugzilla/DB')
-rw-r--r--Bugzilla/DB/Schema/Pg.pm30
1 files changed, 23 insertions, 7 deletions
diff --git a/Bugzilla/DB/Schema/Pg.pm b/Bugzilla/DB/Schema/Pg.pm
index 09636186d..3361dc607 100644
--- a/Bugzilla/DB/Schema/Pg.pm
+++ b/Bugzilla/DB/Schema/Pg.pm
@@ -30,8 +30,8 @@ package Bugzilla::DB::Schema::Pg;
###############################################################################
use strict;
-
use base qw(Bugzilla::DB::Schema);
+use Storable qw(dclone);
#------------------------------------------------------------------------------
sub _initialize {
@@ -94,14 +94,30 @@ sub _initialize {
sub get_add_column_ddl {
my ($self, $table, $column, $definition, $init_value) = @_;
+ # So that we don't change the $definition for the caller.
+ my $def = dclone($definition);
+
my @statements;
my $specific = $self->{db_specific};
- my $type = $definition->{TYPE};
+ my $type = $def->{TYPE};
$type = $specific->{$type} if exists $specific->{$type};
+
+ # SERIAL Types need special handlings
+ # XXX This will create a column that doesn't look like a
+ # "SERIAL" in a pg_dump, but functions identically.
+ if ($type =~ /serial/i) {
+ $type =~ s/serial/integer/i;
+ $def->{DEFAULT} = "nextval('${table}_${column}_seq')";
+ push(@statements, "CREATE SEQUENCE ${table}_${column}_seq");
+ # On Pg, you don't need UNIQUE if you're a PK--it creates
+ # two identical indexes otherwise.
+ $type =~ s/unique//i if $def->{PRIMARYKEY};
+ }
+
push(@statements, "ALTER TABLE $table ADD COLUMN $column $type");
- my $default = $definition->{DEFAULT};
+ my $default = $def->{DEFAULT};
if (defined $default) {
# Replace any abstract default value (such as 'TRUE' or 'FALSE')
# with its database-specific implementation.
@@ -114,20 +130,20 @@ sub get_add_column_ddl {
push(@statements, "UPDATE $table SET $column = $init_value");
}
- if ($definition->{NOTNULL}) {
+ if ($def->{NOTNULL}) {
# 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.
die "NOT NULL columns must have a DEFAULT or an init_value."
- unless (exists $definition->{DEFAULT} || defined $init_value);
+ unless (exists $def->{DEFAULT} || defined $init_value);
push(@statements, "UPDATE $table SET $column = $default");
push(@statements, "ALTER TABLE $table ALTER COLUMN $column "
. " SET NOT NULL");
}
- if ($definition->{PRIMARYKEY}) {
- push(@statements, "ALTER TABLE $table ALTER COLUMN "
+ if ($def->{PRIMARYKEY}) {
+ push(@statements, "ALTER TABLE $table "
. " ADD PRIMARY KEY ($column)");
}