Skip to content

Commit

Permalink
Improve installer when generating SQL script
Browse files Browse the repository at this point in the history
  • Loading branch information
dregad committed Feb 17, 2020
2 parents 8975002 + 449844c commit 0a23feb
Showing 1 changed file with 66 additions and 7 deletions.
73 changes: 66 additions & 7 deletions admin/install.php
Expand Up @@ -871,7 +871,20 @@ function print_test( $p_test_description, $p_result, $p_hard_fail = true, $p_mes
$t_last_id = count( $g_upgrade ) - 1;
$i = $t_last_update + 1;
if( $f_log_queries ) {
echo '<tr><td> <span class="bigger-120">Database Creation Suppressed, SQL Queries follow</span> <pre>';
echo '<tr><td><span class="bigger-120">Database Creation Suppressed, SQL Queries follow</span>';

echo '<div class="space-6"></div>';
echo '<div class="alert alert-warning">';
echo "Please note that executing the generated script below <strong>may not result in a fully functional "
. "database</strong>, particularly in upgrade scenarios. This is due to the fact that some upgrade "
. "steps require the execution of PHP code; these <em>Upgrade Functions</em> are defined in "
. '<a href="https://github.com/mantisbt/mantisbt/blob/master/core/install_helper_functions_api.php">install_helper_functions_api.php</a>'
. " and cannot be translated to SQL statements. Use at your own risk.";
echo '</div>';

echo '<pre>';
echo "-- MantisBT " . MANTIS_VERSION . " Database creation script". PHP_EOL;
echo "-- " . date("c") . PHP_EOL . PHP_EOL;
}

# Make sure we do the upgrades using UTF-8 if needed
Expand Down Expand Up @@ -1010,6 +1023,8 @@ function print_test( $p_test_description, $p_result, $p_hard_fail = true, $p_mes
break;

case null:
$t_sqlarray = array();
$t_sql = false;
# No-op upgrade step - required for oci8
break;

Expand All @@ -1029,17 +1044,38 @@ function print_test( $p_test_description, $p_result, $p_hard_fail = true, $p_mes
break;
}
if( $f_log_queries ) {
echo "-- Schema step $i" . PHP_EOL;
if( $t_sql ) {
foreach( $t_sqlarray as $t_sql ) {
foreach( $t_sqlarray as $t_statement ) {
# "CREATE OR REPLACE TRIGGER" statements must end with "END;\n/" for Oracle sqlplus
if( $f_db_type == 'oci8' && stripos( $t_sql, 'CREATE OR REPLACE TRIGGER' ) === 0 ) {
if( $f_db_type == 'oci8' && stripos( $t_statement, 'CREATE OR REPLACE TRIGGER' ) === 0 ) {
$t_sql_end = PHP_EOL . '/';
} else {
$t_sql_end = ';';
}
echo htmlentities( $t_sql ) . $t_sql_end . PHP_EOL . PHP_EOL;
echo htmlentities( $t_statement ) . $t_sql_end;
}
} elseif( $t_sqlarray ) {
echo "-- Execute PHP Update Function: install_" . htmlentities( $t_sqlarray[0] ) . "(";
# Convert the parameters array to a printable string
if( isset( $t_sqlarray[1] ) ) {
$t_params = array();
foreach( $t_sqlarray[1] as $t_param ) {
$t_value = var_export( $t_param, true );
if( is_array( $t_param ) ) {
# Remove unnecessary array keys, newlines and the trailing comma
$t_value = preg_replace( '/\s*[0-9]+ => /', ' ', $t_value );
$t_value = str_replace( ",\n", ' ', $t_value );
}
$t_params[] = $t_value;
}
echo htmlentities( implode( ', ', $t_params ) );
}
echo ")";
} else {
echo "-- No operation";
}
echo PHP_EOL . PHP_EOL;
} else {
echo 'Schema step ' . $i . ': ';
if( is_null( $g_upgrade[$i][0] ) ) {
Expand Down Expand Up @@ -1078,8 +1114,26 @@ function print_test( $p_test_description, $p_result, $p_hard_fail = true, $p_mes
}
if( $f_log_queries ) {
# add a query to set the database version
echo 'INSERT INTO ' . db_get_table( 'config' ) . ' ( value, type, access_reqd, config_id, project_id, user_id ) VALUES (\'' . $t_last_id . '\', 1, 90, \'database_version\', 0, 0 );' . PHP_EOL;
echo '</pre><br /><p style="color:red">Your database has not been created yet. Please create the database, then install the tables and data using the information above before proceeding.</p></td></tr>';
echo "-- Set database version" . PHP_EOL;
if( $t_last_update == -1 ) {
echo "INSERT INTO " . db_get_table( 'config' )
. " ( value, type, access_reqd, config_id, project_id, user_id )"
. " VALUES ($t_last_id, 1, 90, 'database_version', 0, 0 );"
. PHP_EOL;
} else {
echo "UPDATE " . db_get_table( 'config' )
. " SET value = $t_last_id"
. " WHERE config_id = 'database_version' AND project_id = 0 AND user_id = 0;"
. PHP_EOL;
}
echo '</pre>';

echo '<div class="space-6"></div>';
echo '<div class="alert alert-danger">';
echo "<strong>Your database is not ready yet !</strong> "
. "Please create it, then install the tables and data using the above script before proceeding.";
echo '</div>';
echo '</td></tr>';
}
}
if( false == $g_failed ) {
Expand Down Expand Up @@ -1407,12 +1461,17 @@ function print_test( $p_test_description, $p_result, $p_hard_fail = true, $p_mes
<tr>
<td>
<span class="bigger-130">
<?php if( $f_log_queries ) { ?>
SQL script generated successfully.
Use it to manually create or upgrade your database.
<?php } else { ?>
MantisBT was installed successfully.
<?php if( $f_db_exists ) {?>
<?php if( $f_db_exists ) { ?>
<a href="../login_page.php">Continue</a> to log in.
<?php } else { ?>
Please log in as the administrator and <a href="../login_page.php">create</a> your first project.
</span>
<?php } ?>
<?php } ?>
</td>
<?php print_test_result( GOOD ); ?>
Expand Down

0 comments on commit 0a23feb

Please sign in to comment.