Skip to content

Commit 3e2de74

Browse files
Merge pull request #1255 from sagarnasit/change-db-migration-paths
Add 'type' column in migrations table and move db migrations path
2 parents 84440ad + cee3a5a commit 3e2de74

File tree

4 files changed

+76
-10
lines changed

4 files changed

+76
-10
lines changed

migrations/.gitkeep

Whitespace-only changes.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace EE\Migration;
4+
5+
use EE;
6+
use EE\Migration\Base;
7+
8+
class InsertDockerImagesVersion extends Base {
9+
10+
private static $pdo;
11+
12+
public function __construct() {
13+
14+
try {
15+
self::$pdo = new \PDO( 'sqlite:' . DB );
16+
self::$pdo->setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION );
17+
} catch ( \PDOException $exception ) {
18+
EE::error( $exception->getMessage() );
19+
}
20+
21+
}
22+
23+
/**
24+
* Execute create table query for site and sitemeta table.
25+
*
26+
* @throws EE\ExitException
27+
*/
28+
public function up() {
29+
30+
EE::log( 'Checking/Pulling required images' );
31+
$images = EE\Utils\get_image_versions();
32+
33+
$query = '';
34+
foreach ( $images as $image => $tag ) {
35+
EE::debug( "Checking/Pulling docker image $image:$tag" );
36+
if ( ! \EE::exec( "docker pull ${image}:${tag}" ) ) {
37+
throw new \Exception( "Unable to pull ${image}:${tag}. Please check logs for more details." );
38+
}
39+
$query .= "INSERT INTO options VALUES( '${image}', '${tag}' );";
40+
}
41+
42+
try {
43+
self::$pdo->exec( $query );
44+
} catch ( PDOException $exception ) {
45+
EE::error( 'Encountered Error while inserting in "options" table: ' . $exception->getMessage(), false );
46+
}
47+
}
48+
49+
/**
50+
* Execute drop table query for site and sitemeta table.
51+
*
52+
* @throws EE\ExitException
53+
*/
54+
public function down() {
55+
56+
$query = "DELETE FROM options WHERE key LIKE 'easyengine/%';";
57+
58+
try {
59+
self::$pdo->exec( $query );
60+
} catch ( PDOException $exception ) {
61+
EE::error( 'Encountered Error while deleting from "options" table: ' . $exception->getMessage(), false );
62+
}
63+
}
64+
}

php/EE/Migration/Executor.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static function execute_migrations() {
4040
* @return array of available migrations
4141
*/
4242
private static function get_all_migrations() {
43-
$migrations = [];
43+
$migrations = [];
4444
$packages_path = scandir( EE_VENDOR_DIR . '/easyengine' );
4545

4646
// get migrations from packages.
@@ -50,25 +50,25 @@ private static function get_all_migrations() {
5050
continue;
5151
}
5252

53-
$migration_path = EE_VENDOR_DIR . '/easyengine/' . $package . '/migrations';
53+
$migration_path = EE_VENDOR_DIR . '/easyengine/' . $package . '/migrations/db';
5454
if ( is_dir( $migration_path ) ) {
5555
$files = scandir( $migration_path );
5656
if ( \EE\Utils\inside_phar() ) {
5757
$migrations[] = $files;
58-
} else{
59-
$migrations[] = array_slice( $files,2);
58+
} else {
59+
$migrations[] = array_slice( $files, 2 );
6060
}
6161
}
6262
}
6363
}
6464

6565
// get migrations from core.
6666
if ( is_dir( EE_ROOT . '/migrations' ) ) {
67-
$files = scandir( EE_ROOT . '/migrations' );
67+
$files = scandir( EE_ROOT . '/migrations/db' );
6868
if ( \EE\Utils\inside_phar() ) {
6969
$migrations[] = $files;
70-
} else{
71-
$migrations[] = array_slice( $files,2);
70+
} else {
71+
$migrations[] = array_slice( $files, 2 );
7272
}
7373
}
7474

@@ -121,6 +121,7 @@ private static function execute_migration_stack( $migrations ) {
121121

122122
Migration::create( [
123123
'migration' => $migrations[0],
124+
'type' => 'db',
124125
'timestamp' => date( 'Y-m-d H:i:s' ),
125126
] );
126127

@@ -167,7 +168,7 @@ private static function get_migrations_to_execute( $migrations ) {
167168
* @return array
168169
*/
169170
private static function get_migrations_from_db() {
170-
return array_column( Migration::all(), 'migration' );
171+
return array_column( Migration::where( 'type', 'db' ), 'migration' );
171172
}
172173

173174
/**
@@ -184,9 +185,9 @@ private static function get_migration_path( $migration_name ) {
184185
return '';
185186
}
186187
if ( 'easyengine' === $matches[1] ) {
187-
return EE_ROOT . "/migrations/$migration_name";
188+
return EE_ROOT . "/migrations/db/$migration_name";
188189
} else {
189-
return EE_ROOT . "/vendor/easyengine/$matches[1]/migrations/$migration_name";
190+
return EE_ROOT . "/vendor/easyengine/$matches[1]/migrations/db/$migration_name";
190191
}
191192

192193
}

php/class-ee-db.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ private static function create_required_tables() {
6060
$query = 'CREATE TABLE migrations (
6161
id INTEGER,
6262
migration VARCHAR,
63+
type VARCHAR,
6364
timestamp DATETIME,
6465
PRIMARY KEY (id)
6566
);';

0 commit comments

Comments
 (0)