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