Skip to content

Commit b8a6c0e

Browse files
authored
Merge pull request #142 from kitloong/feature/anonymous
Generate anonymous migration
2 parents 5007ee7 + b76ecfe commit b8a6c0e

10 files changed

+76
-21
lines changed

.phpmd.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</rule>
2929
<rule ref="rulesets/codesize.xml/ExcessiveClassComplexity">
3030
<properties>
31-
<property name="maximum" value="60"/>
31+
<property name="maximum" value="65"/>
3232
</properties>
3333
</rule>
3434
<rule ref="rulesets/codesize.xml/ExcessiveParameterList">

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/DBAL/RegisterColumnType.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public function handle(): void
7676
*/
7777
private function registerLaravelColumnType(): void
7878
{
79-
/** @var array<string, string> $typesMap */
8079
$typesMap = array_flip(Types::ADDITIONAL_TYPES_MAP);
8180

8281
foreach ($typesMap as $type => $doctrineTypeClassName) {
@@ -149,7 +148,7 @@ private function getCustomTypes(): Collection
149148
* Add or override doctrine type.
150149
*
151150
* @param string $type
152-
* @param string $class The class name which is extends {@see \Doctrine\DBAL\Types\Type}.
151+
* @param class-string<\Doctrine\DBAL\Types\Type> $class The class name which is extends {@see \Doctrine\DBAL\Types\Type}.
153152
* @throws \Doctrine\DBAL\Exception
154153
*/
155154
private function addOrOverrideType(string $type, string $class): void

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/Migration/Blueprint/Support/MergeTimestamps.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,18 @@ public function merge(array $lines, bool $tz): array
6767
/**
6868
* Check if column name (created_at or updated_at) is possible a timestamps.
6969
*
70-
* @param string $name Column name, created_at or updated_at.
70+
* @param \KitLoong\MigrationsGenerator\Enum\Migrations\ColumnName $columnName Column name, created_at or updated_at.
7171
* @param \KitLoong\MigrationsGenerator\Migration\Blueprint\Method $method
7272
* @param bool $tz Is timezone.
7373
* @return bool
7474
*/
75-
private function checkTimestamps(string $name, Method $method, bool $tz): bool
75+
private function checkTimestamps(ColumnName $columnName, Method $method, bool $tz): bool
7676
{
7777
if (!$this->isPossibleTimestampsColumn($method, $tz)) {
7878
return false;
7979
}
8080

81-
if ($method->getValues()[0] !== $name) {
81+
if ($method->getValues()[0] !== $columnName->getValue()) {
8282
return false;
8383
}
8484

src/MigrationsGeneratorServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ protected function registerConfig()
9999
* Make column generator singleton by type.
100100
*
101101
* @param \KitLoong\MigrationsGenerator\Enum\Migrations\Method\ColumnType $type
102-
* @param string $columnTypeGenerator
102+
* @param class-string<\KitLoong\MigrationsGenerator\Migration\Generator\Columns\ColumnTypeGenerator> $columnTypeGenerator
103103
*/
104104
protected function columnTypeSingleton(ColumnType $type, string $columnTypeGenerator): void
105105
{

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)