Migrator.php 8.5 KB

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