Skip to content
Merged
16 changes: 16 additions & 0 deletions migrations/1_blank.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace EE\Migration;

/**
* Placeholder migration as we need migration directory in phar but we don't have any migration files.
*
* @package EE\Migration
*/
class Blank extends \EE\Migration\Base {
public function up() {
}

public function down() {
}
}
38 changes: 21 additions & 17 deletions php/EE/Migration/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,27 @@

namespace EE\Migration;

use \EE;
use \EE\Model\Migration;
use \EE\Utils;
use Symfony\Component\Finder\Finder;
use EE;
use EE\Model\Migration;
use EE\Utils;

class Executor {

const MIGRATION_PATH = EE_ROOT . '/migrations';
const MIGRATION_PATH = EE_ROOT . '/migrations/';

/**
* Executes all pending migrations
*/
public static function execute_migrations() {

Utils\delem_log( "ee migration start" );
EE::log( "Migrating EasyEngine data to new version" );
EE::debug( "Executing migrations" );

$migrations = self::get_migrations_to_execute();

if( empty( $migrations ) ) {
EE::success( "Noting to migrate" );
exit( 0 );
EE::debug( "Nothing to migrate" );
return;
}

sort( $migrations );
Expand All @@ -35,7 +34,7 @@ public static function execute_migrations() {
exit( 1 );
}

EE::success( "Successfully migrated EasyEngine" );
EE::debug( "Successfully migrated EasyEngine" );
}

/**
Expand Down Expand Up @@ -69,7 +68,7 @@ private static function execute_migration_stack( $migrations ) {
}

try {
EE::log( "Migrating: $migrations[0]" );
EE::debug( "Migrating: $migrations[0]" );
$migration->up();

Migration::create([
Expand All @@ -78,17 +77,17 @@ 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 );
}
catch( \Throwable $e ) {
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;
}
}
Expand All @@ -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;
Expand Down
53 changes: 53 additions & 0 deletions php/EE/Model/Option.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace EE\Model;

/**
* Option model class.
*/
class Option extends Base {

protected static $table = 'options';

protected static $primary_key = 'key';

/**
* Sets a key on options table
*
* @param string $key Key of option
* @param string $value Value of option
*
* @throws \Exception
* @return bool Key set or not
*/
public function set( string $key, string $value ) {
$existing_key = static::find( $key );

if ( empty( $existing_key ) ) {
return static::create(
[
'key' => $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;
}
}
46 changes: 43 additions & 3 deletions php/EE/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions php/class-ee-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
2 changes: 1 addition & 1 deletion php/class-ee.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
25 changes: 7 additions & 18 deletions php/commands/src/CLI_Command.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

use \Composer\Semver\Comparator;
use \Symfony\Component\Filesystem\Filesystem;
use \EE\Utils;
use \EE\Model\Site;
use Composer\Semver\Comparator;
use EE\Model\Site;
use EE\Utils;
use Symfony\Component\Filesystem\Filesystem;

/**
* Review current EE info, check for updates, or see defined aliases.
Expand Down Expand Up @@ -154,20 +154,6 @@ public function info( $_, $assoc_args ) {
\EE::line( $line );
}
}

if ( '/tmp' === getcwd() ) {
$this->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();
}

/**
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 12 additions & 2 deletions utils/make-phar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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()
Expand Down