Skip to content

Commit

Permalink
Convert redirect_delay column back to integer
Browse files Browse the repository at this point in the history
Follow-up fix for user_pref.redirect_delay, which was incorrectly
set to boolean in check_pgsql_bool_columns() before MantisBT 2.23.0,
so we need to check its type and convert it back to integer if needed.

Fixes #26109
  • Loading branch information
dregad committed Sep 22, 2019
1 parent 393bca8 commit 25110fd
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
39 changes: 39 additions & 0 deletions admin/install.php
Expand Up @@ -934,6 +934,45 @@ function print_test( $p_test_description, $p_result, $p_hard_fail = true, $p_mes
}
}
}
# Follow-up fix for user_pref.redirect_delay, which was incorrectly
# set to boolean in check_pgsql_bool_columns() before MantisBT 2.23.0,
# so we need to check its type and convert it back to integer if needed.
# See issue #26109.
elseif( $f_db_type == 'pgsql' && $t_last_update > 43
&& version_compare( MANTIS_VERSION, '2.23.0', '<=' )
) {
$t_table = db_get_table( 'user_pref' );
$t_column = 'redirect_delay';

try {
$t_is_integer = pgsql_get_column_type( $t_table, $t_column ) == 'integer';
$t_msg = "Column must be converted to INTEGER";
$t_exception_occured = false;
}
catch( Exception $e ) {
$t_exception_occured = true;
$t_msg = $e->getMessage();
}

print_test(
"PostgreSQL: check column '$t_table.$t_column' data type",
!$t_exception_occured && $t_is_integer,
/* hard fail */ $t_exception_occured,
$t_msg
);
if( !$t_exception_occured && !$t_is_integer ) {
$t_sqlarray = $t_dict->AlterColumnSQL( $t_table,
'redirect_delay I NOTNULL DEFAULT 0'
);
print_test(
"Converting column '$t_table.$t_column'' to INTEGER",
2 == $t_dict->ExecuteSQLArray( $t_sqlarray, false ),
true,
print_r( $t_sqlarray, true )
);
}
}

# End of special processing for specific schema versions

while( ( $i <= $t_last_id ) && !$g_failed ) {
Expand Down
39 changes: 39 additions & 0 deletions core/install_helper_functions_api.php
Expand Up @@ -151,6 +151,45 @@ function check_pgsql_bool_columns() {
return $t_result->GetArray();
}

/**
* Get pgsql column's data type
*
* @param string $p_table Table name
* @param string $p_column Column name
*
* @return string column data_type
*
* @throws Exception
*/
function pgsql_get_column_type( $p_table, $p_column ) {
global $f_database_name;
/** @var ADOConnection $g_db */
global $g_db;

# Generate SQL to check columns against schema
$t_sql = 'SELECT data_type
FROM information_schema.columns
WHERE table_catalog = $1
AND table_name = $2
AND column_name = $3';
$t_param = array(
$f_database_name,
db_get_table( $p_table ),
$p_column,
);

/** @var ADORecordSet $t_result */
$t_result = @$g_db->execute( $t_sql, $t_param );
if( $t_result === false ) {
throw new Exception( 'Unable to check information_schema' );
} else if( $t_result->recordCount() == 0 ) {
throw new Exception( "Column '$p_column' not found in table '$p_table'" );
}

$t_rows = $t_result->getAll();
return reset( $t_rows[0] );
}

/**
* Set the value of $g_db_log_queries as specified
* This is used by install callback functions to ensure that only the relevant
Expand Down

0 comments on commit 25110fd

Please sign in to comment.