-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.php
More file actions
127 lines (103 loc) · 3.23 KB
/
migrate.php
File metadata and controls
127 lines (103 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
include_once('config.php');
/*
* Executes each migration contained in config.php
*/
foreach( $migrations as $migration ) {
if ( ! array_key_exists('origin', $migration) ) throw new \RuntimeExcetion('Origin information is requered.');
/*
* Builds the dump command
*/
$origin = $migration['origin'];
$dumpCommand = buildDumpCommand( $origin );
echo "Executing dump command $dumpCommand \n";
exec( $dumpCommand );
/*
* Builds the restore command
*/
$dumpDir = sprintf("./dump/%s", $origin['database']);
$destination = $migration['destination'];
$destination['dir'] = $dumpDir;
$restoreCommand = buildRestoreCommand( $destination );
echo "Executing restore command $restoreCommand \n";
exec( $restoreCommand );
}
/**
* Builds the command line for dumping a database
* @param array $origin origin parameters as follows:
*
* <code>
* [
* 'host' => '',
* 'username' => '',
* 'password' => '',
* 'port' => '',
* 'database' => '',
* 'exclude-collection' => ''
* ]
* </code>
*
* @return string
*/
function buildDumpCommand( Array $origin ) {
$command = "mongodump";
if ( array_key_exists('host', $origin ) ) {
$command .= sprintf(" --host %s", $origin['host']);
}
if ( array_key_exists('username', $origin ) ) {
$command .= sprintf(" --username %s", $origin['username']);
}
if ( array_key_exists('password', $origin ) ) {
$command .= sprintf(" --password %s", $origin['password']);
}
if ( array_key_exists('port', $origin ) ) {
$command .= sprintf(" --port %s", $origin['port']);
}
if ( array_key_exists('database', $origin ) ) {
$command .= sprintf(" --db %s", $origin['database']);
}
if ( array_key_exists('authentication-database', $origin ) ) {
$command .= sprintf(" --authenticationDatabase %s", $origin['authentication-database']);
}
if ( array_key_exists('exclude-collection', $origin ) ) {
$command .= sprintf(" --excludeCollection %s", $origin['exclude-collection']);
}
return $command;
}
/**
* Builds the command line for restore a database
*
* @param array $destination as follows
* <code>
* [
* 'database' => '',
* 'username' => '',
* 'password' => '',
* 'dir' => ''
* ]
* </code>
*
* @return string
*/
function buildRestoreCommand( Array $destination ) {
$command = "mongorestore --drop";
if ( array_key_exists('host', $destination) ) {
$command .= sprintf(" --host %s", $destination['host']);
}
if ( array_key_exists('username', $destination) ) {
$command .= sprintf(" --username %s", $destination['username']);
}
if ( array_key_exists('password', $destination ) ) {
$command .= sprintf(" --password '%s'", $destination['password']);
}
if ( array_key_exists('database', $destination) ) {
$command .= sprintf(" --db %s", $destination['database']);
}
if ( array_key_exists('authentication-database', $destination ) ) {
$command .= sprintf(" --authenticationDatabase %s", $destination['authentication-database']);
}
if ( array_key_exists('dir', $destination) ) {
$command .= sprintf(" --dir %s", $destination['dir']);
}
return $command;
}