instance. */ private static $executed = []; /** * Executes pending migrations of container. */ public static function execute_migrations() { Utils\delem_log( 'ee migration start' ); EE::debug( 'Executing custom container migrations' ); self::$executed = []; $migrations = self::get_all_migrations(); if ( empty( $migrations ) ) { EE::debug( 'No custom container migration' ); return; } sort( $migrations ); try { self::execute_migration_stack( $migrations ); } catch ( \Throwable $e ) { Utils\delem_log( 'ee migration ended abruptly' ); // Rethrown, not exit(1): the migration step processor reports it and rolls back. throw $e; } EE::debug( 'Successfully migrated EasyEngine' ); } /** * Reverts the container migrations completed by this invocation, newest first, when a later upgrade step fails. * * Their rows are deleted so the next attempt runs them again. Migrations recorded by earlier runs are not touched. */ public static function revert_executed_migrations() { $executed = array_reverse( self::$executed, true ); self::$executed = []; if ( empty( $executed ) ) { return; } // On a fresh install they are no-ops by design, and older down() methods assume an upgrade. if ( ! \EE\Model\Option::get( 'version' ) ) { EE::debug( 'Fresh install: not reverting container migrations' ); return; } foreach ( $executed as $name => $migration ) { EE::debug( "Reverting: $name" ); $reverted = true; try { $migration->down(); } catch ( \Throwable $e ) { $reverted = false; EE::warning( "Could not revert container migration $name: " . $e->getMessage() ); } // Deleted even if down() failed: migrations are idempotent, and a retry must run it again. try { foreach ( Migration::where( 'migration', $name ) as $row ) { $row->delete(); } EE::debug( $reverted ? "Reverted: $name" : "Removed the migrations row of $name" ); } catch ( \Throwable $e ) { EE::warning( "Could not delete the migrations row of $name: " . $e->getMessage() ); } } } /** * Keeps the container migrations completed by this invocation: a later failure no longer reverts them. */ public static function keep_executed_migrations() { self::$executed = []; EE::debug( 'Keeping the container migrations of this run' ); } /** * @return array of available migrations */ private static function get_all_migrations() { $migrations = []; $packages_path = scandir( EE_VENDOR_DIR . '/easyengine' ); // get migrations from packages. if ( ! empty( $packages_path ) ) { foreach ( $packages_path as $package ) { if ( '.' === $package || '..' === $package || is_file( $package ) ) { continue; } $migration_path = EE_VENDOR_DIR . '/easyengine/' . $package . '/migrations/container'; if ( is_dir( $migration_path ) ) { $files = scandir( $migration_path ); if ( \EE\Utils\inside_phar() ) { $migrations[] = $files; } else { $migrations[] = array_slice( $files, 2 ); } } } } // get migrations from core. if ( is_dir( EE_ROOT . '/migrations/container' ) ) { $files = scandir( EE_ROOT . '/migrations/container' ); if ( \EE\Utils\inside_phar() ) { $migrations[] = $files; } else { $migrations[] = array_slice( $files, 2 ); } } if ( ! empty( $migrations ) ) { $migrations = array_merge( ...$migrations ); } $migrations = self::get_migrations_to_execute( $migrations ); return array_filter( $migrations, function ( $file_name ) { if ( preg_match( '/^\d*[_]([a-zA-Z-]*)[_].*(\.php)$/', $file_name ) ) { return true; } return false; } ); } /** * Executes all migrations passed to it recursively. * Also undo'es all migration if there was error executing any migration * * @param $migrations array of new migrations. * * @throws EE\ExitException * @throws \Throwable */ private static function execute_migration_stack( $migrations ) { if ( empty( $migrations ) ) { return; } $migration_path = self::get_migration_path( $migrations[0] ); $migration_class_name = self::get_migration_class_name( $migrations[0] ); if ( ! file_exists( $migration_path ) ) { EE::error( "Unable to find migration file at $migration_path", false ); throw new \Exception( "Unable to find migration file at $migration_path" ); } require( $migration_path ); try { $migration = new $migration_class_name; if ( ! $migration instanceof Base ) { throw new \Exception( "$migration_class_name is not a instance of base migration class" ); } } catch ( \Throwable $e ) { EE::error( $e->getMessage(), false ); throw $e; } try { EE::debug( "Migrating: $migrations[0]" ); $migration->up(); Migration::create( [ 'migration' => $migrations[0], 'type' => 'container', 'timestamp' => date( 'Y-m-d H:i:s' ), ] ); $migration->status = 'complete'; self::$executed[ $migrations[0] ] = $migration; EE::debug( "Migrated: $migrations[0]" ); $remaining_migrations = array_splice( $migrations, 1, count( $migrations ) ); self::execute_migration_stack( $remaining_migrations ); } catch ( \Throwable $e ) { if ( 'complete' !== $migration->status ) { EE::error( "Errors were encountered while processing: $migrations[0]\n" . $e->getMessage(), false ); } EE::debug( "Reverting: $migrations[0]" ); // remove db entry in 'migration' table when reverting migrations. $migrated = Migration::where( 'migration', $migrations[0] ); if ( ! empty( $migrated ) ) { $migration->down(); $migrated[0]->delete(); } unset( self::$executed[ $migrations[0] ] ); EE::debug( "Reverted: $migrations[0]" ); throw $e; } } /** * Get migrations to be executed. * * @param $migrations array of all migrations. * * @return array * @throws \Exception */ private static function get_migrations_to_execute( $migrations ) { return array_values( array_diff( $migrations, self::get_migrations_from_db() ) ); } /** * Get already migrated migrations from database. * * @return array|void * @throws \Exception */ private static function get_migrations_from_db() { return array_column( Migration::where( 'type', 'container' ), 'migration' ); } /** * Get path of the migration file. * * @param $migration_name name of a migration file. * * @return string path of a migration file. */ private static function get_migration_path( $migration_name ) { preg_match( '/^\d*[_]([a-zA-Z-]*)[_]/', $migration_name, $matches ); if ( empty( $matches[1] ) ) { return ''; } if ( 'easyengine' === $matches[1] ) { return EE_ROOT . "/migrations/container/$migration_name"; } else { return EE_ROOT . "/vendor/easyengine/$matches[1]/migrations/container/$migration_name"; } } /** * Get class name from name of migration file. * * @param $migration_name string name of migration file. * * @return string */ private static function get_migration_class_name( $migration_name ) { // Remove date and package name from it $class_name = preg_replace( '/(^\d*)[_]([a-zA-Z-]*[_])/', '', substr( $migration_name, 0, -4 ) ); // Convet snake_case to CamelCase $class_name = self::camelize( $class_name ); // Replace dot with underscore $class_name = str_replace( '.', '_', $class_name ); return "\EE\Migration\\$class_name"; } /** * Convert string in camelcase format. * * @param $input string to be camelized. * @param string $separator string of separator. * * @return mixed */ private static function camelize( $input, $separator = '_' ) { return str_replace( $separator, '', ucwords( $input, $separator ) ); } }