Migrator.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * The Minz_Migrator helps to migrate data (in a database or not) or the
  5. * architecture of a Minz application.
  6. *
  7. * @author Marien Fressinaud <dev@marienfressinaud.fr>
  8. * @license http://www.gnu.org/licenses/agpl-3.0.en.html AGPL
  9. */
  10. class Minz_Migrator
  11. {
  12. /** @var array<string> */
  13. private array $applied_versions;
  14. /** @var array<callable> */
  15. private array $migrations = [];
  16. /**
  17. * Execute a list of migrations, skipping versions indicated in a file
  18. *
  19. * @return true|string Returns true if execute succeeds to apply
  20. * migrations, or a string if it fails.
  21. * @throws DomainException if there is no migrations corresponding to the
  22. * given version (can happen if version file has
  23. * been modified, or migrations path cannot be
  24. * read).
  25. *
  26. * @throws BadFunctionCallException if a callback isn’t callable.
  27. */
  28. public static function execute(string $migrations_path, string $applied_migrations_path): string|bool {
  29. $applied_migrations = @file_get_contents($applied_migrations_path);
  30. if ($applied_migrations === false) {
  31. return "Cannot open the {$applied_migrations_path} file";
  32. }
  33. $applied_migrations = array_filter(explode("\n", $applied_migrations), fn(string $migration): bool => trim($migration) !== '');
  34. $migration_files = scandir($migrations_path) ?: [];
  35. $migration_files = array_filter($migration_files, static function (string $filename) {
  36. $file_extension = pathinfo($filename, PATHINFO_EXTENSION);
  37. return $file_extension === 'php';
  38. });
  39. $migration_versions = array_map(static function (string $filename) {
  40. return basename($filename, '.php');
  41. }, $migration_files);
  42. // We apply a "low-cost" comparison to avoid to include the migration
  43. // files at each run. It is equivalent to the upToDate method.
  44. if (count($applied_migrations) === count($migration_versions) &&
  45. empty(array_diff($applied_migrations, $migration_versions))) {
  46. // already at the latest version, so there is nothing more to do
  47. return true;
  48. }
  49. $lock_path = $applied_migrations_path . '.lock';
  50. if (!@mkdir($lock_path, 0770, true)) {
  51. // Someone is probably already executing the migrations (the folder
  52. // already exists).
  53. // We should probably return something else, but we don’t want the
  54. // user to think there is an error (it’s normal workflow), so let’s
  55. // stick to this solution for now.
  56. // Another option would be to show him a maintenance page.
  57. Minz_Log::warning(
  58. 'A request has been served while the application wasn’t up-to-date. '
  59. . 'Too many of these errors probably means a previous migration failed.'
  60. );
  61. return true;
  62. }
  63. $migrator = new self($migrations_path);
  64. $migrator->setAppliedVersions($applied_migrations);
  65. $results = $migrator->migrate();
  66. foreach ($results as $migration => $result) {
  67. if ($result === true) {
  68. $result = 'OK';
  69. } elseif ($result === false) {
  70. $result = 'KO';
  71. }
  72. Minz_Log::notice("Migration {$migration}: {$result}");
  73. }
  74. $applied_versions = implode("\n", $migrator->appliedVersions());
  75. $saved = file_put_contents($applied_migrations_path, $applied_versions);
  76. if (!@rmdir($lock_path)) {
  77. Minz_Log::error(
  78. 'We weren’t able to unlink the migration executing folder, '
  79. . 'you might want to delete yourself: ' . $lock_path
  80. );
  81. // we don’t return early because the migrations could have been
  82. // applied successfully. This file is not "critical" if not removed
  83. // and more errors will eventually appear in the logs.
  84. }
  85. if ($saved === false) {
  86. return "Cannot save the {$applied_migrations_path} file";
  87. }
  88. if (!$migrator->upToDate()) {
  89. // still not up to date? It means last migration failed.
  90. return trim('A migration failed to be applied, please see previous logs.' . "\n" . implode("\n", $results));
  91. }
  92. return true;
  93. }
  94. /**
  95. * Create a Minz_Migrator instance. If directory is given, it'll load the
  96. * migrations from it.
  97. *
  98. * All the files in the directory must declare a class named
  99. * <app_name>_Migration_<filename> with a static `migrate` method.
  100. *
  101. * - <app_name> is the application name declared in the APP_NAME constant
  102. * - <filename> is the migration file name, without the `.php` extension
  103. *
  104. * The files starting with a dot are ignored.
  105. *
  106. * @throws BadFunctionCallException if a callback isn’t callable (i.e. cannot call a migrate method).
  107. */
  108. public function __construct(?string $directory = null) {
  109. $this->applied_versions = [];
  110. if ($directory == null || !is_dir($directory)) {
  111. return;
  112. }
  113. foreach (scandir($directory) ?: [] as $filename) {
  114. $file_extension = pathinfo($filename, PATHINFO_EXTENSION);
  115. if ($file_extension !== 'php') {
  116. continue;
  117. }
  118. $filepath = $directory . '/' . $filename;
  119. $migration_version = basename($filename, '.php');
  120. $migration_class = APP_NAME . "_Migration_" . $migration_version;
  121. $migration_callback = $migration_class . '::migrate';
  122. $include_result = @include_once($filepath);
  123. if (!$include_result) {
  124. Minz_Log::error(
  125. "{$filepath} migration file cannot be loaded.",
  126. ADMIN_LOG
  127. );
  128. }
  129. if (!is_callable($migration_callback)) {
  130. throw new BadFunctionCallException("{$migration_version} migration cannot be called.");
  131. }
  132. $this->addMigration($migration_version, $migration_callback);
  133. }
  134. }
  135. /**
  136. * Register a migration into the migration system.
  137. *
  138. * @param string $version The version of the migration (be careful, migrations
  139. * are sorted with the `strnatcmp` function)
  140. * @param callable $callback The migration function to execute, it should
  141. * return true on success and must return false
  142. * on error
  143. */
  144. public function addMigration(string $version, callable $callback): void {
  145. $this->migrations[$version] = $callback;
  146. }
  147. /**
  148. * Return the list of migrations, sorted with `strnatcmp`
  149. *
  150. * @see https://www.php.net/manual/en/function.strnatcmp.php
  151. *
  152. * @return array<string,callable>
  153. */
  154. public function migrations(): array {
  155. $migrations = $this->migrations;
  156. uksort($migrations, 'strnatcmp');
  157. /** @var array<string,callable> $migrations */
  158. return $migrations;
  159. }
  160. /**
  161. * Set the applied versions of the application.
  162. *
  163. * @param array<string> $versions
  164. *
  165. * @throws DomainException if there is no migrations corresponding to a version
  166. */
  167. public function setAppliedVersions(array $versions): void {
  168. foreach ($versions as $version) {
  169. $version = trim($version);
  170. if (!isset($this->migrations[$version])) {
  171. throw new DomainException("{$version} migration does not exist.");
  172. }
  173. $this->applied_versions[] = $version;
  174. }
  175. }
  176. /**
  177. * @return string[]
  178. */
  179. public function appliedVersions(): array {
  180. $versions = $this->applied_versions;
  181. usort($versions, 'strnatcmp');
  182. return $versions;
  183. }
  184. /**
  185. * Return the list of available versions, sorted with `strnatcmp`
  186. *
  187. * @see https://www.php.net/manual/en/function.strnatcmp.php
  188. *
  189. * @return string[]
  190. */
  191. public function versions(): array {
  192. $migrations = $this->migrations();
  193. return array_keys($migrations);
  194. }
  195. /**
  196. * @return bool Return true if the application is up-to-date, false otherwise.
  197. * If no migrations are registered, it always returns true.
  198. */
  199. public function upToDate(): bool {
  200. // Counting versions is enough since we cannot apply a version which
  201. // doesn’t exist (see setAppliedVersions method).
  202. return count($this->versions()) === count($this->applied_versions);
  203. }
  204. /**
  205. * Migrate the system to the latest version.
  206. *
  207. * It only executes migrations AFTER the current version. If a migration
  208. * returns false or fails, it immediately stops the process.
  209. *
  210. * If the migration doesn’t return false nor raise an exception, it is
  211. * considered as successful. It is considered as good practice to return
  212. * true on success though.
  213. *
  214. * @return array<string,bool|string> Return the results of each executed migration. If an
  215. * exception was raised in a migration, its result is set to
  216. * the exception message.
  217. */
  218. public function migrate(): array {
  219. $result = [];
  220. foreach ($this->migrations() as $version => $callback) {
  221. if (in_array($version, $this->applied_versions, true)) {
  222. // the version is already applied so we skip this migration
  223. continue;
  224. }
  225. try {
  226. $migration_result = $callback();
  227. $result[$version] = (bool)$migration_result;
  228. } catch (Exception $e) {
  229. $migration_result = false;
  230. $result[$version] = $e->getMessage();
  231. }
  232. if ($migration_result === false) {
  233. break;
  234. }
  235. $this->applied_versions[] = $version;
  236. }
  237. return $result;
  238. }
  239. }