| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\MigrationLog; |
| 4 |
|
| 5 |
use Give\Framework\Database\DB; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class MigrationLogRepository |
| 9 |
* @package Give\MigrationLog |
| 10 |
* |
| 11 |
* @since 2.10.0 |
| 12 |
*/ |
| 13 |
class MigrationLogRepository { |
| 14 |
/** |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
private $migration_table; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var MigrationLogFactory |
| 21 |
*/ |
| 22 |
private $migrationFactory; |
| 23 |
|
| 24 |
/** |
| 25 |
* MigrationRepository constructor. |
| 26 |
* |
| 27 |
* @param MigrationLogFactory $migrationFactory |
| 28 |
*/ |
| 29 |
public function __construct( MigrationLogFactory $migrationFactory ) { |
| 30 |
global $wpdb; |
| 31 |
$this->migration_table = $wpdb->give_migrations; |
| 32 |
$this->migrationFactory = $migrationFactory; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Save Migration |
| 37 |
* |
| 38 |
* @param MigrationLogModel $model |
| 39 |
*/ |
| 40 |
public function save( MigrationLogModel $model ) { |
| 41 |
$query = " |
| 42 |
INSERT INTO {$this->migration_table} (id, status, error, last_run ) |
| 43 |
VALUES (%s, %s, %s, NOW()) |
| 44 |
ON DUPLICATE KEY UPDATE |
| 45 |
status = %s, |
| 46 |
error = %s, |
| 47 |
last_run = NOW() |
| 48 |
"; |
| 49 |
|
| 50 |
DB::query( |
| 51 |
DB::prepare( |
| 52 |
$query, |
| 53 |
$model->getId(), |
| 54 |
$model->getStatus(), |
| 55 |
$model->getError(), |
| 56 |
$model->getStatus(), |
| 57 |
$model->getError() |
| 58 |
) |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get all migrations |
| 64 |
* |
| 65 |
* @return MigrationLogModel[] |
| 66 |
*/ |
| 67 |
public function getMigrations() { |
| 68 |
$migrations = []; |
| 69 |
|
| 70 |
$result = DB::get_results( "SELECT * FROM {$this->migration_table}" ); |
| 71 |
|
| 72 |
if ( $result ) { |
| 73 |
foreach ( $result as $migration ) { |
| 74 |
|
| 75 |
$migrations[] = $this->migrationFactory->make( |
| 76 |
$migration->id, |
| 77 |
$migration->status, |
| 78 |
$migration->error, |
| 79 |
$migration->last_run |
| 80 |
); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
return $migrations; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get migration by ID |
| 89 |
* |
| 90 |
* @param string $id |
| 91 |
* |
| 92 |
* @return MigrationLogModel|null |
| 93 |
*/ |
| 94 |
public function getMigration( $id ) { |
| 95 |
$migration = DB::get_row( |
| 96 |
DB::prepare( "SELECT * FROM {$this->migration_table} WHERE id = %s", $id ) |
| 97 |
); |
| 98 |
|
| 99 |
if ( $migration ) { |
| 100 |
return $this->migrationFactory->make( |
| 101 |
$migration->id, |
| 102 |
$migration->status, |
| 103 |
$migration->error, |
| 104 |
$migration->last_run |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
return null; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get migrations by status |
| 113 |
* |
| 114 |
* @param string $status |
| 115 |
* |
| 116 |
* @return MigrationLogModel[] |
| 117 |
*/ |
| 118 |
public function getMigrationsByStatus( $status ) { |
| 119 |
$migrations = []; |
| 120 |
|
| 121 |
$result = DB::get_results( |
| 122 |
DB::prepare( "SELECT * FROM {$this->migration_table} WHERE status = %s", $status ) |
| 123 |
); |
| 124 |
|
| 125 |
if ( $result ) { |
| 126 |
foreach ( $result as $migration ) { |
| 127 |
$migrations[] = $this->migrationFactory->make( |
| 128 |
$migration->id, |
| 129 |
$migration->status, |
| 130 |
$migration->error, |
| 131 |
$migration->last_run |
| 132 |
); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
return $migrations; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Get completed migrations IDs |
| 141 |
* |
| 142 |
* @return array |
| 143 |
*/ |
| 144 |
public function getCompletedMigrationsIDs() { |
| 145 |
$migrations = []; |
| 146 |
|
| 147 |
try { |
| 148 |
$result = DB::get_results( |
| 149 |
DB::prepare( "SELECT * FROM {$this->migration_table} WHERE status = %s", MigrationLogStatus::SUCCESS ) |
| 150 |
); |
| 151 |
} catch ( \Exception $exception ) { |
| 152 |
// This exception should happen only once, during the migration system storage update. |
| 153 |
// But, we will log this error just in case to see if this is a repeating problem. |
| 154 |
error_log( $exception->getMessage() ); |
| 155 |
|
| 156 |
// Fallback to legacy migration storage system |
| 157 |
return get_option( 'give_database_migrations', [] ); |
| 158 |
} |
| 159 |
|
| 160 |
if ( $result ) { |
| 161 |
foreach ( $result as $migration ) { |
| 162 |
$migrations[] = $migration->id; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
return $migrations; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Get migration count |
| 171 |
* |
| 172 |
* @return int|null |
| 173 |
*/ |
| 174 |
public function getMigrationsCount() { |
| 175 |
try { |
| 176 |
return DB::get_var( "SELECT count(id) FROM {$this->migration_table}" ); |
| 177 |
} catch ( \Exception $exception ) { |
| 178 |
return 0; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Get failed migrations count by list of migrations ids |
| 184 |
* |
| 185 |
* @param array $migrationIds |
| 186 |
* |
| 187 |
* @return int |
| 188 |
*/ |
| 189 |
public function getFailedMigrationsCountByIds( $migrationIds ) { |
| 190 |
try { |
| 191 |
$query = sprintf( |
| 192 |
"SELECT count(id) FROM %s WHERE id IN ('%s') AND status != '%s'", |
| 193 |
$this->migration_table, |
| 194 |
implode( "','", array_map( 'esc_sql', $migrationIds ) ), |
| 195 |
MigrationLogStatus::SUCCESS |
| 196 |
); |
| 197 |
|
| 198 |
return DB::get_var( $query ); |
| 199 |
} catch ( \Exception $exception ) { |
| 200 |
// Fallback |
| 201 |
return 0; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
} |
| 206 |
|