database.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php namespace Laravel\CLI\Tasks\Migrate;
  2. use Laravel\Request;
  3. use Laravel\Database as DB;
  4. class Database {
  5. /**
  6. * Log a migration in the migration table.
  7. *
  8. * @param string $bundle
  9. * @param string $name
  10. * @param int $batch
  11. * @return void
  12. */
  13. public function log($bundle, $name, $batch)
  14. {
  15. $this->table()->insert(compact('bundle', 'name', 'batch'));
  16. }
  17. /**
  18. * Delete a row from the migration table.
  19. *
  20. * @param string $bundle
  21. * @param string $name
  22. * @return void
  23. */
  24. public function delete($bundle, $name)
  25. {
  26. $this->table()->where_bundle_and_name($bundle, $name)->delete();
  27. }
  28. /**
  29. * Return an array of the last batch of migrations.
  30. *
  31. * @return array
  32. */
  33. public function last()
  34. {
  35. $table = $this->table();
  36. // First we need to grab the last batch ID from the migration table,
  37. // as this will allow us to grab the lastest batch of migrations
  38. // that need to be run for a rollback command.
  39. $id = $this->batch();
  40. // Once we have the batch ID, we will pull all of the rows for that
  41. // batch. Then we can feed the results into the resolve method to
  42. // get the migration instances for the command.
  43. return $table->where_batch($id)->order_by('name', 'desc')->get();
  44. }
  45. /**
  46. * Get all of the migrations that have run for a bundle.
  47. *
  48. * @param string $bundle
  49. * @return array
  50. */
  51. public function ran($bundle)
  52. {
  53. return $this->table()->where_bundle($bundle)->lists('name');
  54. }
  55. /**
  56. * Get the maximum batch ID from the migration table.
  57. *
  58. * @return int
  59. */
  60. public function batch()
  61. {
  62. return $this->table()->max('batch');
  63. }
  64. /**
  65. * Get a database query instance for the migration table.
  66. *
  67. * @return Query
  68. */
  69. protected function table()
  70. {
  71. return DB::connection(Request::server('cli.db'))->table('laravel_migrations');
  72. }
  73. }