diff --git a/documentation/database/patches/patch-0002-add-mailbox-lastsizebytes.sql b/documentation/database/patches/patch-0002-add-mailbox-lastsizebytes.sql new file mode 100644 index 0000000..70d902d --- /dev/null +++ b/documentation/database/patches/patch-0002-add-mailbox-lastsizebytes.sql @@ -0,0 +1 @@ +ALTER TABLE `{$prefix}mailbox` ADD COLUMN `mailbox_lastsizebytes` INT(10) UNSIGNED AFTER `mailbox_description`; diff --git a/documentation/database/patches/patch-0002-rename-denormalized-mailbox-lastsizebytes.sql b/documentation/database/patches/patch-0002-rename-denormalized-mailbox-lastsizebytes.sql deleted file mode 100644 index c4f2902..0000000 --- a/documentation/database/patches/patch-0002-rename-denormalized-mailbox-lastsizebytes.sql +++ /dev/null @@ -1,2 +0,0 @@ -ALTER TABLE `{$prefix}mailbox` CHANGE `mailbox_lastquotabytes` `mailbox_lastsizebytes` INT(10) UNSIGNED; - diff --git a/scripts/database-upgrade.php b/scripts/database-upgrade.php index cfca7c8..bfdfc12 100755 --- a/scripts/database-upgrade.php +++ b/scripts/database-upgrade.php @@ -1,153 +1,147 @@ #!/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/database-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 ); if( !$queries ) { throw new Exception( "missing file $file" ); } // replace the database prefix with the current one //$database_prefix = DB::instance()->getPrefix(); // this cannot work in this phase $database_prefix = $GLOBALS['prefix']; $queries = str_replace( '{$prefix}', $database_prefix, $queries ); // execute the patch queries (it will die in case of error) 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 ); } }