]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 346410: Move enum-table population code into Bugzilla::DB
authormkanat%bugzilla.org <>
Sat, 29 Jul 2006 18:50:02 +0000 (18:50 +0000)
committermkanat%bugzilla.org <>
Sat, 29 Jul 2006 18:50:02 +0000 (18:50 +0000)
Patch By Max Kanat-Alexander <mkanat@bugzilla.org> (module owner) a=justdave

Bugzilla/DB.pm
Bugzilla/DB/Mysql.pm
checksetup.pl

index 2cd974117b341085d3e8af11a4b0d86e7e06c0f6..b268a12f32f0c62246fd310ae1d6a234f03664ec 100644 (file)
@@ -43,12 +43,35 @@ use Bugzilla::Util;
 use Bugzilla::Error;
 use Bugzilla::DB::Schema;
 
+use List::Util qw(max);
+
 #####################################################################
 # Constants
 #####################################################################
 
 use constant BLOB_TYPE => DBI::SQL_BLOB;
 
+# Set default values for what used to be the enum types.  These values
+# are no longer stored in localconfig.  If we are upgrading from a
+# Bugzilla with enums to a Bugzilla without enums, we use the
+# enum values.
+#
+# The values that you see here are ONLY DEFAULTS. They are only used
+# the FIRST time you run checksetup.pl, IF you are NOT upgrading from a
+# Bugzilla with enums. After that, they are either controlled through
+# the Bugzilla UI or through the DB.
+use constant ENUM_DEFAULTS => {
+    bug_severity  => ['blocker', 'critical', 'major', 'normal',
+                      'minor', 'trivial', 'enhancement'],
+    priority     => ["P1","P2","P3","P4","P5"],
+    op_sys       => ["All","Windows","Mac OS","Linux","Other"],
+    rep_platform => ["All","PC","Macintosh","Other"],
+    bug_status   => ["UNCONFIRMED","NEW","ASSIGNED","REOPENED","RESOLVED",
+                     "VERIFIED","CLOSED"],
+    resolution   => ["","FIXED","INVALID","WONTFIX","LATER","REMIND",
+                     "DUPLICATE","WORKSFORME","MOVED"],
+};
+
 #####################################################################
 # Connection Methods
 #####################################################################
@@ -368,11 +391,18 @@ sub bz_setup_database {
     }
 }
 
-# The default implementation just returns what you passed-in. This function
-# really exists just to be overridden in Bugzilla::DB::Mysql.
+# This really just exists to get overridden in Bugzilla::DB::Mysql.
 sub bz_enum_initial_values {
-    my ($self, $enum_defaults) = @_;
-    return $enum_defaults;
+    return ENUM_DEFAULTS;
+}
+
+sub bz_populate_enum_tables {
+    my ($self) = @_;
+
+    my $enum_values = $self->bz_enum_initial_values();
+    while (my ($table, $values) = each %$enum_values) {
+        $self->_bz_populate_enum_table($table, $values);
+    }
 }
 
 #####################################################################
@@ -952,6 +982,30 @@ sub _bz_store_real_schema {
     $sth->execute();
 }
 
+# For bz_populate_enum_tables
+sub _bz_populate_enum_table {
+    my ($self, $table, $valuelist) = @_;
+
+    my $sql_table = $self->quote_identifier($table);
+
+    # Check if there are any table entries
+    my $table_size = $self->selectrow_array("SELECT COUNT(*) FROM $sql_table");
+
+    # If the table is empty...
+    if (!$table_size) {
+        my $insert = $self->prepare(
+            "INSERT INTO $sql_table (value,sortkey) VALUES (?,?)");
+        print "Inserting values into the '$table' table:\n";
+        my $sortorder = 0;
+        my $maxlen    = max(map(length($_), @$valuelist)) + 2;
+        foreach my $value (@$valuelist) {
+            $sortorder += 100;
+            printf "%-${maxlen}s sortkey: $sortorder\n", "'$value'";
+            $insert->execute($value, $sortorder);
+        }
+    }
+}
+
 1;
 
 __END__
@@ -1331,15 +1385,17 @@ the database.
 
 =over 4
 
-=item C<bz_enum_initial_values(\%enum_defaults)>
+=item C<bz_populate_enum_tables()>
+
+Description: For an upgrade or an initial installation, populates
+             the tables that hold the legal values for the old
+             "enum" fields: C<bug_severity>, C<resolution>, etc.
+             Prints out information if it inserts anything into the
+             DB.
 
- Description: For an upgrade or an initial installation, provides
-              what the values should be for the "enum"-type fields,
-              such as version, op_sys, rep_platform, etc.
- Params:      \%enum_defaults - The default initial list of values for
-                                each enum field. A hash, with the field
-                                names pointing to an arrayref of values.
- Returns:     A hashref with the correct initial values for the enum fields.
+Params:      none
+
+Returns:     nothing
 
 =back
 
index 5cf588983767b62b0898d2a1bfac0467747a879a..2f23e83456f9e9047b8fb8148d33a2f178d2505e 100644 (file)
@@ -532,8 +532,8 @@ sub bz_setup_database {
 
 
 sub bz_enum_initial_values {
-    my ($self, $enum_defaults) = @_;
-    my %enum_values = %$enum_defaults;
+    my ($self) = @_;
+    my %enum_values = %{$self->ENUM_DEFAULTS};
     # Get a complete description of the 'bugs' table; with DBD::MySQL
     # there isn't a column-by-column way of doing this.  Could use
     # $dbh->column_info, but it would go slower and we would have to
index 3221c69a90314da3eacc0f6618849b13ca9cfdd0..a08ae08f9c968edef9d5c78e60f914df4e9fbf69 100755 (executable)
@@ -339,74 +339,8 @@ my $dbh = Bugzilla->dbh;
 # Note: table definitions are now in Bugzilla::DB::Schema.
 $dbh->bz_setup_database();
 
-###########################################################################
-# Detect changed local settings
-###########################################################################
-
-# Nick Barnes nb+bz@ravenbrook.com 2005-10-05
-# 
-# PopulateEnumTable($table, @values): if the table $table has no
-# entries, fill it with the entries in the list @values, in the same
-# order as that list.
-
-sub PopulateEnumTable {
-    my ($table, @valuelist) = @_;
-
-    # If we encounter any of the keys in this hash, they are 
-    # automatically set to isactive=0
-    my %defaultinactive = ('---' => 1);
-
-    # Check if there are any table entries
-    my $query = "SELECT COUNT(id) FROM $table";
-    my $sth = $dbh->prepare($query);
-    $sth->execute();
-
-    # If the table is empty...
-    if ( !$sth->fetchrow_array() ) {
-        my $insert = $dbh->prepare("INSERT INTO $table"
-            . " (value,sortkey,isactive) VALUES (?,?,?)");
-        my $sortorder = 0;
-        foreach my $value (@valuelist) {
-            $sortorder = $sortorder + 100;
-            # Not active if the value exists in $defaultinactive
-            my $isactive = exists($defaultinactive{$value}) ? 0 : 1;
-            print "Inserting value '$value' in table $table" 
-                . " with sortkey $sortorder...\n";
-            $insert->execute($value, $sortorder, $isactive);
-        }
-    }
-}
-
-# Set default values for what used to be the enum types.  These values
-# are no longer stored in localconfig.  If we are upgrading from a
-# Bugzilla with enums to a Bugzilla without enums, we use the
-# enum values.
-#
-# The values that you see here are ONLY DEFAULTS. They are only used
-# the FIRST time you run checksetup, IF you are NOT upgrading from a
-# Bugzilla with enums. After that, they are either controlled through
-# the Bugzilla UI or through the DB.
-
-my $enum_defaults = {
-    bug_severity  => ['blocker', 'critical', 'major', 'normal',
-                      'minor', 'trivial', 'enhancement'],
-    priority     => ["P1","P2","P3","P4","P5"],
-    op_sys       => ["All","Windows","Mac OS","Linux","Other"],
-    rep_platform => ["All","PC","Macintosh","Other"],
-    bug_status   => ["UNCONFIRMED","NEW","ASSIGNED","REOPENED","RESOLVED",
-                     "VERIFIED","CLOSED"],
-    resolution   => ["","FIXED","INVALID","WONTFIX","LATER","REMIND",
-                     "DUPLICATE","WORKSFORME","MOVED"],
-};
-
-# Get all the enum column values for the existing database, or the
-# defaults if the columns are not enums.
-my $enum_values = $dbh->bz_enum_initial_values($enum_defaults);
-
-# Populate the enum tables.
-while (my ($table, $values) = each %$enum_values) {
-    PopulateEnumTable($table, @$values);
-}
+# Populate the tables that hold the values for the <select> fields.
+$dbh->bz_populate_enum_tables();
 
 ###########################################################################
 # Check data directory