Index: Bugzilla/Config.pm =================================================================== --- Bugzilla/Config.pm +++ Bugzilla/Config.pm @@ -78,6 +78,9 @@ sub SetParam { my ($name, $value) = @_; + # Initialize the parameters if none exist (first time) and reload + update_params() if !keys %{Bugzilla->params}; + _load_params unless %params; die "Unknown param $name" unless (exists $params{$name}); @@ -100,10 +103,11 @@ my ($params) = @_; my $answer = Bugzilla->installation_answers; + my $param = read_params(); + # Check to see if we need to migrate old file based parameters - _migrate_db_parameters(); + $param = _migrate_file_parameters($param); - my $param = read_params(); my %new_params; # If we didn't return any param values, then this is a new installation. @@ -260,11 +264,8 @@ my ($param_data) = @_; $param_data ||= Bugzilla->params; - return if $ENV{CI} && scalar keys %{$param_data} < 6; - try { my $dbh = Bugzilla->dbh; - $dbh->bz_start_transaction(); foreach my $key (keys %{$param_data}) { if (my $param = Bugzilla::Config::Param->new({name => $key})) { my $value = $param_data->{$key} || ($param->is_numeric ? 0 : ''); @@ -279,7 +280,6 @@ Bugzilla::Config::Param->create({name => $key, value => $value}); } } - $dbh->bz_commit_transaction(); } catch { WARN("Database not available: $_"); @@ -292,6 +292,7 @@ sub read_params { my %params; + try { my @all_params = Bugzilla::Config::Param->get_all(); if (@all_params) { @@ -310,37 +311,33 @@ return \%params; } -sub _migrate_db_parameters { +sub _migrate_file_parameters { + my $params = shift; + # Return if the old data/params file has already been removed my $datadir = bz_locations()->{'datadir'}; return if !-f "$datadir/params"; - # Return if we have already populated the params table before - my $count = 0; - try { - $count = Bugzilla->dbh->selectrow_array('SELECT COUNT(id) FROM params'); - } - catch { - WARN("Database not available: $_"); - return; - }; - return if $count; - - WARN('Migrating old parameters from data/params to database'); - # Read in the old data/params values my $s = Safe->new; $s->rdo("$datadir/params"); die "Error reading $datadir/params: $!" if $!; die "Error evaluating $datadir/params: $@" if $@; - my $params = $s->varglob('param'); + my $file_params = $s->varglob('param'); + return if !%{$file_params}; + + WARN('Migrating old parameters from data/params to database'); # Insert the key/values into the params table - foreach my $key (keys %{$params}) { - my $value = $params->{$key}; - next if !defined $value; - Bugzilla->dbh->do('INSERT INTO params (name, value) VALUES (?, ?)', undef, $key, $value); + foreach my $key (keys %{$file_params}) { + $params->{$key} = $file_params->{$key} || ''; } + + # Move the params file so we do not run this again + rename("$datadir/params", "$datadir/params.old") + or die "Rename params file failed: $!"; + + return $params; } 1; Index: scripts/entrypoint.pl =================================================================== --- scripts/entrypoint.pl +++ scripts/entrypoint.pl @@ -277,7 +277,6 @@ sub check_data_dir { die "/app/data must be writable by user 'app' (id: $EUID)" unless -w '/app/data'; - die '/app/data/params must exist' unless -f '/app/data/params'; } sub check_env { Index: t/migrate-params-db.t =================================================================== --- t/migrate-params-db.t +++ t/migrate-params-db.t @@ -23,17 +23,17 @@ close $data_fh; close DATA; +ok(-f "$datadir/params", 'Original parameters file exists'); + # We run import manually instead of with 'use' so that -# these load after the data/params file has been written. +# this loads after the data/params file has been written. require Bugzilla::Test::MockDB; Bugzilla::Test::MockDB->import; -require Bugzilla::Test::MockParams; -Bugzilla::Test::MockParams->import; # Read file back as a Perl data structure for comparison -ok(-f "$datadir/params", 'Parameters file exists'); +ok(-f "$datadir/params.old", 'Backup parameters file exists'); my $s = Safe->new; -$s->rdo("$datadir/params"); +$s->rdo("$datadir/params.old"); my %file_params = %{$s->varglob('param')}; ok(scalar keys %file_params, 'Parameters file read back correctly'); @@ -55,7 +55,5 @@ 'github_client_id' => 'GITHUB_CLIENT_ID', 'honeypot_api_key' => 'HONEYPOT_API_KEY', 'iprepd_client_secret' => 'IPREPD_CLIENT_SECRET', - 'mfa_group' => 'editbugs', 'phabricator_api_key' => 'PHABRICATOR_API_KEY', - 'webhooks_group' => 'editbugs', );