diff --git a/cli/database-upgrade.php b/cli/database-upgrade.php index 186eb5c..a3651db 100755 --- a/cli/database-upgrade.php +++ b/cli/database-upgrade.php @@ -1,141 +1,138 @@ #!/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 <<getPrefix(); - // try to check if the database exists $database_exists = true; try { ( new UserAPI() ) ->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', 1 ); // 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++; $patch_name = sprintf( 'patch-%04d.sql', $current_database_version ); // path to the expected patch $patch_path = "$patch_directory/$patch_name"; echo "looking for patch $patch_path\n"; // check if there is a database patch to be applied if( file_exists( $patch_path ) ) { execute_queries_from_file( $patch_path ); } else { echo "\t skipped unexisting patch\n"; } echo "\t increment database version to $current_database_version\n"; // update the database version 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 ); }