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