Skip to content

Commit 6f334b0

Browse files
committed
add mark all completed command
1 parent 358378d commit 6f334b0

File tree

5 files changed

+157
-39
lines changed

5 files changed

+157
-39
lines changed

src/CommandRunner.php

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ public function __construct(
7474
Container $laravel,
7575
DatabaseMigrationRepository $repository,
7676
Resolver $resolver,
77-
Filesystem $files)
78-
{
77+
Filesystem $files
78+
) {
7979
$this->laravel = $laravel;
8080
$this->files = $files;
8181
$this->resolver = $resolver;
@@ -116,9 +116,7 @@ public function run($paths = [], array $options = [])
116116
// run each of the outstanding migrations against a database connection.
117117
$files = $this->getCommandFiles($paths);
118118

119-
$this->requireFiles($migrations = $this->pendingMigrations(
120-
$files, $this->repository->getRan()
121-
));
119+
$migrations = $this->requirePendingMigrations($files);
122120

123121
// Once we have all these migrations that are outstanding we are ready to run
124122
// we will go ahead and run them "up". This will execute each migration as
@@ -128,11 +126,53 @@ public function run($paths = [], array $options = [])
128126
return $migrations;
129127
}
130128

129+
/**
130+
* @param array $paths
131+
* @param array $options
132+
* @return array
133+
*/
134+
public function markAllCompleted($paths = [], array $options = [])
135+
{
136+
$files = $this->getCommandFiles($paths);
137+
138+
$migrations = $this->requirePendingMigrations($files);
139+
140+
foreach ($migrations as $file) {
141+
$this->markFileCompleted($file);
142+
}
143+
144+
$this->note("<info>Marked all pending commands as completed</info>");
145+
146+
return $migrations;
147+
}
148+
149+
/**
150+
* @param string $file
151+
* @return void
152+
*/
153+
protected function markFileCompleted($file)
154+
{
155+
$name = $this->getCommandName($file);
156+
$this->repository->log($name);
157+
}
158+
159+
/**
160+
* @param array $files
161+
* @return array
162+
*/
163+
protected function requirePendingMigrations(array $files = [])
164+
{
165+
$this->requireFiles($migrations = $this->pendingMigrations(
166+
$files, $this->repository->getRan()
167+
));
168+
return $migrations;
169+
}
170+
131171
/**
132172
* Get the migration files that have not yet run.
133173
*
134-
* @param array $files
135-
* @param array $ran
174+
* @param array $files
175+
* @param array $ran
136176
* @return array
137177
*/
138178
protected function pendingMigrations($files, $ran)
@@ -211,8 +251,8 @@ protected function runCommand($file, $pretend)
211251
/**
212252
* Pretend to run the migrations.
213253
*
214-
* @param object|Command $command
215-
* @param string $method
254+
* @param object|Command $command
255+
* @param string $method
216256
* @return void
217257
*/
218258
protected function pretendToRun($command, $method)
@@ -227,8 +267,8 @@ protected function pretendToRun($command, $method)
227267
/**
228268
* Get all of the queries that would be run for a migration.
229269
*
230-
* @param object|Command $command
231-
* @param string $method
270+
* @param object|Command $command
271+
* @param string $method
232272
* @return array
233273
*/
234274
protected function getQueries($command, $method)
@@ -248,7 +288,7 @@ protected function getQueries($command, $method)
248288
/**
249289
* Resolve a migration instance from a file.
250290
*
251-
* @param string $file
291+
* @param string $file
252292
* @return object|Command
253293
*/
254294
public function resolve($file)
@@ -265,13 +305,13 @@ public function resolve($file)
265305
/**
266306
* Get all of the migration files in a given path.
267307
*
268-
* @param string|array $paths
308+
* @param string|array $paths
269309
* @return array
270310
*/
271311
public function getCommandFiles($paths)
272312
{
273313
return Collection::make($paths)->flatMap(function ($path) {
274-
return $this->files->glob($path.'/*_*.php');
314+
return $this->files->glob($path . '/*_*.php');
275315
})->filter()->sortBy(function ($file) {
276316
return $this->getCommandName($file);
277317
})->values()->keyBy(function ($file) {
@@ -282,7 +322,7 @@ public function getCommandFiles($paths)
282322
/**
283323
* Require in all the migration files in a given path.
284324
*
285-
* @param array $files
325+
* @param array $files
286326
* @return void
287327
*/
288328
public function requireFiles(array $files)
@@ -295,7 +335,7 @@ public function requireFiles(array $files)
295335
/**
296336
* Get the name of the migration.
297337
*
298-
* @param string $path
338+
* @param string $path
299339
* @return string
300340
*/
301341
public function getCommandName($path)
@@ -306,7 +346,7 @@ public function getCommandName($path)
306346
/**
307347
* Register a custom migration path.
308348
*
309-
* @param string $path
349+
* @param string $path
310350
* @return void
311351
*/
312352
public function path($path)
@@ -327,12 +367,12 @@ public function paths()
327367
/**
328368
* Set the default connection name.
329369
*
330-
* @param string $name
370+
* @param string $name
331371
* @return void
332372
*/
333373
public function setConnection($name)
334374
{
335-
if (! is_null($name)) {
375+
if (!is_null($name)) {
336376
$this->resolver->setDefaultConnection($name);
337377
}
338378

@@ -344,7 +384,7 @@ public function setConnection($name)
344384
/**
345385
* Resolve the database connection instance.
346386
*
347-
* @param string $connection
387+
* @param string $connection
348388
* @return \Illuminate\Database\Connection
349389
*/
350390
public function resolveConnection($connection)
@@ -355,7 +395,7 @@ public function resolveConnection($connection)
355395
/**
356396
* Get the schema grammar out of a migration connection.
357397
*
358-
* @param \Illuminate\Database\Connection $connection
398+
* @param \Illuminate\Database\Connection $connection
359399
* @return \Illuminate\Database\Schema\Grammars\Grammar
360400
*/
361401
protected function getSchemaGrammar($connection)
@@ -402,7 +442,7 @@ public function getFilesystem()
402442
/**
403443
* Raise a note event for the migrator.
404444
*
405-
* @param string $message
445+
* @param string $message
406446
* @return void
407447
*/
408448
protected function note($message)

src/Console/BaseCommand.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,20 @@ protected function getCommandPath()
3636
{
3737
return $this->laravel->databasePath().DIRECTORY_SEPARATOR.'commands';
3838
}
39+
40+
/**
41+
* Prepare the migration database for running.
42+
*
43+
* @return void
44+
*/
45+
protected function prepareDatabase()
46+
{
47+
$this->commandRunner->setConnection($this->option('database'));
48+
49+
if (! $this->commandRunner->repositoryExists()) {
50+
$this->call(
51+
'deploy-commands:install', ['--database' => $this->option('database')]
52+
);
53+
}
54+
}
3955
}

src/Console/MarkCommand.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace DaverDalas\LaravelPostDeployCommands\Console;
4+
5+
use DaverDalas\LaravelPostDeployCommands\CommandRunner;
6+
7+
class MarkCommand extends BaseCommand
8+
{
9+
/**
10+
* The name and signature of the console command.
11+
*
12+
* @var string
13+
*/
14+
protected $signature = 'deploy-commands:mark-all-completed {--database= : The database connection to use.}
15+
{--path= : The path of commands files to be executed.}';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Mark all post deploy commands as completed';
23+
24+
/**
25+
* The CommandRunner instance.
26+
*
27+
* @var \DaverDalas\LaravelPostDeployCommands\CommandRunner
28+
*/
29+
protected $commandRunner;
30+
31+
/**
32+
* Create a new migration command instance.
33+
*
34+
* @param \DaverDalas\LaravelPostDeployCommands\CommandRunner $commandRunner
35+
* @return void
36+
*/
37+
public function __construct(CommandRunner $commandRunner)
38+
{
39+
parent::__construct();
40+
41+
$this->commandRunner = $commandRunner;
42+
}
43+
44+
/**
45+
* Execute the console command.
46+
*
47+
* @return void
48+
* @throws \Exception
49+
* @throws \Throwable
50+
*/
51+
public function handle()
52+
{
53+
$this->prepareDatabase();
54+
55+
$this->commandRunner->setOutput($this->output);
56+
57+
// Next, we will check to see if a path option has been defined. If it has
58+
// we will use the path relative to the root of this installation folder
59+
// so that migrations may be run for any path within the applications.
60+
$this->commandRunner->markAllCompleted($this->getCommandPaths());
61+
}
62+
}

src/Console/RunCommand.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,4 @@ public function handle()
6262
'pretend' => $this->option('pretend'),
6363
]);
6464
}
65-
66-
/**
67-
* Prepare the migration database for running.
68-
*
69-
* @return void
70-
*/
71-
protected function prepareDatabase()
72-
{
73-
$this->commandRunner->setConnection($this->option('database'));
74-
75-
if (! $this->commandRunner->repositoryExists()) {
76-
$this->call(
77-
'deploy-commands:install', ['--database' => $this->option('database')]
78-
);
79-
}
80-
}
8165
}

src/ServiceProvider.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DaverDalas\LaravelPostDeployCommands\Console\InstallCommand;
66
use DaverDalas\LaravelPostDeployCommands\Console\MakeCommand;
7+
use DaverDalas\LaravelPostDeployCommands\Console\MarkCommand;
78
use DaverDalas\LaravelPostDeployCommands\Console\RunCommand;
89
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
910

@@ -24,7 +25,8 @@ public function boot()
2425
$this->commands([
2526
'command.deploy-commands.install',
2627
'command.deploy-commands.make',
27-
'command.deploy-commands.run'
28+
'command.deploy-commands.run',
29+
'command.deploy-commands.mark'
2830
]);
2931
}
3032
}
@@ -53,6 +55,8 @@ public function register()
5355
$this->registerMakeCommand();
5456

5557
$this->registerRunCommand();
58+
59+
$this->registerMarkCommand();
5660
}
5761
}
5862

@@ -142,6 +146,18 @@ protected function registerRunCommand()
142146
});
143147
}
144148

149+
/**
150+
* Register the command.
151+
*
152+
* @return void
153+
*/
154+
protected function registerMarkCommand()
155+
{
156+
$this->app->singleton('command.deploy-commands.mark', function ($app) {
157+
return new MarkCommand($app['deploy-commands.runner']);
158+
});
159+
}
160+
145161

146162
/**
147163
* Get the services provided by the provider.

0 commit comments

Comments
 (0)