diff --git a/cli/database-upgrade.php b/cli/database-upgrade.php index d24e987..640dc7a 100755 --- a/cli/database-upgrade.php +++ b/cli/database-upgrade.php @@ -1,142 +1,149 @@ #!/usr/bin/php . /** * This is the script to upgrade the database * * It will execute the available database patches until the database * is to its latest version. * * To be honest, it also create the database schema if missing. */ require __DIR__ . '/../load.php'; echo <<limit( 1 ) ->queryRow(); } catch( Exception $e ) { $database_exists = false; } if( !$database_exists ) { // database schema installation echo "important tables are missing! assuming no database.\n"; echo "importing the schema for the first time\n"; execute_queries_from_file( "$documentation_path/schema.sql" ); // if we have not imported any database version, just set the latest one $version_exists = get_option( 'database_version', 0 ); if( !$version_exists ) { set_option( 'database_version', DATABASE_VERSION ); } } // get the current database version $current_database_version = get_option( 'database_version', 0 ); // notify about the current status printf( "current database version: %d\n", $current_database_version ); printf( "last database version: %d\n", DATABASE_VERSION ); // update to next database versions once at time while( $current_database_version < DATABASE_VERSION ) { $current_database_version++; // note that the patch name can have a name such as 0001-foo.sql $patch_name = sprintf( 'patch-%04d-*.sql', $current_database_version ); // path to the expected patch $patch_path = "$patch_directory/$patch_name"; // check if there is a database patch to be applied echo "looking for patch $patch_path\n"; $found = false; foreach( glob( $patch_path ) as $filename ) { execute_queries_from_file( $filename ); $found = true; } // actually the unexistence of a patch is good if( !$found ) { echo "\t skipped unexisting patch\n"; } // update the database version echo "\t increment database version to $current_database_version\n"; set_option( 'database_version', $current_database_version ); } echo "database upgrade end. good for you!\n"; /** * Execute some queries from a file * * @param string $file */ function execute_queries_from_file( $file ) { echo "\t executing queries from $file\n"; // get the patch content $queries = file_get_contents( $file ); // replace the database prefix with the current one $database_prefix = DB::instance()->getPrefix(); $queries = str_replace( '{$prefix}', $database_prefix, $queries ); // execute the patch queries (it will die in case of error) - multiquery( $queries ); + try { + multiquery( $queries ); + } catch( Exception $e ) { + echo "\n"; + printf( "ERROR:\n%s\n\n", $e->getMessage() ); + printf( "DEBUG QUERIES:\n%s\n", $queries ); + exit( 1 ); + } }