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 1 commit
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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,15 @@ $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 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
15 changes: 15 additions & 0 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Package

public bool $hasAssets = false;

public bool $discoversMigrations = false;

public bool $runsMigrations = false;

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

public function discoversMigrations(bool $discoversMigrations = true): static
{
$this->discoversMigrations = $discoversMigrations;

return $this;
}

public function runsMigrations(bool $runsMigrations = true): static
{
$this->runsMigrations = $runsMigrations;
Expand All @@ -168,6 +177,12 @@ public function hasMigration(string $migrationFileName): static

public function hasMigrations(...$migrationFileNames): static
{
if (empty($migrationFileNames)) {
joelbutcher marked this conversation as resolved.
Show resolved Hide resolved
$this->discoversMigrations();

return $this;
}

$this->migrationFileNames = array_merge(
$this->migrationFileNames,
collect($migrationFileNames)->flatten()->toArray()
Expand Down
73 changes: 55 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,56 @@ public function packageView(?string $namespace)
{
return is_null($namespace) ? $this->package->shortName() : $this->package->viewNamespace;
}

protected function bootPackageMigrations(): void
{
$now = Carbon::now();

if ($this->package->discoversMigrations) {
$this->discoverMigrations();

return;
}

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();

$files = (new Filesystem())->files($this->package->basePath('/../database/migrations'));
joelbutcher marked this conversation as resolved.
Show resolved Hide resolved

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);
}
}
}
}
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]);
});
Loading