Skip to content

Commit 5bedd05

Browse files
Merge pull request #1209 from sagarnasit/develop-v4
Fix migration into individual package
2 parents ab4a6b7 + b2c3760 commit 5bedd05

File tree

8 files changed

+203
-296
lines changed

8 files changed

+203
-296
lines changed

migrations/.gitkeep

Whitespace-only changes.

migrations/1_blank.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

php/EE/Migration/Executor.php

Lines changed: 195 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -5,137 +5,204 @@
55
use EE;
66
use EE\Model\Migration;
77
use EE\Utils;
8+
use Symfony\Component\Finder\Finder;
89

910
class Executor {
1011

11-
const MIGRATION_PATH = EE_ROOT . '/migrations/';
12-
13-
/**
14-
* Executes all pending migrations
15-
*/
16-
public static function execute_migrations() {
17-
18-
Utils\delem_log( "ee migration start" );
19-
EE::debug( "Executing migrations" );
20-
21-
$migrations = self::get_migrations_to_execute();
22-
23-
if( empty( $migrations ) ) {
24-
EE::debug( "Nothing to migrate" );
25-
return;
26-
}
27-
28-
sort( $migrations );
29-
30-
try {
31-
self::execute_migration_stack( $migrations );
32-
} catch( \Throwable $e ) {
33-
Utils\delem_log( "ee migration ended abruptly" );
34-
exit( 1 );
35-
}
36-
37-
EE::debug( "Successfully migrated EasyEngine" );
38-
}
39-
40-
/**
41-
* Executes all migrations passed to it recursively.
42-
* Also undo'es all migration if there was error executing any migration
43-
*/
44-
private static function execute_migration_stack( $migrations ) {
45-
if( empty( $migrations ) ) {
46-
return;
47-
}
48-
49-
$migration_path = self::get_migration_path( $migrations[0] );
50-
$migration_class_name = self::get_migration_class_name( $migrations[0] );
51-
52-
if( ! file_exists( $migration_path ) ) {
53-
EE::error( "Unable to find migration file at $migration_path", false );
54-
throw new Exception();
55-
}
56-
57-
require( $migration_path );
58-
59-
try {
60-
$migration = new $migration_class_name;
61-
if( ! $migration instanceof Base ) {
62-
throw new \Exception( "$migration_class_name is not a instance of base migration class" );
63-
}
64-
}
65-
catch( \Throwable $e ) {
66-
EE::error( $e->getMessage(), false );
67-
throw $e;
68-
}
69-
70-
try {
71-
EE::debug( "Migrating: $migrations[0]" );
72-
$migration->up();
73-
74-
Migration::create([
75-
'migration' => $migrations[0],
76-
'timestamp' => date('Y-m-d H:i:s'),
77-
]);
78-
79-
$migration->status = 'complete';
80-
EE::debug( "Migrated: $migrations[0]" );
81-
$remaining_migrations = array_splice( $migrations, 1, count( $migrations ) );
82-
self::execute_migration_stack( $remaining_migrations );
83-
}
84-
catch( \Throwable $e ) {
85-
if( $migration->status !== 'complete' ) {
86-
EE::error( "Errors were encountered while processing: $migrations[0]\n" . $e->getMessage(), false );
87-
}
88-
EE::debug( "Reverting: $migrations[0]" );
89-
$migration->down();
90-
EE::debug( "Reverted: $migrations[0]" );
91-
throw $e;
92-
}
93-
}
94-
95-
private static function get_migrations_to_execute() {
96-
return array_values(
97-
array_diff(
98-
self::get_migrations_from_fs(),
99-
self::get_migrations_from_db()
100-
)
101-
);
102-
}
103-
104-
private static function get_migrations_from_db() {
105-
return array_column( Migration::all(), 'migration' );
106-
}
107-
108-
private static function get_migrations_from_fs() {
109-
$migrations = scandir( self::MIGRATION_PATH );
110-
111-
if( ! Utils\inside_phar() ) {
112-
// array_slice is used to remove . and .. returned by scandir()
113-
$migrations = array_slice( $migrations, 2 );
12+
/**
13+
* Executes all pending migrations
14+
*/
15+
public static function execute_migrations() {
16+
17+
Utils\delem_log( 'ee migration start' );
18+
EE::debug( 'Executing migrations' );
19+
20+
$migrations = self::get_all_migrations();
21+
22+
if ( empty( $migrations ) ) {
23+
EE::debug( 'Nothing to migrate' );
24+
return;
25+
}
26+
27+
sort( $migrations );
28+
29+
try {
30+
self::execute_migration_stack( $migrations );
31+
} catch ( \Throwable $e ) {
32+
Utils\delem_log( 'ee migration ended abruptly' );
33+
exit( 1 );
34+
}
35+
36+
EE::debug( 'Successfully migrated EasyEngine' );
37+
}
38+
39+
/**
40+
* @return array of available migrations
41+
*/
42+
private static function get_all_migrations() {
43+
$migrations = [];
44+
$packages_path = scandir( EE_VENDOR_DIR . '/easyengine' );
45+
46+
// get migrations from packages.
47+
if ( ! empty( $packages_path ) ) {
48+
foreach ( $packages_path as $package ) {
49+
if ( '.' === $package || '..' === $package || is_file( $package ) ) {
50+
continue;
51+
}
52+
53+
$migration_path = EE_VENDOR_DIR . '/easyengine/' . $package . '/migrations';
54+
if ( is_dir( $migration_path ) ) {
55+
$files = scandir( $migration_path );
56+
if ( \EE\Utils\inside_phar() ) {
57+
$migrations[] = $files;
58+
} else{
59+
$migrations[] = array_slice( $files,2);
60+
}
61+
}
62+
}
63+
}
64+
65+
// get migrations from core.
66+
if ( is_dir( EE_ROOT . '/migrations' ) ) {
67+
$files = scandir( EE_ROOT . '/migrations' );
68+
if ( \EE\Utils\inside_phar() ) {
69+
$migrations[] = $files;
70+
} else{
71+
$migrations[] = array_slice( $files,2);
72+
}
73+
}
74+
75+
if ( ! empty( $migrations ) ) {
76+
$migrations = array_merge( ...$migrations );
77+
}
78+
79+
$migrations = self::get_migrations_to_execute( $migrations );
80+
81+
return array_filter( $migrations, function ( $file_name ) {
82+
if ( preg_match( '/^\d*[_]([a-zA-Z-]*)[_].*(\.php)$/', $file_name ) ) {
83+
return true;
84+
}
85+
return false;
86+
} );
87+
}
88+
89+
/**
90+
* Executes all migrations passed to it recursively.
91+
* Also undo'es all migration if there was error executing any migration
92+
*/
93+
private static function execute_migration_stack( $migrations ) {
94+
if ( empty( $migrations ) ) {
95+
return;
11496
}
11597

116-
array_walk( $migrations, function( &$migration, $index ) {
117-
$migration = rtrim( $migration, '.php' );
118-
});
119-
return $migrations;
120-
}
121-
122-
private static function get_migration_path( $migration_name ) {
123-
return self::MIGRATION_PATH . $migration_name . '.php' ;
124-
}
125-
126-
private static function get_migration_class_name( $migration_name ) {
127-
// Convet snake_case to CamelCase
128-
$class_name = self::camelize( $migration_name );
129-
// Replace dot with underscore
130-
$class_name = str_replace( '.', '_', $class_name );
131-
// Remove date from it
132-
$class_name = preg_replace( '/^\d*(?=[A-Z])/', '', $class_name );
133-
134-
return "\EE\Migration\\$class_name";
135-
}
136-
137-
private static function camelize($input, $separator = '_')
138-
{
139-
return str_replace($separator, '', ucwords($input, $separator));
140-
}
98+
$migration_path = self::get_migration_path( $migrations[0] );
99+
$migration_class_name = self::get_migration_class_name( $migrations[0] );
100+
101+
if ( ! file_exists( $migration_path ) ) {
102+
EE::error( "Unable to find migration file at $migration_path", false );
103+
throw new Exception();
104+
}
105+
106+
require( $migration_path );
107+
108+
try {
109+
$migration = new $migration_class_name;
110+
if ( ! $migration instanceof Base ) {
111+
throw new \Exception( "$migration_class_name is not a instance of base migration class" );
112+
}
113+
} catch ( \Throwable $e ) {
114+
EE::error( $e->getMessage(), false );
115+
throw $e;
116+
}
117+
118+
try {
119+
EE::debug( "Migrating: $migrations[0]" );
120+
$migration->up();
121+
122+
Migration::create( [
123+
'migration' => $migrations[0],
124+
'timestamp' => date( 'Y-m-d H:i:s' ),
125+
] );
126+
127+
$migration->status = 'complete';
128+
EE::debug( "Migrated: $migrations[0]" );
129+
$remaining_migrations = array_splice( $migrations, 1, count( $migrations ) );
130+
self::execute_migration_stack( $remaining_migrations );
131+
} catch ( \Throwable $e ) {
132+
if ( 'complete' !== $migration->status ) {
133+
EE::error( "Errors were encountered while processing: $migrations[0]\n" . $e->getMessage(), false );
134+
}
135+
EE::debug( "Reverting: $migrations[0]" );
136+
// remove db entry in 'migration' table when reverting migrations.
137+
$migrated = Migration::where( 'migration', $migrations[0] );
138+
if ( ! empty( $migrated ) ) {
139+
$migration->down();
140+
$migrated[0]->delete();
141+
}
142+
143+
EE::debug( "Reverted: $migrations[0]" );
144+
throw $e;
145+
}
146+
}
147+
148+
/**
149+
* Get migrations need to be executed.
150+
*
151+
* @param $path path to the migration directory.
152+
*
153+
* @return array
154+
*/
155+
private static function get_migrations_to_execute( $migrations ) {
156+
return array_values(
157+
array_diff(
158+
$migrations,
159+
self::get_migrations_from_db()
160+
)
161+
);
162+
}
163+
164+
/**
165+
* Get already migrated migrations from database.
166+
*
167+
* @return array
168+
*/
169+
private static function get_migrations_from_db() {
170+
return array_column( Migration::all(), 'migration' );
171+
}
172+
173+
/**
174+
* Get path of the migration file.
175+
*
176+
* @param $migration_name name of a migration file.
177+
*
178+
* @return string path of a migration file.
179+
*/
180+
private static function get_migration_path( $migration_name ) {
181+
preg_match( '/^\d*[_]([a-zA-Z-]*)[_]/', $migration_name, $matches );
182+
183+
if ( empty( $matches[1] ) ) {
184+
return '';
185+
}
186+
if ( 'easyengine' === $matches[1] ) {
187+
return EE_ROOT . "/migrations/$migration_name";
188+
} else {
189+
return EE_ROOT . "/vendor/easyengine/$matches[1]/migrations/$migration_name";
190+
}
191+
192+
}
193+
194+
private static function get_migration_class_name( $migration_name ) {
195+
// Remove date and package name from it
196+
$class_name = preg_replace( '/(^\d*)[_]([a-zA-Z-]*[_])/', '', rtrim( $migration_name, '.php' ) );
197+
// Convet snake_case to CamelCase
198+
$class_name = self::camelize( $class_name );
199+
// Replace dot with underscore
200+
$class_name = str_replace( '.', '_', $class_name );
201+
202+
return "\EE\Migration\\$class_name";
203+
}
204+
205+
private static function camelize( $input, $separator = '_' ) {
206+
return str_replace( $separator, '', ucwords( $input, $separator ) );
207+
}
141208
}

php/EE/Model/Cron.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

php/EE/Model/Migration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Migration extends Base {
1414
*/
1515
public static function get_migrations() {
1616

17-
$db = new EE_DB();
17+
$db = new \EE_DB();
1818
$migrations = $db->table( 'migrations' )
1919
->select( 'migration' )
2020
->get();

0 commit comments

Comments
 (0)