1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pmurkin\MongoSchemaDumper\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Facades\DB; |
7
|
|
|
|
8
|
|
|
class SchemaExport extends Command |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The name and signature of the console command. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $signature = 'schema:export |
16
|
|
|
{--databases= : List of databases for export} |
17
|
|
|
{--dump= : list of collections for dump} |
18
|
|
|
{--file=./schema.json : file with exported data}'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The console command description. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $description = 'Export schemas mongo databases to file'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* SchemaExport constructor. |
29
|
|
|
*/ |
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
parent::__construct(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Execute the console command. |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function handle() |
41
|
|
|
{ |
42
|
|
|
$result = []; |
43
|
|
|
|
44
|
|
|
$databases = $this->option('databases'); |
45
|
|
|
if (!$databases) { |
46
|
|
|
$this->warn('Not defined databases'); |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$databases = explode(',', $databases); |
51
|
|
|
if (count($databases) === 0) { |
52
|
|
|
$this->warn('Not defined databases'); |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$dump = $this->option('dump'); |
57
|
|
|
$file = $this->option('file'); |
58
|
|
|
|
59
|
|
|
foreach ($databases as $database) { |
60
|
|
|
$result[$database] = $this->getInfo($database); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($dump) { |
64
|
|
|
$dump = explode(',', $dump); |
65
|
|
|
foreach ($dump as $collection) { |
66
|
|
|
$collection = explode('.', $collection); |
67
|
|
|
$data = DB::getMongoClient()->selectCollection($collection[0], $collection[1])->find()->toArray(); |
68
|
|
|
|
69
|
|
|
foreach ($data as $idx => $item) { |
70
|
|
|
$data[$idx]['_id'] = (string)$item['_id']; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$result[$collection[0]][$collection[1]]['data'] = $data; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
file_put_contents($file, json_encode($result, JSON_PRETTY_PRINT)); |
78
|
|
|
|
79
|
|
|
$this->info('Export is done'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $database |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
private function getInfo($database) |
87
|
|
|
{ |
88
|
|
|
$result = []; |
89
|
|
|
|
90
|
|
|
$collections = DB::getMongoClient()->selectDatabase($database)->listCollections(); |
91
|
|
|
foreach($collections as $collection) { |
92
|
|
|
$collectionName = $collection->getName(); |
93
|
|
|
$result[$collectionName] = [ |
94
|
|
|
'indexes' => [], |
95
|
|
|
'options' => $collection->getOptions(), |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
$indexes = DB::getMongoClient()->selectCollection($database, $collectionName)->listIndexes(); |
99
|
|
|
foreach($indexes as $index) { |
100
|
|
|
$idx = [ |
101
|
|
|
'name' => $index->getName(), |
102
|
|
|
'ns' => $index->getNamespace(), |
103
|
|
|
'key' => $index->getKey(), |
104
|
|
|
'v' => $index->getVersion(), |
105
|
|
|
]; |
106
|
|
|
|
107
|
|
|
if ($index->isUnique()) { |
108
|
|
|
$idx['unique'] = true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($index->isSparse()) { |
112
|
|
|
$idx['sparse'] = true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$result[$collectionName]['indexes'][] = $idx; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $result; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|