DatabaseMigrationRepository.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 ran 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. * Log that a migration was run.
  73. *
  74. * @param string $file
  75. * @param int $batch
  76. * @return void
  77. */
  78. public function log($file, $batch)
  79. {
  80. $record = ['migration' => $file, 'batch' => $batch];
  81. $this->table()->insert($record);
  82. }
  83. /**
  84. * Remove a migration from the log.
  85. *
  86. * @param object $migration
  87. * @return void
  88. */
  89. public function delete($migration)
  90. {
  91. $this->table()->where('migration', $migration->migration)->delete();
  92. }
  93. /**
  94. * Get the next migration batch number.
  95. *
  96. * @return int
  97. */
  98. public function getNextBatchNumber()
  99. {
  100. return $this->getLastBatchNumber() + 1;
  101. }
  102. /**
  103. * Get the last migration batch number.
  104. *
  105. * @return int
  106. */
  107. public function getLastBatchNumber()
  108. {
  109. return $this->table()->max('batch');
  110. }
  111. /**
  112. * Create the migration repository data store.
  113. *
  114. * @return void
  115. */
  116. public function createRepository()
  117. {
  118. $schema = $this->getConnection()->getSchemaBuilder();
  119. $schema->create($this->table, function ($table) {
  120. // The migrations table is responsible for keeping track of which of the
  121. // migrations have actually run for the application. We'll create the
  122. // table to hold the migration file's path as well as the batch ID.
  123. $table->increments('id');
  124. $table->string('migration');
  125. $table->integer('batch');
  126. });
  127. }
  128. /**
  129. * Determine if the migration repository exists.
  130. *
  131. * @return bool
  132. */
  133. public function repositoryExists()
  134. {
  135. $schema = $this->getConnection()->getSchemaBuilder();
  136. return $schema->hasTable($this->table);
  137. }
  138. /**
  139. * Get a query builder for the migration table.
  140. *
  141. * @return \Illuminate\Database\Query\Builder
  142. */
  143. protected function table()
  144. {
  145. return $this->getConnection()->table($this->table)->useWritePdo();
  146. }
  147. /**
  148. * Get the connection resolver instance.
  149. *
  150. * @return \Illuminate\Database\ConnectionResolverInterface
  151. */
  152. public function getConnectionResolver()
  153. {
  154. return $this->resolver;
  155. }
  156. /**
  157. * Resolve the database connection instance.
  158. *
  159. * @return \Illuminate\Database\Connection
  160. */
  161. public function getConnection()
  162. {
  163. return $this->resolver->connection($this->connection);
  164. }
  165. /**
  166. * Set the information source to gather data.
  167. *
  168. * @param string $name
  169. * @return void
  170. */
  171. public function setSource($name)
  172. {
  173. $this->connection = $name;
  174. }
  175. }