Skip to content

Commit

Permalink
Merge pull request #86 from creasico/feat/publish-migration
Browse files Browse the repository at this point in the history
feat(db): add basic database migration for `addresses` table, fix #85
  • Loading branch information
feryardiant authored Aug 6, 2024
2 parents 60eb2fb + 51ef9c5 commit d2b22a8
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ jobs:
composer require "laravel/framework=${{ matrix.laravel }}" --no-update
composer update --prefer-dist --no-interaction --no-progress
- name: Run migrations
run: |
composer testbench vendor:publish -- --tag creasi-migrations
composer testbench migrate
- name: Run tests
run: composer test -- --coverage

Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,14 @@ class User extends Model implements HasAddresses
}
```

To be able to use `Address` model, all you need is to publish the migration, like so

```sh
php artisan vendor:publish --tag creasi-migrations
```

Then simply run `artisan migrate` to apply the additional migrations.

### Databases

The database structure documentation please consult to [`database/README.md`](https://github.com/creasico/laravel-nusa/blob/main/database/README.md).
Expand Down
4 changes: 2 additions & 2 deletions database/.gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
migrations export-ignore
seeders export-ignore
migrations/*.php export-ignore
seeders export-ignore
22 changes: 22 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Creasi\Nusa;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;

Expand Down Expand Up @@ -58,6 +60,10 @@ protected function registerPublishables()
$this->publishes([
self::LIB_PATH.'/resources/lang' => \resource_path('lang/vendor/creasico'),
], ['creasi-lang']);

$this->publishes([
self::LIB_PATH.'/database/migrations/create_addresses_tables.php.stub' => $this->getMigrationFileName('create_addresses_tables.php'),
], 'creasi-migrations');
}

protected function registerBindings()
Expand Down Expand Up @@ -85,6 +91,22 @@ protected function registerBindings()
});
}

/**
* Returns existing migration file if found, else uses the current timestamp.
*
* @link https://github.com/spatie/laravel-permission/blob/main/src/PermissionServiceProvider.php
*/
protected function getMigrationFileName(string $migrationFileName): string
{
$timestamp = date('Y_m_d_His');
$filesystem = $this->app->make(Filesystem::class);

return Collection::make([$this->app->databasePath().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR])
->flatMap(fn ($path) => $filesystem->glob($path.'*_'.$migrationFileName))
->push($this->app->databasePath()."/migrations/{$timestamp}_{$migrationFileName}")
->first();
}

public function provides()
{
return [
Expand Down
6 changes: 0 additions & 6 deletions workbench/app/Providers/WorkbenchServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
use Illuminate\Support\ServiceProvider;
use Workbench\App\Console\StatCommand;

use function Orchestra\Testbench\workbench_path;

class WorkbenchServiceProvider extends ServiceProvider
{
/**
Expand All @@ -23,10 +21,6 @@ public function register(): void
*/
public function boot(): void
{
$this->loadMigrationsFrom(
workbench_path('database/migrations')
);

if (app()->runningInConsole()) {
$this->commands([
StatCommand::class,
Expand Down

0 comments on commit d2b22a8

Please sign in to comment.