|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace LaracraftTech\LaravelUsefulTraits; |
| 4 | + |
| 5 | +use Illuminate\Contracts\Console\Kernel; |
| 6 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 7 | +use Illuminate\Foundation\Testing\RefreshDatabaseState; |
| 8 | +use Symfony\Component\Finder\Finder; |
| 9 | + |
| 10 | +/** |
| 11 | + * Only migrate fresh if necessary -> much faster after first time! |
| 12 | + * Credits to mayahi, with some small changes of mine. |
| 13 | + * @link https://mayahi.net/laravel/make-refresh-database-trait-much-faster/ |
| 14 | + */ |
| 15 | +trait RefreshDatabaseFast |
| 16 | +{ |
| 17 | + use RefreshDatabase; |
| 18 | + |
| 19 | + /** |
| 20 | + * Only migrates fresh if necessary |
| 21 | + * |
| 22 | + * @return void |
| 23 | + */ |
| 24 | + protected function refreshTestDatabase(): void |
| 25 | + { |
| 26 | + if (! RefreshDatabaseState::$migrated) { |
| 27 | + $this->runMigrationsIfNecessary(); |
| 28 | + |
| 29 | + $this->app[Kernel::class]->setArtisan(null); |
| 30 | + |
| 31 | + RefreshDatabaseState::$migrated = true; |
| 32 | + } |
| 33 | + |
| 34 | + $this->beginDatabaseTransaction(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Check if migration fresh is necessary by checking |
| 39 | + * if the migrations files checksum has changed. |
| 40 | + * |
| 41 | + * @return void |
| 42 | + */ |
| 43 | + protected function runMigrationsIfNecessary(): void |
| 44 | + { |
| 45 | + if (!$this->identicalChecksum()) { |
| 46 | + if (config('useful-traits.refresh_db_fast.seed')) { |
| 47 | + $this->seed(); |
| 48 | + } |
| 49 | + |
| 50 | + $this->artisan('migrate:fresh', $this->migrateFreshUsing()); |
| 51 | + |
| 52 | + //create checksum after migration finishes |
| 53 | + $this->createChecksum(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Calaculates the checksum of the migration files. |
| 59 | + * |
| 60 | + * @return string |
| 61 | + */ |
| 62 | + private function calculateChecksum(): string |
| 63 | + { |
| 64 | + $files = Finder::create() |
| 65 | + ->files() |
| 66 | + ->exclude([ |
| 67 | + 'factories', |
| 68 | + 'seeders', |
| 69 | + ]) |
| 70 | + ->in(database_path()) |
| 71 | + ->ignoreDotFiles(true) |
| 72 | + ->ignoreVCS(true) |
| 73 | + ->getIterator(); |
| 74 | + |
| 75 | + $files = array_keys(iterator_to_array($files)); |
| 76 | + |
| 77 | + $checksum = collect($files)->map(function($file) {return md5_file($file);})->implode(''); |
| 78 | + |
| 79 | + return md5($checksum); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Filepath to store the checksum. |
| 84 | + * |
| 85 | + * @return string |
| 86 | + */ |
| 87 | + private function checksumFilePath(): string |
| 88 | + { |
| 89 | + return base_path('.phpunit.database.checksum'); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Creates the checksum file. |
| 94 | + * |
| 95 | + * @return void |
| 96 | + */ |
| 97 | + private function createChecksum(): void |
| 98 | + { |
| 99 | + file_put_contents($this->checksumFilePath(), $this->calculateChecksum()); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Get the checksum file content. |
| 104 | + * |
| 105 | + * @return false|string |
| 106 | + */ |
| 107 | + private function checksumFileContents() |
| 108 | + { |
| 109 | + return file_get_contents($this->checksumFilePath()); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Check if checksum exists. |
| 114 | + * |
| 115 | + * @return bool |
| 116 | + */ |
| 117 | + private function isChecksumExists(): bool |
| 118 | + { |
| 119 | + return file_exists($this->checksumFilePath()); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Check if checksum of current database migration files |
| 124 | + * are identical to the one we stored already. |
| 125 | + * |
| 126 | + * @return bool |
| 127 | + */ |
| 128 | + private function identicalChecksum(): bool |
| 129 | + { |
| 130 | + if (!$this->isChecksumExists()) { |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + if ($this->checksumFileContents() === $this->calculateChecksum()) { |
| 135 | + return true; |
| 136 | + } |
| 137 | + |
| 138 | + return false; |
| 139 | + } |
| 140 | +} |
0 commit comments