1 | <?php |
||||
2 | |||||
3 | namespace App\Console\Commands; |
||||
4 | |||||
5 | use Illuminate\Contracts\Events\Dispatcher; |
||||
6 | use Illuminate\Database\ConnectionResolverInterface; |
||||
7 | use Illuminate\Database\Console\DumpCommand; |
||||
8 | use LibreNMS\DB\Schema; |
||||
9 | use Symfony\Component\Console\Output\StreamOutput; |
||||
10 | use Symfony\Component\Yaml\Yaml; |
||||
11 | |||||
12 | class SchemaDumpCommand extends DumpCommand |
||||
13 | { |
||||
14 | /** |
||||
15 | * Create a new command instance. |
||||
16 | * |
||||
17 | * @return void |
||||
18 | */ |
||||
19 | public function __construct() |
||||
20 | { |
||||
21 | $this->signature .= '{--snapshots : Dump snapshots to reduce initial migration time}'; |
||||
22 | parent::__construct(); |
||||
23 | } |
||||
24 | |||||
25 | /** |
||||
26 | * Execute the console command. |
||||
27 | * |
||||
28 | * @return int |
||||
29 | */ |
||||
30 | public function handle(ConnectionResolverInterface $connections, Dispatcher $dispatcher) |
||||
31 | { |
||||
32 | $database = $this->option('database'); |
||||
33 | |||||
34 | if ($this->option('snapshots')) { |
||||
35 | $databases = $database ? [$database] : ['mysql', 'testing', 'testing_persistent']; |
||||
36 | foreach ($databases as $database) { |
||||
37 | $this->line("Database: $database"); |
||||
38 | $this->input->setOption('database', $database); |
||||
39 | parent::handle($connections, $dispatcher); |
||||
40 | } |
||||
41 | |||||
42 | // in memory db doesn't dump right, copy the sqlite on-disk dump |
||||
43 | $persistent_dump_file = base_path('/database/schema/testing_persistent-schema.dump'); |
||||
44 | if (in_array('testing_persistent', $databases) && file_exists($persistent_dump_file)) { |
||||
45 | copy($persistent_dump_file, base_path('/database/schema/testing_memory-schema.dump')); |
||||
46 | } |
||||
47 | |||||
48 | return 0; |
||||
49 | } |
||||
50 | |||||
51 | $stdout = new StreamOutput(fopen('php://stdout', 'w')); |
||||
52 | $parameters = ['--force' => true, '--ansi' => true]; |
||||
53 | if ($database) { |
||||
54 | $parameters['--database'] = $database; |
||||
55 | } |
||||
56 | |||||
57 | \Artisan::call('migrate', $parameters, $stdout); |
||||
58 | |||||
59 | $file = $this->option('path') ?: base_path('/misc/db_schema.yaml'); |
||||
60 | $yaml = Yaml::dump(Schema::dump($database), 3, 2); |
||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
61 | |||||
62 | if (file_put_contents($file, $yaml)) { |
||||
63 | $this->info(basename($file) . ' updated!'); |
||||
64 | |||||
65 | return 0; |
||||
66 | } |
||||
67 | |||||
68 | $this->error('Failed to write file ' . $file); |
||||
0 ignored issues
–
show
Are you sure
$file of type string|string[]|true can be used in concatenation ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
69 | |||||
70 | return 1; |
||||
71 | } |
||||
72 | } |
||||
73 |