database.php 1.79 KB
Newer Older
1 2
<?php namespace Laravel\CLI\Tasks\Migrate;

3
use Laravel\Request;
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
use Laravel\Database as DB;

class Database {

	/**
	 * Log a migration in the migration table.
	 *
	 * @param  string  $bundle
	 * @param  string  $name
	 * @param  int     $batch
	 * @return void
	 */
	public function log($bundle, $name, $batch)
	{
		$this->table()->insert(compact('bundle', 'name', 'batch'));
	}

	/**
	 * Delete a row from the migration table.
	 *
	 * @param  string  $bundle
	 * @param  string  $name
	 * @return void
	 */
	public function delete($bundle, $name)
	{
		$this->table()->where_bundle_and_name($bundle, $name)->delete();
	}

	/**
	 * Return an array of the last batch of migrations.
	 *
	 * @return array
	 */
	public function last()
	{
		$table = $this->table();

		// First we need to grab the last batch ID from the migration table,
43
		// as this will allow us to grab the latest batch of migrations
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
		// that need to be run for a rollback command.
		$id = $this->batch();

		// Once we have the batch ID, we will pull all of the rows for that
		// batch. Then we can feed the results into the resolve method to
		// get the migration instances for the command.
		return $table->where_batch($id)->order_by('name', 'desc')->get();
	}

	/**
	 * Get all of the migrations that have run for a bundle.
	 *
	 * @param  string  $bundle
	 * @return array
	 */
	public function ran($bundle)
	{
		return $this->table()->where_bundle($bundle)->lists('name');
	}

	/**
	 * Get the maximum batch ID from the migration table.
	 *
	 * @return int
	 */
	public function batch()
	{
		return $this->table()->max('batch');
	}

	/**
	 * Get a database query instance for the migration table.
	 *
Phill Sparks committed
77
	 * @return Laravel\Database\Query
78 79 80
	 */
	protected function table()
	{
81
		return DB::connection(Request::server('cli.db'))->table('laravel_migrations');
82 83 84
	}

}