DatabaseMigrationRepository.php 4.8KB

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