DatabaseMigrationRepository.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 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')
  58. ->orderBy('migration', 'desc')
  59. ->take($steps)->get()->all();
  60. }
  61. /**
  62. * Get the list of the migrations by batch number.
  63. *
  64. * @param int $batch
  65. * @return array
  66. */
  67. public function getMigrationsByBatch($batch)
  68. {
  69. return $this->table()
  70. ->where('batch', $batch)
  71. ->orderBy('migration', 'desc')
  72. ->get()
  73. ->all();
  74. }
  75. /**
  76. * Get the last migration batch.
  77. *
  78. * @return array
  79. */
  80. public function getLast()
  81. {
  82. $query = $this->table()->where('batch', $this->getLastBatchNumber());
  83. return $query->orderBy('migration', 'desc')->get()->all();
  84. }
  85. /**
  86. * Get the completed migrations with their batch numbers.
  87. *
  88. * @return array
  89. */
  90. public function getMigrationBatches()
  91. {
  92. return $this->table()
  93. ->orderBy('batch', 'asc')
  94. ->orderBy('migration', 'asc')
  95. ->pluck('batch', 'migration')->all();
  96. }
  97. /**
  98. * Log that a migration was run.
  99. *
  100. * @param string $file
  101. * @param int $batch
  102. * @return void
  103. */
  104. public function log($file, $batch)
  105. {
  106. $record = ['migration' => $file, 'batch' => $batch];
  107. $this->table()->insert($record);
  108. }
  109. /**
  110. * Remove a migration from the log.
  111. *
  112. * @param object $migration
  113. * @return void
  114. */
  115. public function delete($migration)
  116. {
  117. $this->table()->where('migration', $migration->migration)->delete();
  118. }
  119. /**
  120. * Get the next migration batch number.
  121. *
  122. * @return int
  123. */
  124. public function getNextBatchNumber()
  125. {
  126. return $this->getLastBatchNumber() + 1;
  127. }
  128. /**
  129. * Get the last migration batch number.
  130. *
  131. * @return int
  132. */
  133. public function getLastBatchNumber()
  134. {
  135. return $this->table()->max('batch');
  136. }
  137. /**
  138. * Create the migration repository data store.
  139. *
  140. * @return void
  141. */
  142. public function createRepository()
  143. {
  144. $schema = $this->getConnection()->getSchemaBuilder();
  145. $schema->create($this->table, function ($table) {
  146. // The migrations table is responsible for keeping track of which of the
  147. // migrations have actually run for the application. We'll create the
  148. // table to hold the migration file's path as well as the batch ID.
  149. $table->increments('id');
  150. $table->string('migration');
  151. $table->integer('batch');
  152. });
  153. }
  154. /**
  155. * Determine if the migration repository exists.
  156. *
  157. * @return bool
  158. */
  159. public function repositoryExists()
  160. {
  161. $schema = $this->getConnection()->getSchemaBuilder();
  162. return $schema->hasTable($this->table);
  163. }
  164. /**
  165. * Delete the migration repository data store.
  166. *
  167. * @return void
  168. */
  169. public function deleteRepository()
  170. {
  171. $schema = $this->getConnection()->getSchemaBuilder();
  172. $schema->drop($this->table);
  173. }
  174. /**
  175. * Get a query builder for the migration table.
  176. *
  177. * @return \Illuminate\Database\Query\Builder
  178. */
  179. protected function table()
  180. {
  181. return $this->getConnection()->table($this->table)->useWritePdo();
  182. }
  183. /**
  184. * Get the connection resolver instance.
  185. *
  186. * @return \Illuminate\Database\ConnectionResolverInterface
  187. */
  188. public function getConnectionResolver()
  189. {
  190. return $this->resolver;
  191. }
  192. /**
  193. * Resolve the database connection instance.
  194. *
  195. * @return \Illuminate\Database\Connection
  196. */
  197. public function getConnection()
  198. {
  199. return $this->resolver->connection($this->connection);
  200. }
  201. /**
  202. * Set the information source to gather data.
  203. *
  204. * @param string $name
  205. * @return void
  206. */
  207. public function setSource($name)
  208. {
  209. $this->connection = $name;
  210. }
  211. }