| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Database\Console;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Console\Command;
|
|
|
6 |
use Illuminate\Contracts\Events\Dispatcher;
|
|
|
7 |
use Illuminate\Database\Connection;
|
|
|
8 |
use Illuminate\Database\ConnectionResolverInterface;
|
|
|
9 |
use Illuminate\Database\Events\SchemaDumped;
|
|
|
10 |
use Illuminate\Filesystem\Filesystem;
|
|
|
11 |
use Illuminate\Support\Facades\Config;
|
|
|
12 |
use Symfony\Component\Console\Attribute\AsCommand;
|
|
|
13 |
|
|
|
14 |
#[AsCommand(name: 'schema:dump')]
|
|
|
15 |
class DumpCommand extends Command
|
|
|
16 |
{
|
|
|
17 |
/**
|
|
|
18 |
* The console command name.
|
|
|
19 |
*
|
|
|
20 |
* @var string
|
|
|
21 |
*/
|
|
|
22 |
protected $signature = 'schema:dump
|
|
|
23 |
{--database= : The database connection to use}
|
|
|
24 |
{--path= : The path where the schema dump file should be stored}
|
|
|
25 |
{--prune : Delete all existing migration files}';
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* The name of the console command.
|
|
|
29 |
*
|
|
|
30 |
* This name is used to identify the command during lazy loading.
|
|
|
31 |
*
|
|
|
32 |
* @var string|null
|
|
|
33 |
*
|
|
|
34 |
* @deprecated
|
|
|
35 |
*/
|
|
|
36 |
protected static $defaultName = 'schema:dump';
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* The console command description.
|
|
|
40 |
*
|
|
|
41 |
* @var string
|
|
|
42 |
*/
|
|
|
43 |
protected $description = 'Dump the given database schema';
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Execute the console command.
|
|
|
47 |
*
|
|
|
48 |
* @param \Illuminate\Database\ConnectionResolverInterface $connections
|
|
|
49 |
* @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
|
|
|
50 |
* @return int
|
|
|
51 |
*/
|
|
|
52 |
public function handle(ConnectionResolverInterface $connections, Dispatcher $dispatcher)
|
|
|
53 |
{
|
|
|
54 |
$connection = $connections->connection($database = $this->input->getOption('database'));
|
|
|
55 |
|
|
|
56 |
$this->schemaState($connection)->dump(
|
|
|
57 |
$connection, $path = $this->path($connection)
|
|
|
58 |
);
|
|
|
59 |
|
|
|
60 |
$dispatcher->dispatch(new SchemaDumped($connection, $path));
|
|
|
61 |
|
|
|
62 |
$info = 'Database schema dumped';
|
|
|
63 |
|
|
|
64 |
if ($this->option('prune')) {
|
|
|
65 |
(new Filesystem)->deleteDirectory(
|
|
|
66 |
database_path('migrations'), $preserve = false
|
|
|
67 |
);
|
|
|
68 |
|
|
|
69 |
$info .= ' and pruned';
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
$this->components->info($info.' successfully.');
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Create a schema state instance for the given connection.
|
|
|
77 |
*
|
|
|
78 |
* @param \Illuminate\Database\Connection $connection
|
|
|
79 |
* @return mixed
|
|
|
80 |
*/
|
|
|
81 |
protected function schemaState(Connection $connection)
|
|
|
82 |
{
|
|
|
83 |
return $connection->getSchemaState()
|
|
|
84 |
->withMigrationTable($connection->getTablePrefix().Config::get('database.migrations', 'migrations'))
|
|
|
85 |
->handleOutputUsing(function ($type, $buffer) {
|
|
|
86 |
$this->output->write($buffer);
|
|
|
87 |
});
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Get the path that the dump should be written to.
|
|
|
92 |
*
|
|
|
93 |
* @param \Illuminate\Database\Connection $connection
|
|
|
94 |
*/
|
|
|
95 |
protected function path(Connection $connection)
|
|
|
96 |
{
|
| 399 |
lars |
97 |
return tap($this->option('path') ?: database_path('schema/'.$connection->getName().'-schema.sql'), function ($path) {
|
| 148 |
lars |
98 |
(new Filesystem)->ensureDirectoryExists(dirname($path));
|
|
|
99 |
});
|
|
|
100 |
}
|
|
|
101 |
}
|