Skip to content

Database table name changed from migration_actions to actions #114

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

Merged
merged 4 commits into from
Jan 21, 2023
Merged
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
2 changes: 1 addition & 1 deletion config/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
|
*/

'table' => 'deploy_actions',
'table' => 'actions',

/*
|--------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use DragonCode\LaravelActions\Database\BaseChangeColumn;

return new class () extends BaseChangeColumn
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use DragonCode\LaravelActions\Database\BaseRenameMigrationsActionsTableToActions;

return new class () extends BaseRenameMigrationsActionsTableToActions
{
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use DragonCode\LaravelActions\Database\BaseChangeColumn;

class ChangeMigrationActionsTable extends BaseChangeColumn
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use DragonCode\LaravelActions\Database\BaseRenameMigrationsActionsTableToActions;

class RenameMigrationsActionsTableToActions extends BaseRenameMigrationsActionsTableToActions
{
}
55 changes: 55 additions & 0 deletions src/Database/BaseRenameMigrationsActionsTableToActions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelActions\Database;

use DragonCode\LaravelActions\Action;
use DragonCode\LaravelActions\Helpers\Config;
use Illuminate\Support\Facades\Schema;
use RuntimeException;

class BaseRenameMigrationsActionsTableToActions extends Action
{
protected Config $config;

public function __construct()
{
$this->config = app(Config::class);
}

public function up(): void
{
if (Schema::hasTable('migration_actions') && $this->doesntSame('migration_actions', $this->table())) {
$this->validateTable($this->table());

Schema::rename('migration_actions', $this->table());
}
}

public function down(): void
{
if (Schema::hasTable($this->table()) && $this->doesntSame('migration_actions', $this->table())) {
$this->validateTable('migration_actions');

Schema::rename($this->table(), 'migration_actions');
}
}

protected function validateTable(string $name): void
{
if (Schema::hasTable($name)) {
throw new RuntimeException(sprintf('A table named [%s] already exists. Change the table name settings in the [%s] configuration file.', $name, 'config/actions.php'));
}
}

protected function doesntSame(string $first, string $second): bool
{
return $first !== $second;
}

protected function table(): string
{
return $this->config->table();
}
}
2 changes: 1 addition & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public function boot(): void
if ($this->app->runningInConsole()) {
$this->publishConfig();

$this->registerCommands();
$this->registerAbout();
$this->registerCommands();
$this->registerMigrations();
$this->registerNotifications();
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Migrations/MigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function testRunMigrationAfterInstall(): void

$this->artisan('migrate')->run();

$this->assertDatabaseCount('migrations', 1);
$this->assertDatabaseCount('migrations', 2);
$this->assertDatabaseHas('migrations', ['migration' => '2022_08_18_180137_change_migration_actions_table']);
$this->assertDatabaseHas('migrations', ['migration' => '2023_01_21_172923_rename_migrations_actions_table_to_actions']);
}
}