Skip to content

Commit b0b509b

Browse files
authored
Merge pull request #162 from tech-wolf-tw/fix/issue-153-strip-timestamp-prefix-migration
Fix: Migration Name Prefixed with by timestamp twice
2 parents 98db123 + fdca3c7 commit b0b509b

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

src/PackageServiceProvider.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,15 @@ protected function generateMigrationName(string $migrationFileName, Carbon $now)
373373
}
374374
}
375375

376+
$migrationFileName = self::stripTimestampPrefix($migrationFileName);
376377
$timestamp = $now->format('Y_m_d_His');
377-
$migrationFileName = Str::of($migrationFileName)->snake()->finish('.php');
378+
$formattedFileName = Str::of($migrationFileName)->snake()->finish('.php');
378379

379-
return database_path($migrationsPath . $timestamp . '_' . $migrationFileName);
380+
return database_path("{$migrationsPath}{$timestamp}_{$formattedFileName}");
381+
}
382+
383+
private static function stripTimestampPrefix(string $filename): string
384+
{
385+
return preg_replace('/^\d{4}_\d{2}_\d{2}_\d{6}_/', '', $filename);
380386
}
381387
}

tests/PackageServiceProviderTests/PackageDiscoversMigrationsTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ public function configurePackage(Package $package)
6060
/** @var \Illuminate\Database\Migrations\Migrator $migrator */
6161
$migrator = app('migrator');
6262

63-
$this->assertCount(5, $migrator->paths());
64-
$this->assertStringContainsString('laravel_package_tools', $migrator->paths()[0]);
63+
$this->assertCount(6, $migrator->paths());
64+
$this->assertStringContainsString('laravel_package_tools', $migrator->paths()[1]);
6565
});

tests/PackageServiceProviderTests/PackageMigrationTest.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public function configurePackage(Package $package)
1313
->name('laravel-package-tools')
1414
->hasMigration('create_another_laravel_package_tools_table')
1515
->hasMigration('create_regular_laravel_package_tools_table')
16+
->hasMigration('2025_03_14_011123_create_custom_table')
1617
->runsMigrations();
1718
}
1819
}
@@ -28,6 +29,15 @@ public function configurePackage(Package $package)
2829
assertMigrationPublished('create_another_laravel_package_tools_table.php');
2930
});
3031

32+
it('can publish migration with a prefixed timestamp', function () {
33+
$this
34+
->artisan('vendor:publish --tag=package-tools-migrations')
35+
->doesntExpectOutput('hey')
36+
->assertExitCode(0);
37+
38+
assertMigrationPublished('2020_01_01_000003_create_custom_table.php');
39+
});
40+
3141
it('can publish the migration without being stubbed', function () {
3242
$this
3343
->artisan('vendor:publish --tag=package-tools-migrations')
@@ -80,6 +90,6 @@ public function configurePackage(Package $package)
8090
/** @var \Illuminate\Database\Migrations\Migrator $migrator */
8191
$migrator = app('migrator');
8292

83-
$this->assertCount(2, $migrator->paths());
93+
$this->assertCount(3, $migrator->paths());
8494
$this->assertStringContainsString('laravel_package_tools', $migrator->paths()[0]);
8595
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateCustomTable extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::create('custom', function (Blueprint $table) {
12+
$table->bigIncrements('id');
13+
14+
$table->timestamps();
15+
});
16+
}
17+
}

0 commit comments

Comments
 (0)