Skip to content

Commit 0609043

Browse files
committed
Add make:migration command
1 parent 4047272 commit 0609043

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace ProAI\SuperMigrations\Console\Migrations;
4+
5+
use Illuminate\Support\Composer;
6+
use Illuminate\Database\Migrations\MigrationCreator;
7+
8+
class MigrateMakeCommand extends BaseCommand
9+
{
10+
/**
11+
* The console command signature.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'make:migration:super {name : The name of the migration.}';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Create a new migration file';
23+
24+
/**
25+
* The migration creator instance.
26+
*
27+
* @var \Illuminate\Database\Migrations\MigrationCreator
28+
*/
29+
protected $creator;
30+
31+
/**
32+
* The Composer instance.
33+
*
34+
* @var \Illuminate\Support\Composer
35+
*/
36+
protected $composer;
37+
38+
/**
39+
* Create a new migration install command instance.
40+
*
41+
* @param \Illuminate\Database\Migrations\MigrationCreator $creator
42+
* @param \Illuminate\Support\Composer $composer
43+
* @return void
44+
*/
45+
public function __construct(MigrationCreator $creator, Composer $composer)
46+
{
47+
parent::__construct();
48+
49+
$this->creator = $creator;
50+
$this->composer = $composer;
51+
}
52+
53+
/**
54+
* Execute the console command.
55+
*
56+
* @return void
57+
*/
58+
public function fire()
59+
{
60+
// It's possible for the developer to specify the tables to modify in this
61+
// schema operation. The developer may also specify if this table needs
62+
// to be freshly created so we can create the appropriate migrations.
63+
$name = trim($this->input->getArgument('name'));
64+
65+
$table = $this->input->getOption('table');
66+
67+
// Now we are ready to write the migration out to disk. Once we've written
68+
// the migration out, we will dump-autoload for the entire framework to
69+
// make sure that the migrations are registered by the class loaders.
70+
$this->writeMigration($name, $table);
71+
72+
$this->composer->dumpAutoloads();
73+
}
74+
75+
/**
76+
* Write the migration file to disk.
77+
*
78+
* @param string $name
79+
* @param string $table
80+
* @return string
81+
*/
82+
protected function writeMigration($name, $table)
83+
{
84+
$path = $this->getMigrationPath();
85+
86+
$file = pathinfo($this->creator->create($name, $path, $table), PATHINFO_FILENAME);
87+
88+
$this->line("<info>Created Migration:</info> {$file}");
89+
}
90+
91+
/**
92+
* Get migration path (either specified by '--path' option or default location).
93+
*
94+
* @return string
95+
*/
96+
protected function getMigrationPath()
97+
{
98+
if (! is_null($targetPath = $this->input->getOption('path'))) {
99+
return $this->laravel->basePath().'/'.$targetPath;
100+
}
101+
102+
return parent::getMigrationPath();
103+
}
104+
}

src/Console/stubs/.gitkeep

Whitespace-only changes.

src/Console/stubs/migration.stub

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use ProAI\SuperMigrations\Migration;
4+
5+
class DummyClass extends Migration
6+
{
7+
/**
8+
* Get table names and related methods for up and down schemas.
9+
*
10+
* @return void
11+
*/
12+
public function schemas()
13+
{
14+
return [
15+
//
16+
];
17+
}
18+
}

src/Console/stubs/table.stub

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use ProAI\SuperMigrations\Table;
5+
6+
class DummyClass extends Table
7+
{
8+
/**
9+
* Create the table.
10+
*
11+
* @return void
12+
*/
13+
public function create()
14+
{
15+
// up
16+
$this->upSchema()->create(function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->timestamps();
19+
});
20+
21+
// down
22+
$this->downSchema()->dropIfExists();
23+
}
24+
}

0 commit comments

Comments
 (0)