diff --git a/migrations/1_blank.php b/migrations/1_blank.php new file mode 100644 index 000000000..aafb96fa8 --- /dev/null +++ b/migrations/1_blank.php @@ -0,0 +1,16 @@ +up(); Migration::create([ @@ -78,7 +77,7 @@ private static function execute_migration_stack( $migrations ) { ]); $migration->status = 'complete'; - EE::log( "Migrated: $migrations[0]" ); + EE::debug( "Migrated: $migrations[0]" ); $remaining_migrations = array_splice( $migrations, 1, count( $migrations ) ); self::execute_migration_stack( $remaining_migrations ); } @@ -86,9 +85,9 @@ private static function execute_migration_stack( $migrations ) { if( $migration->status !== 'complete' ) { EE::error( "Errors were encountered while processing: $migrations[0]\n" . $e->getMessage(), false ); } - EE::log( "Reverting: $migrations[0]" ); + EE::debug( "Reverting: $migrations[0]" ); $migration->down(); - EE::log( "Reverted: $migrations[0]" ); + EE::debug( "Reverted: $migrations[0]" ); throw $e; } } @@ -103,13 +102,18 @@ private static function get_migrations_to_execute() { } private static function get_migrations_from_db() { - return Migration::all(); + return array_column( Migration::all(), 'migration' ); } private static function get_migrations_from_fs() { - // array_slice is used to remove . and .. returned by scandir() - $migrations = array_slice( scandir( self::MIGRATION_PATH ), 2 ); - array_walk( $migrations, function( &$migration, $index ) { + $migrations = scandir( self::MIGRATION_PATH ); + + if( ! Utils\inside_phar() ) { + // array_slice is used to remove . and .. returned by scandir() + $migrations = array_slice( $migrations, 2 ); + } + + array_walk( $migrations, function( &$migration, $index ) { $migration = rtrim( $migration, '.php' ); }); return $migrations; diff --git a/php/EE/Model/Option.php b/php/EE/Model/Option.php new file mode 100644 index 000000000..f026b0a87 --- /dev/null +++ b/php/EE/Model/Option.php @@ -0,0 +1,53 @@ + $key, + 'value' => $value, + ] + ); + } + + $existing_key->value = $value; + + return $existing_key->save(); + } + + /** + * Gets a key from options table + * + * @param string $key Key of option + * + * @throws \Exception + * @return bool|Option + */ + public function get( string $key ) { + $option = static::find( $key ); + + return false === $option ? false : $option->value; + } +} diff --git a/php/EE/Runner.php b/php/EE/Runner.php index 248f042f5..4d381c2c5 100644 --- a/php/EE/Runner.php +++ b/php/EE/Runner.php @@ -2,13 +2,15 @@ namespace EE; +use Composer\Semver\Comparator; use EE; -use EE\Utils; use EE\Dispatcher; use EE\Dispatcher\CompositeCommand; -use Mustangostang\Spyc; +use EE\Model\Option; +use EE\Utils; use Monolog\Logger; -use EE\Model\Site; +use Mustangostang\Spyc; + /** * Performs the execution of a command. * @@ -59,6 +61,17 @@ private function init_ee() { define( 'WEBROOT', \EE\Utils\trailingslashit( $this->config['sites_path'] ) ); define( 'DB', EE_CONF_ROOT.'/ee.sqlite' ); define( 'LOCALHOST_IP', '127.0.0.1' ); + + $this->maybe_trigger_migration(); + } + + /** + * Function to run migrations required to upgrade to the newer version. Will always be invoked from the newer phar downloaded inside the /tmp folder + */ + private function migrate() { + $rsp = new \EE\RevertableStepProcessor(); + $rsp->add_step( 'ee-migrations', 'EE\Migration\Executor::execute_migrations' ); + return $rsp->execute(); } /** @@ -778,6 +791,33 @@ private function auto_check_update() { exit; } + /** + * Triggers migration if current phar version > version in ee_option table + */ + private function maybe_trigger_migration() { + $db_version = Option::get( 'version' ); + $current_version = preg_replace( '/-nightly.*$/', '', EE_VERSION ); + + if ( ! $db_version ) { + $this->trigger_migration( $current_version ); + return; + } + + if ( Comparator::lessThan( $current_version, $db_version ) ) { + EE::error( 'It seems you\'re not running latest version. Please download and run latest version of EasyEngine' ); + } elseif ( Comparator::greaterThan( $current_version, $db_version ) ) { + EE::log( 'Executing migrations. This might take some time.' ); + $this->trigger_migration( $current_version ); + } + } + + private function trigger_migration( $version ) { + if ( ! $this->migrate() ) { + EE::error( 'There was some error while migrating. Please check logs.' ); + } + Option::set( 'version', $version ); + } + /** * Get a suggestion on similar (sub)commands when the user entered an * unknown (sub)command. diff --git a/php/class-ee-db.php b/php/class-ee-db.php index 0533eb362..6461fb301 100644 --- a/php/class-ee-db.php +++ b/php/class-ee-db.php @@ -127,6 +127,12 @@ private static function create_required_tables() { PRIMARY KEY (id) );'; + $query .= 'CREATE TABLE options ( + key VARCHAR NOT NULL, + value VARCHAR NOT NULL, + PRIMARY KEY (key) + );'; + try { self::$pdo->exec( $query ); } catch ( PDOException $exception ) { diff --git a/php/class-ee.php b/php/class-ee.php index 472af447b..ae1353a0c 100644 --- a/php/class-ee.php +++ b/php/class-ee.php @@ -94,7 +94,7 @@ public static function get_cache() { if ( ! $cache ) { $home = Utils\get_home_dir(); - $dir = getenv( 'EE_CACHE_DIR' ) ? : "$home/.ee/cache"; + $dir = getenv( 'EE_CACHE_DIR' ) ? : '/opt/easyengine/.cache/'; // 6 months, 300mb $cache = new FileCache( $dir, 15552000, 314572800 ); diff --git a/php/commands/src/CLI_Command.php b/php/commands/src/CLI_Command.php index 815d2af1a..5254a633d 100644 --- a/php/commands/src/CLI_Command.php +++ b/php/commands/src/CLI_Command.php @@ -1,9 +1,9 @@ migrate(); - } - } - - /** - * Function to run migrations required to upgrade to the newer version. Will always be invoked from the newer phar downloaded inside the /tmp folder - */ - private function migrate() { - $rsp = new \EE\RevertableStepProcessor(); - $rsp->add_step( 'ee-migrations', 'EE\Migration\Executor::execute_migrations' ); - $rsp->add_step( 'site-migration', 'EE\Migration\Containers::start_container_migration' ); - $rsp->execute(); } /** @@ -319,6 +305,9 @@ public function update( $_, $assoc_args ) { } else { EE::error( "md5 hash for download ({$md5_file}) is different than the release hash ({$release_hash})." ); } + + EE::log( 'Updating EasyEngine to new version. This might take some time.' ); + $php_binary = Utils\get_php_binary(); $process = EE\Process::create( "{$php_binary} $temp cli info" ); $result = $process->run(); diff --git a/utils/make-phar.php b/utils/make-phar.php index 7e4ef7d91..866c46ab1 100644 --- a/utils/make-phar.php +++ b/utils/make-phar.php @@ -16,9 +16,9 @@ require EE_VENDOR_DIR . '/autoload.php'; require EE_ROOT . '/php/utils.php'; -use Symfony\Component\Finder\Finder; -use EE\Utils; use EE\Configurator; +use EE\Utils; +use Symfony\Component\Finder\Finder; $configurator = new Configurator( EE_ROOT . '/utils/make-phar-spec.php' ); @@ -161,6 +161,7 @@ function get_composer_versions( $current_version ) { ->ignoreVCS(true) ->name('*.php') ->in(EE_ROOT . '/php') + ->in(EE_ROOT . '/migrations') ->in(EE_VENDOR_DIR . '/mustache') ->in(EE_VENDOR_DIR . '/rmccue/requests') ->in(EE_VENDOR_DIR . '/composer') @@ -221,6 +222,15 @@ function get_composer_versions( $current_version ) { add_file( $phar, $file ); } +if( 2 === count( scandir(__DIR__ . '/../migrations') ) ) { + $finder = new Finder(); + $finder->directories()->name('migrations')->in(EE_ROOT); + + foreach ( $finder as $file ) { + add_file( $phar, $file ); + } +} + $finder = new Finder(); $finder ->files()