From 7a67cd30a31905047966bdedd418e389138326f0 Mon Sep 17 00:00:00 2001 From: "mkanat%kerio.com" <> Date: Wed, 22 Feb 2006 18:50:36 +0000 Subject: Bug 328063: [PostgreSQL] Bugzilla::DB::Pg does not support adding a new SERIAL-type column to a table Patch By Max Kanat-Alexander r=LpSolit, a=justdave --- Bugzilla/DB/Schema/Pg.pm | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) (limited to 'Bugzilla/DB') 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)"); } -- cgit v1.2.3-24-g4f1b