independent-analytics
/
vendor
/
illuminate
/
database
/
Migrations
/
DatabaseMigrationRepository.php
stubs
3 weeks ago
DatabaseMigrationRepository.php
3 weeks ago
Migration.php
2 years ago
MigrationCreator.php
3 weeks ago
MigrationRepositoryInterface.php
2 years ago
Migrator.php
3 weeks ago
DatabaseMigrationRepository.php
193 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED\Illuminate\Database\Migrations; |
| 4 | |
| 5 | use IAWPSCOPED\Illuminate\Database\ConnectionResolverInterface as Resolver; |
| 6 | /** @internal */ |
| 7 | class DatabaseMigrationRepository implements MigrationRepositoryInterface |
| 8 | { |
| 9 | /** |
| 10 | * The database connection resolver instance. |
| 11 | * |
| 12 | * @var \Illuminate\Database\ConnectionResolverInterface |
| 13 | */ |
| 14 | protected $resolver; |
| 15 | /** |
| 16 | * The name of the migration table. |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | protected $table; |
| 21 | /** |
| 22 | * The name of the database connection to use. |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | protected $connection; |
| 27 | /** |
| 28 | * Create a new database migration repository instance. |
| 29 | * |
| 30 | * @param \Illuminate\Database\ConnectionResolverInterface $resolver |
| 31 | * @param string $table |
| 32 | * @return void |
| 33 | */ |
| 34 | public function __construct(Resolver $resolver, $table) |
| 35 | { |
| 36 | $this->table = $table; |
| 37 | $this->resolver = $resolver; |
| 38 | } |
| 39 | /** |
| 40 | * Get the completed migrations. |
| 41 | * |
| 42 | * @return array |
| 43 | */ |
| 44 | public function getRan() |
| 45 | { |
| 46 | return $this->table()->orderBy('batch', 'asc')->orderBy('migration', 'asc')->pluck('migration')->all(); |
| 47 | } |
| 48 | /** |
| 49 | * Get the list of migrations. |
| 50 | * |
| 51 | * @param int $steps |
| 52 | * @return array |
| 53 | */ |
| 54 | public function getMigrations($steps) |
| 55 | { |
| 56 | $query = $this->table()->where('batch', '>=', '1'); |
| 57 | return $query->orderBy('batch', 'desc')->orderBy('migration', 'desc')->take($steps)->get()->all(); |
| 58 | } |
| 59 | /** |
| 60 | * Get the last migration batch. |
| 61 | * |
| 62 | * @return array |
| 63 | */ |
| 64 | public function getLast() |
| 65 | { |
| 66 | $query = $this->table()->where('batch', $this->getLastBatchNumber()); |
| 67 | return $query->orderBy('migration', 'desc')->get()->all(); |
| 68 | } |
| 69 | /** |
| 70 | * Get the completed migrations with their batch numbers. |
| 71 | * |
| 72 | * @return array |
| 73 | */ |
| 74 | public function getMigrationBatches() |
| 75 | { |
| 76 | return $this->table()->orderBy('batch', 'asc')->orderBy('migration', 'asc')->pluck('batch', 'migration')->all(); |
| 77 | } |
| 78 | /** |
| 79 | * Log that a migration was run. |
| 80 | * |
| 81 | * @param string $file |
| 82 | * @param int $batch |
| 83 | * @return void |
| 84 | */ |
| 85 | public function log($file, $batch) |
| 86 | { |
| 87 | $record = ['migration' => $file, 'batch' => $batch]; |
| 88 | $this->table()->insert($record); |
| 89 | } |
| 90 | /** |
| 91 | * Remove a migration from the log. |
| 92 | * |
| 93 | * @param object $migration |
| 94 | * @return void |
| 95 | */ |
| 96 | public function delete($migration) |
| 97 | { |
| 98 | $this->table()->where('migration', $migration->migration)->delete(); |
| 99 | } |
| 100 | /** |
| 101 | * Get the next migration batch number. |
| 102 | * |
| 103 | * @return int |
| 104 | */ |
| 105 | public function getNextBatchNumber() |
| 106 | { |
| 107 | return $this->getLastBatchNumber() + 1; |
| 108 | } |
| 109 | /** |
| 110 | * Get the last migration batch number. |
| 111 | * |
| 112 | * @return int |
| 113 | */ |
| 114 | public function getLastBatchNumber() |
| 115 | { |
| 116 | return $this->table()->max('batch'); |
| 117 | } |
| 118 | /** |
| 119 | * Create the migration repository data store. |
| 120 | * |
| 121 | * @return void |
| 122 | */ |
| 123 | public function createRepository() |
| 124 | { |
| 125 | $schema = $this->getConnection()->getSchemaBuilder(); |
| 126 | $schema->create($this->table, function ($table) { |
| 127 | // The migrations table is responsible for keeping track of which of the |
| 128 | // migrations have actually run for the application. We'll create the |
| 129 | // table to hold the migration file's path as well as the batch ID. |
| 130 | $table->increments('id'); |
| 131 | $table->string('migration'); |
| 132 | $table->integer('batch'); |
| 133 | }); |
| 134 | } |
| 135 | /** |
| 136 | * Determine if the migration repository exists. |
| 137 | * |
| 138 | * @return bool |
| 139 | */ |
| 140 | public function repositoryExists() |
| 141 | { |
| 142 | $schema = $this->getConnection()->getSchemaBuilder(); |
| 143 | return $schema->hasTable($this->table); |
| 144 | } |
| 145 | /** |
| 146 | * Delete the migration repository data store. |
| 147 | * |
| 148 | * @return void |
| 149 | */ |
| 150 | public function deleteRepository() |
| 151 | { |
| 152 | $schema = $this->getConnection()->getSchemaBuilder(); |
| 153 | $schema->drop($this->table); |
| 154 | } |
| 155 | /** |
| 156 | * Get a query builder for the migration table. |
| 157 | * |
| 158 | * @return \Illuminate\Database\Query\Builder |
| 159 | */ |
| 160 | protected function table() |
| 161 | { |
| 162 | return $this->getConnection()->table($this->table)->useWritePdo(); |
| 163 | } |
| 164 | /** |
| 165 | * Get the connection resolver instance. |
| 166 | * |
| 167 | * @return \Illuminate\Database\ConnectionResolverInterface |
| 168 | */ |
| 169 | public function getConnectionResolver() |
| 170 | { |
| 171 | return $this->resolver; |
| 172 | } |
| 173 | /** |
| 174 | * Resolve the database connection instance. |
| 175 | * |
| 176 | * @return \Illuminate\Database\Connection |
| 177 | */ |
| 178 | public function getConnection() |
| 179 | { |
| 180 | return $this->resolver->connection($this->connection); |
| 181 | } |
| 182 | /** |
| 183 | * Set the information source to gather data. |
| 184 | * |
| 185 | * @param string $name |
| 186 | * @return void |
| 187 | */ |
| 188 | public function setSource($name) |
| 189 | { |
| 190 | $this->connection = $name; |
| 191 | } |
| 192 | } |
| 193 |