Skip to content

Commit 6abe80f

Browse files
committed
Change namespace from "McMatters\DbCommands" to "McMatters\LaravelDbCommands".
Add new commands migrate:drop-single. Add package autodiscovering feature. Use "ConfirmableTrait" in DropTables class. Import php functions and constants for better performance.
1 parent fd9b45f commit 6abe80f

File tree

8 files changed

+220
-94
lines changed

8 files changed

+220
-94
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ Package with laravel database commands.
88
composer require mcmatters/laravel-db-commands
99
```
1010

11-
Include the service provider within your `config/app.php` file.
11+
If you don't use package discovering feature, then just include the service provider within your `config/app.php` file.
1212

1313
```php
1414
'providers' => [
15-
McMatters\DbCommands\ServiceProvider::class,
15+
McMatters\LaravelDbCommands\ServiceProvider::class,
1616
]
1717
```
1818

@@ -22,3 +22,4 @@ Available commands:
2222

2323
* `php artisan db:drop-tables` — drops all tables.
2424
* `php artisan migrate:single {"file" or "class"}` — migrate single migration by file or class name.
25+
* `php artisan migrate:drop-single {"file" or "class"}` — drop single migration by file or class name.

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@
1515
},
1616
"autoload": {
1717
"psr-4": {
18-
"McMatters\\DbCommands\\": "src/"
18+
"McMatters\\LaravelDbCommands\\": "src/"
19+
}
20+
},
21+
"extra": {
22+
"laravel": {
23+
"providers": [
24+
"McMatters\\LaravelDbCommands\\ServiceProvider"
25+
]
1926
}
2027
}
2128
}

src/Console/Commands/DropTables.php

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@
22

33
declare(strict_types = 1);
44

5-
namespace McMatters\DbCommands\Console\Commands;
5+
namespace McMatters\LaravelDbCommands\Console\Commands;
66

77
use Illuminate\Console\Command;
8-
use Illuminate\Support\Facades\Config;
8+
use Illuminate\Console\ConfirmableTrait;
99
use Illuminate\Support\Facades\DB;
1010
use Illuminate\Support\Facades\Schema;
11+
use function reset;
1112

1213
/**
1314
* Class DropTables
1415
*
15-
* @package McMatters\DbCommands\Console\Commands
16+
* @package McMatters\LaravelDbCommands\Console\Commands
1617
*/
1718
class DropTables extends Command
1819
{
20+
use ConfirmableTrait;
21+
1922
/**
2023
* @var string
2124
*/
@@ -27,17 +30,22 @@ class DropTables extends Command
2730
protected $description = 'Drops all tables in the default configuration.';
2831

2932
/**
30-
* Run command.
33+
* Support for =< 5.4 versions.
3134
*
3235
* @return void
3336
*/
37+
public function fire()
38+
{
39+
$this->handle();
40+
}
41+
42+
/**
43+
* @return void
44+
*/
3445
public function handle()
3546
{
36-
if (!$this->isForced() && $this->isProduction()) {
37-
$this->error(
38-
'This action can\'t be performed in non-local environment.
39-
Please use --force option if you really want to proceed.'
40-
);
47+
if (!$this->confirmToProceed()) {
48+
return;
4149
}
4250

4351
Schema::disableForeignKeyConstraints();
@@ -48,22 +56,4 @@ public function handle()
4856

4957
Schema::enableForeignKeyConstraints();
5058
}
51-
52-
/**
53-
* Check force mode.
54-
*
55-
* @return bool
56-
*/
57-
protected function isForced(): bool
58-
{
59-
return null !== $this->option('force');
60-
}
61-
62-
/**
63-
* @return bool
64-
*/
65-
protected function isProduction(): bool
66-
{
67-
return in_array(Config::get('app.env'), ['production', 'live'], true);
68-
}
6959
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace McMatters\LaravelDbCommands\Console\Commands;
6+
7+
use Illuminate\Console\ConfirmableTrait;
8+
use Illuminate\Contracts\Filesystem\FileNotFoundException;
9+
use Illuminate\Database\Console\Migrations\BaseCommand;
10+
use McMatters\LaravelDbCommands\Console\Commands\Traits\MigrateTrait;
11+
use McMatters\LaravelDbCommands\Extensions\Database\Migrator;
12+
use RuntimeException;
13+
use const false;
14+
use function count, strpos;
15+
16+
/**
17+
* Class MigrateDropSingle
18+
*
19+
* @package McMatters\LaravelDbCommands\Console\Commands
20+
*/
21+
class MigrateDropSingle extends BaseCommand
22+
{
23+
use ConfirmableTrait, MigrateTrait;
24+
25+
/**
26+
* @var string
27+
*/
28+
protected $signature = 'migrate:drop-single
29+
{--file= : The file of migration to be executed.}
30+
{--class= : The class name of migration.}
31+
{--database= : The database connection to use.}
32+
{--force : Force the operation to run when in production.}
33+
{--pretend : Dump the SQL queries that would be run.}';
34+
35+
/**
36+
* @var string
37+
*/
38+
protected $description = 'Drop the single database migration';
39+
40+
/**
41+
* @var Migrator
42+
*/
43+
protected $migrator;
44+
45+
/**
46+
* MigrateDropSingle constructor.
47+
*
48+
* @param Migrator $migrator
49+
*/
50+
public function __construct(Migrator $migrator)
51+
{
52+
parent::__construct();
53+
54+
$this->migrator = $migrator;
55+
}
56+
57+
/**
58+
* Support for =< 5.4 versions.
59+
*
60+
* @return void
61+
* @throws FileNotFoundException
62+
* @throws RuntimeException
63+
*/
64+
public function fire()
65+
{
66+
$this->handle();
67+
}
68+
69+
/**
70+
* @return void
71+
* @throws FileNotFoundException
72+
* @throws RuntimeException
73+
*/
74+
public function handle()
75+
{
76+
if (!$this->confirmToProceed()) {
77+
return;
78+
}
79+
80+
$this->checkRequirements();
81+
82+
$this->migrator->rollback(
83+
$this->getMigrationFile(),
84+
[
85+
'step' => count($this->migrator->getRepository()->getRan()),
86+
'pretend' => $this->option('pretend'),
87+
]
88+
);
89+
90+
foreach ($this->migrator->getNotes() as $note) {
91+
if (strpos($note, 'Migration not found') === false) {
92+
$this->output->writeln($note);
93+
}
94+
}
95+
}
96+
}

src/Console/Commands/MigrateSingle.php

Lines changed: 9 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@
22

33
declare(strict_types = 1);
44

5-
namespace McMatters\DbCommands\Console\Commands;
5+
namespace McMatters\LaravelDbCommands\Console\Commands;
66

77
use Illuminate\Contracts\Filesystem\FileNotFoundException;
88
use Illuminate\Database\Console\Migrations\MigrateCommand;
9-
use Illuminate\Support\Arr;
10-
use Illuminate\Support\Str;
11-
use McMatters\DbCommands\Extenders\Database\Migrator;
9+
use McMatters\LaravelDbCommands\Console\Commands\Traits\MigrateTrait;
10+
use McMatters\LaravelDbCommands\Extensions\Database\Migrator;
1211
use RuntimeException;
1312

1413
/**
1514
* Class MigrateSingle
1615
*
17-
* @package McMatters\DbCommands\Console\Commands
16+
* @package McMatters\LaravelDbCommands\Console\Commands
1817
*/
1918
class MigrateSingle extends MigrateCommand
2019
{
20+
use MigrateTrait;
21+
2122
/**
2223
* @var string
2324
*/
@@ -31,7 +32,7 @@ class MigrateSingle extends MigrateCommand
3132
/**
3233
* @var string
3334
*/
34-
protected $description = 'Run the database migration';
35+
protected $description = 'Run the single database migration';
3536

3637
/**
3738
* MigrateSingle constructor.
@@ -47,8 +48,8 @@ public function __construct(Migrator $migrator)
4748
* Support for =< 5.4 versions.
4849
*
4950
* @return void
50-
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
51-
* @throws \RuntimeException
51+
* @throws FileNotFoundException
52+
* @throws RuntimeException
5253
*/
5354
public function fire()
5455
{
@@ -77,52 +78,4 @@ public function handle()
7778
$this->output->writeln($note);
7879
}
7980
}
80-
81-
/**
82-
* @return string
83-
* @throws FileNotFoundException
84-
*/
85-
protected function getMigrationFile(): string
86-
{
87-
if ($this->hasOption('class')) {
88-
$file = $this->getFileByClass();
89-
} else {
90-
$file = $this->getMigrationPath().'/'.$this->argument('file');
91-
}
92-
93-
if (!$file || !file_exists($file)) {
94-
throw new FileNotFoundException('File with migration not found.');
95-
}
96-
97-
return $file;
98-
}
99-
100-
/**
101-
* @return string|null
102-
*/
103-
protected function getFileByClass()
104-
{
105-
$class = $this->option('class');
106-
107-
if (!$class) {
108-
return null;
109-
}
110-
111-
$class = Str::snake($class);
112-
113-
$files = glob("{$this->getMigrationPath()}/[0-9_]*{$class}.php");
114-
115-
return Arr::first($files);
116-
}
117-
118-
/**
119-
* @return void
120-
* @throws RuntimeException
121-
*/
122-
protected function checkRequirements()
123-
{
124-
if (!$this->hasOption('file') && !$this->hasOption('class')) {
125-
throw new RuntimeException('You must pass at least one argument "file" or "class"');
126-
}
127-
}
12881
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace McMatters\LaravelDbCommands\Console\Commands\Traits;
6+
7+
use Illuminate\Contracts\Filesystem\FileNotFoundException;
8+
use Illuminate\Support\Arr;
9+
use Illuminate\Support\Str;
10+
use RuntimeException;
11+
use const null;
12+
use function glob, file_exists;
13+
14+
/**
15+
* Trait MigrateTrait
16+
*
17+
* @package McMatters\LaravelDbCommands\Console\Commands\Traits
18+
*/
19+
trait MigrateTrait
20+
{
21+
/**
22+
* @return string
23+
* @throws FileNotFoundException
24+
*/
25+
protected function getMigrationFile(): string
26+
{
27+
if ($this->hasOption('class')) {
28+
$file = $this->getFileByClass();
29+
} else {
30+
$file = $this->getMigrationPath().'/'.$this->argument('file');
31+
}
32+
33+
if (!$file || !file_exists($file)) {
34+
throw new FileNotFoundException('File with migration not found.');
35+
}
36+
37+
return $file;
38+
}
39+
40+
/**
41+
* @return string|null
42+
*/
43+
protected function getFileByClass()
44+
{
45+
$class = $this->option('class');
46+
47+
if (!$class) {
48+
return null;
49+
}
50+
51+
$class = Str::snake($class);
52+
53+
$files = glob("{$this->getMigrationPath()}/[0-9_]*{$class}.php");
54+
55+
return Arr::first($files);
56+
}
57+
58+
/**
59+
* @return void
60+
* @throws RuntimeException
61+
*/
62+
protected function checkRequirements()
63+
{
64+
if (!$this->hasOption('file') && !$this->hasOption('class')) {
65+
throw new RuntimeException('You must pass at least one argument "file" or "class"');
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)