From f53d4eacd120cdcc4d64bcc06ac3a72f8277a1f4 Mon Sep 17 00:00:00 2001 From: "mkanat%kerio.com" <> Date: Wed, 9 Mar 2005 13:45:34 +0000 Subject: Bug 284348: Move initial table creation into the Bugzilla::DB modules Patch By Max Kanat-Alexander r=glob, r=Tomas.Kopal, a=justdave --- Bugzilla/DB.pm | 48 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) (limited to 'Bugzilla/DB.pm') diff --git a/Bugzilla/DB.pm b/Bugzilla/DB.pm index 1dccf535a..59d19b48b 100644 --- a/Bugzilla/DB.pm +++ b/Bugzilla/DB.pm @@ -48,6 +48,7 @@ Exporter::export_ok_tags('deprecated'); use Bugzilla::Config qw(:DEFAULT :db); use Bugzilla::Util; use Bugzilla::Error; +use Bugzilla::DB::Schema; # All this code is backwards compat fu. As such, its a bit ugly. Note the # circular dependencies on Bugzilla.pm @@ -276,6 +277,31 @@ sub bz_get_field_defs { return(@fields); } +##################################################################### +# Database Setup +##################################################################### + +sub bz_setup_database { + my ($self) = @_; + + # Get a list of the existing tables (if any) in the database + my $table_sth = $self->table_info(undef, undef, undef, "TABLE"); + my @current_tables = + @{$self->selectcol_arrayref($table_sth, { Columns => [3] })}; + + my @desired_tables = $self->_bz_schema->get_table_list(); + + foreach my $table_name (@desired_tables) { + next if grep /^$table_name$/, @current_tables; + print "Creating table $table_name ...\n"; + + my @table_sql = $self->_bz_schema->get_table_ddl($table_name); + foreach my $sql_statement (@table_sql) { + $self->do($sql_statement); + } + } +} + ##################################################################### # Schema Modification Methods ##################################################################### @@ -383,6 +409,14 @@ sub bz_rename_field ($$$) { # Schema Information Methods ##################################################################### +sub _bz_schema { + my ($self) = @_; + return $self->{private_bz_schema} if exists $self->{private_bz_schema}; + $self->{private_bz_schema} = + Bugzilla::DB::Schema->new($self->MODULE_NAME); + return $self->{private_bz_schema}; +} + # XXX - Needs to be made cross-db compatible. sub bz_get_field_def ($$) { my ($self, $table, $field) = @_; @@ -417,7 +451,7 @@ sub bz_get_index_def ($$) { $sth->execute; while (my $ref = $sth->fetchrow_arrayref) { - next if $$ref[2] ne $field; + next if $$ref[4] ne $field; return $ref; } } @@ -449,7 +483,6 @@ sub bz_start_transaction { } else { # Turn AutoCommit off and start a new transaction $self->begin_work(); - $self->{privateprivate_bz_in_transaction} = 1; } } @@ -557,7 +590,7 @@ Bugzilla::DB - Database access routines, using L # Schema Information my @fields = $dbh->bz_get_field_defs(); my @fieldinfo = $dbh->bz_get_field_def($table, $column); - my @indexinfo = $dbh->bz_get_index_def($table, $index); + my @indexinfo = $dbh->bz_get_index_def($table, $field); my $exists = $dbh->bz_table_exists($table); =head1 DESCRIPTION @@ -595,6 +628,11 @@ would be 'MySQL.' You should not depend on this variable to know what type of database you are running on; this is intended merely for displaying to the admin to let them know what DB they're running. +=item C + +The name of the Bugzilla::DB module that we are. For example, for the MySQL +Bugzilla::DB module, this would be "Mysql." For PostgreSQL it would be "Pg." + =head1 CONNECTION A new database handle to the required database can be created using this @@ -898,11 +936,11 @@ These methods return info about the current Bugzilla database schema. Params: $table = the table that you want to count indexes on Returns: The number of indexes on the table. -=item C +=item C Description: Returns information about an index on a table in the database. Params: $table = name of table containing the index (scalar) - $index = name of the index (scalar) + $field = name of a field that the index is on (scalar) Returns: A reference to an array containing information about the index, with the following information in each array place: 0 = name of the table that the index is on -- cgit v1.2.3-24-g4f1b