Skip to content

Commit 2ab862c

Browse files
committed
Add up() and down() method to table
1 parent c1f7f52 commit 2ab862c

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Furthermore for each of the migration specific names that we declared in the mig
7676

7777
use Illuminate\Database\Schema\Blueprint;
7878
use ProAI\SuperMigrations\Table;
79+
use Illuminate\Support\Facades\DB;
7980

8081
class UserTable extends Table
8182
{
@@ -86,20 +87,29 @@ class UserTable extends Table
8687
*/
8788
public function create()
8889
{
89-
// migrations up
90+
// These statements will be only executed if direction of migrations is up
9091
$this->upSchema()->create(function (Blueprint $table) {
9192
$table->increments('id');
9293
$table->timestamps();
9394
});
9495

95-
// migrations down
96+
$this->up(function($tableName) {
97+
DB::raw('ALTER TABLE `'.$tableName.'` ADD `client_id` BINARY(16)');
98+
});
99+
100+
101+
// These statements will be only executed if direction of migrations is down
96102
$this->downSchema()->dropIfExists();
103+
104+
$this->down(function($tableName) {
105+
// some code
106+
});
97107
}
98108
}
99109

100110
```
101111

102-
We use `$this->upSchema()` and `$this->downSchema()` to define the up and down schema. These methods return a `ProAI\SuperMigrations\Builder` instance that is similar to the Laravel database schema builder (see [Laravel docs](https://laravel.com/docs/5.3/migrations)). The only difference is that you don't need the tablename as first argument, because the tablename is already known.
112+
We use `$this->upSchema()` and `$this->downSchema()` to define the up and down schema. These methods return a `ProAI\SuperMigrations\Builder` instance that is similar to the Laravel database schema builder (see [Laravel docs](https://laravel.com/docs/5.3/migrations)). The only difference is that you don't need the tablename as first argument, because the tablename is already known. Furthermore we use `$this->up(...)` and `$this->down(...)` to insert custom code like raw DB statements.
103113

104114
### Generator Command
105115

src/Table.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66

77
abstract class Table
88
{
9+
/**
10+
* The table associated with the schema.
11+
*
12+
* @var string
13+
*/
14+
private $table;
15+
916
/**
1017
* Direction of the migration (up or down).
1118
*
@@ -28,13 +35,40 @@ abstract class Table
2835
*/
2936
public function __construct($table, $direction)
3037
{
38+
$this->table = $table;
3139
$this->direction = $direction;
3240

3341
$this->schema = new Builder($table);
3442
}
3543

3644
/**
37-
* Run the migrations.
45+
* Execute a command if migration direction is up.
46+
*
47+
* @param $command \Closure
48+
* @return void
49+
*/
50+
protected function up($command)
51+
{
52+
if ($this->direction === 'up') {
53+
$command($this->table);
54+
}
55+
}
56+
57+
/**
58+
* Execute a command if migration direction is down.
59+
*
60+
* @param $command \Closure
61+
* @return void
62+
*/
63+
protected function down($command)
64+
{
65+
if ($this->direction === 'down') {
66+
$command($this->table);
67+
}
68+
}
69+
70+
/**
71+
* Get a schema builder instance if migration direction is up.
3872
*
3973
* @return \ProAI\SuperMigrations\Builder
4074
*/
@@ -50,9 +84,9 @@ protected function upSchema()
5084
}
5185

5286
/**
53-
* Reverse the migrations.
87+
* Get a schema builder instance if migration direction is up.
5488
*
55-
* @return void
89+
* @return \ProAI\SuperMigrations\Builder
5690
*/
5791
protected function downSchema()
5892
{

0 commit comments

Comments
 (0)