Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: discover migrations #153

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,24 @@ $package
->hasMigrations(['my_package_tables', 'some_other_migration']);
```

Calling `hasMigration` will also make migrations publishable. Users of your package will be able to publish the
Alternatively, if you wish to publish all migrations in your package by default, you may call `discoversMigrations`.

```php
$package
->name('your-package-name')
->discoversMigrations();
```

Calling this method will look for migrations in the `./database/migrations` directory of your project. However, if you have defined your migrations
in a different folder, you may pass a value to the `$path` variable to instruct the app to discover migrations from that location.

```php
$package
->name('your-package-name')
->discoversMigrations(path: '/path/to/your/migrations/folder');
```

Calling either `hasMigration`, `hasMigration` or `discoversMigrations` will also make migrations publishable. Users of your package will be able to publish the
migrations with this command:

```bash
Expand Down
12 changes: 12 additions & 0 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class Package

public bool $hasAssets = false;

public bool $discoversMigrations = false;

public ?string $migrationsPath = null;

public bool $runsMigrations = false;

public array $migrationFileNames = [];
Expand Down Expand Up @@ -152,6 +156,14 @@ public function hasAssets(): static
return $this;
}

public function discoversMigrations(bool $discoversMigrations = true, string $path = '/database/migrations'): static
{
$this->discoversMigrations = $discoversMigrations;
$this->migrationsPath = $path;

return $this;
}

public function runsMigrations(bool $runsMigrations = true): static
{
$this->runsMigrations = $runsMigrations;
Expand Down
72 changes: 54 additions & 18 deletions src/PackageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Spatie\LaravelPackageTools;

use Carbon\Carbon;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -76,24 +78,7 @@ public function boot()
], "{$this->packageView($this->package->viewNamespace)}-inertia-components");
}

$now = Carbon::now();
foreach ($this->package->migrationFileNames as $migrationFileName) {
$filePath = $this->package->basePath("/../database/migrations/{$migrationFileName}.php");
if (! file_exists($filePath)) {
// Support for the .stub file extension
$filePath .= '.stub';
}

$this->publishes([
$filePath => $this->generateMigrationName(
$migrationFileName,
$now->addSecond()
), ], "{$this->package->shortName()}-migrations");

if ($this->package->runsMigrations) {
$this->loadMigrationsFrom($filePath);
}
}
$this->bootPackageMigrations();

if ($this->package->hasTranslations) {
$this->publishes([
Expand Down Expand Up @@ -213,4 +198,55 @@ public function packageView(?string $namespace)
{
return is_null($namespace) ? $this->package->shortName() : $this->package->viewNamespace;
}

protected function bootPackageMigrations(): void
{
if ($this->package->discoversMigrations) {
$this->discoverMigrations();

return;
}

$now = Carbon::now();
foreach ($this->package->migrationFileNames as $migrationFileName) {
$filePath = $this->package->basePath("/../database/migrations/{$migrationFileName}.php");
if (! file_exists($filePath)) {
// Support for the .stub file extension
$filePath .= '.stub';
}

$this->publishes([
$filePath => $this->generateMigrationName(
$migrationFileName,
$now->addSecond()
), ], "{$this->package->shortName()}-migrations");

if ($this->package->runsMigrations) {
$this->loadMigrationsFrom($filePath);
}
}
}

protected function discoverMigrations(): void
{
$now = Carbon::now();
$path = trim($this->package->migrationsPath, '/');
$files = (new Filesystem())->files($this->package->basePath("/../{$path}"));

foreach ($files as $file) {
$filePath = $file->getPathname();

$migrationFileName = Str::replace(['.stub', '.php'], '', $file->getFilename());

$this->publishes([
$filePath => $this->generateMigrationName(
$migrationFileName,
$now->addSecond()
), ], "{$this->package->shortName()}-migrations");

if ($this->package->runsMigrations) {
$this->loadMigrationsFrom($filePath);
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Spatie\LaravelPackageTools\Tests\PackageServiceProviderTests;

use Spatie\LaravelPackageTools\Package;
use function Spatie\PestPluginTestTime\testTime;

trait ConfigurePackageDiscoverMigrationsTest
{
public function configurePackage(Package $package)
{
testTime()->freeze('2020-01-01 00:00:00');

$package
->name('laravel-package-tools')
->discoversMigrations()
->runsMigrations();
}
}

uses(ConfigurePackageDiscoverMigrationsTest::class);

it('publishes discovered migrations', function () {
$this
->artisan('vendor:publish --tag=package-tools-migrations')
->doesntExpectOutput('hey')
->assertExitCode(0);

assertMigrationPublished('create_another_laravel_package_tools_table.php');
});

it('can publish the migration without being stubbed', function () {
$this
->artisan('vendor:publish --tag=package-tools-migrations')
->assertExitCode(0);

assertMigrationPublished('create_regular_laravel_package_tools_table.php');
});

it('does not overwrite the existing migration', function () {
$this
->artisan('vendor:publish --tag=package-tools-migrations')
->assertExitCode(0);

$filePath = database_path('migrations/2020_01_01_000001_create_another_laravel_package_tools_table.php');

assertMigrationPublished('create_another_laravel_package_tools_table.php');


file_put_contents($filePath, 'modified');

$this
->artisan('vendor:publish --tag=package-tools-migrations')
->assertExitCode(0);

$this->assertStringEqualsFile($filePath, 'modified');
});

it('can run migrations which registers them', function () {
/** @var \Illuminate\Database\Migrations\Migrator $migrator */
$migrator = app('migrator');

$this->assertCount(5, $migrator->paths());
$this->assertStringContainsString('laravel_package_tools', $migrator->paths()[0]);
});