Skip to content

Commit 8c2afc7

Browse files
committed
Fix #141 Generate anonymous migration
1 parent 3c69bc9 commit 8c2afc7

File tree

6 files changed

+70
-14
lines changed

6 files changed

+70
-14
lines changed

config/migrations-generator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
return [
44
// Where the templates for the generators are stored.
5-
'migration_template_path' => __DIR__ . '/../stubs/migration.generate.stub',
5+
'migration_template_path' => __DIR__ . '/../stubs/migration.generate.stub',
6+
'migration_anonymous_template_path' => __DIR__ . '/../stubs/migration.generate.anonymous.stub',
67

78
// Where the generated files will be saved.
8-
'migration_target_path' => base_path('database/migrations'),
9+
'migration_target_path' => base_path('database/migrations'),
910

1011
// Migration filename pattern.
11-
'filename_pattern' => [
12+
'filename_pattern' => [
1213
'table' => '[datetime]_create_[name]_table.php',
1314
'view' => '[datetime]_create_[name]_view.php',
1415
'procedure' => '[datetime]_create_[name]_proc.php',

src/MigrateGenerateCommand.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222
use KitLoong\MigrationsGenerator\Schema\Schema;
2323
use KitLoong\MigrationsGenerator\Schema\SQLiteSchema;
2424
use KitLoong\MigrationsGenerator\Schema\SQLSrvSchema;
25+
use KitLoong\MigrationsGenerator\Support\CheckMigrationMethod;
2526

2627
class MigrateGenerateCommand extends Command
2728
{
29+
use CheckMigrationMethod;
30+
2831
/**
2932
* The name and signature of the console command.
3033
*
@@ -148,9 +151,7 @@ protected function setup(string $connection): void
148151
$this->option('path') ?? Config::get('migrations-generator.migration_target_path')
149152
);
150153

151-
$setting->setStubPath(
152-
$this->option('template-path') ?? Config::get('migrations-generator.migration_template_path')
153-
);
154+
$this->setStubPath($setting);
154155

155156
$setting->setDate(
156157
$this->option('date') ? Carbon::parse($this->option('date')) : Carbon::now()
@@ -173,6 +174,25 @@ protected function setup(string $connection): void
173174
);
174175
}
175176

177+
/**
178+
* Set migration stub.
179+
*
180+
* @param \KitLoong\MigrationsGenerator\Setting $setting
181+
* @return void
182+
*/
183+
protected function setStubPath(Setting $setting): void
184+
{
185+
$defaultStub = Config::get('migrations-generator.migration_anonymous_template_path');
186+
187+
if (!$this->hasAnonymousMigration()) {
188+
$defaultStub = Config::get('migrations-generator.migration_template_path');
189+
}
190+
191+
$setting->setStubPath(
192+
$this->option('template-path') ?? $defaultStub
193+
);
194+
}
195+
176196
/**
177197
* Get all tables from schema or return table list provided in option.
178198
* Then filter and exclude tables in `--ignore` option if any.

src/Support/CheckMigrationMethod.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace KitLoong\MigrationsGenerator\Support;
44

5+
use Illuminate\Database\Migrations\Migrator;
56
use Illuminate\Database\Schema\Blueprint;
67
use Illuminate\Database\Schema\Grammars\Grammar;
78

@@ -38,4 +39,15 @@ public function hasFullText(): bool
3839
{
3940
return method_exists(Grammar::class, 'compileFulltext');
4041
}
42+
43+
/**
44+
* Check if support anonymous migration.
45+
* This feature is added in late Laravel v8 and above.
46+
*
47+
* @return bool
48+
*/
49+
public function hasAnonymousMigration(): bool
50+
{
51+
return method_exists(Migrator::class, 'getMigrationClass');
52+
}
4153
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
{{ use }}
4+
5+
return new class extends Migration
6+
{
7+
/**
8+
* Run the migrations.
9+
*
10+
* @return void
11+
*/
12+
public function up()
13+
{
14+
{{ up }}
15+
}
16+
17+
/**
18+
* Reverse the migrations.
19+
*
20+
* @return void
21+
*/
22+
public function down()
23+
{
24+
{{ down }}
25+
}
26+
};

tests/Feature/FeatureTestCase.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ protected function generateMigrations(array $options = []): void
144144
$this->artisan(
145145
'migrate:generate',
146146
array_merge([
147-
'--path' => $this->getStorageMigrationsPath(),
148-
'--template-path' => base_path('stubs/migration.generate.stub'),
147+
'--path' => $this->getStorageMigrationsPath(),
149148
], $options)
150149
)
151150
->expectsQuestion('Do you want to log these migrations in the migrations table?', true)

tests/Feature/MySQL57/DBConnectionTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,8 @@ public function testDBConnection()
6363
$this->artisan(
6464
'migrate:generate',
6565
[
66-
'--connection' => 'mysql57',
67-
'--path' => $this->getStorageMigrationsPath(),
68-
'--template-path' => base_path('stubs/migration.generate.stub'),
66+
'--connection' => 'mysql57',
67+
'--path' => $this->getStorageMigrationsPath(),
6968
]
7069
)
7170
->expectsQuestion('Do you want to log these migrations in the migrations table?', true)
@@ -101,9 +100,8 @@ public function testLogMigrationToAnotherSource()
101100
$this->artisan(
102101
'migrate:generate',
103102
[
104-
'--connection' => 'mysql57',
105-
'--path' => $this->getStorageMigrationsPath(),
106-
'--template-path' => base_path('stubs/migration.generate.stub'),
103+
'--connection' => 'mysql57',
104+
'--path' => $this->getStorageMigrationsPath(),
107105
]
108106
)
109107
->expectsQuestion('Do you want to log these migrations in the migrations table?', true)

0 commit comments

Comments
 (0)