1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Example\Migrations\TestAntiMattr\MongoDB; |
4
|
|
|
|
5
|
|
|
use AntiMattr\MongoDB\Migrations\AbstractMigration; |
6
|
|
|
use MongoDB\Database; |
7
|
|
|
|
8
|
|
|
class Version20140822185743 extends AbstractMigration |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @return string |
12
|
|
|
*/ |
13
|
|
|
public function getDescription() |
14
|
|
|
{ |
15
|
|
|
return 'Second Version executes console script'; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function up(Database $db) |
19
|
|
|
{ |
20
|
|
|
$testA = $db->selectCollection('test_a'); |
21
|
|
|
$this->analyze($testA); |
22
|
|
|
|
23
|
|
|
// This will remove all records with actor property starting with character a-m |
24
|
|
|
$result = $this->executeScript($db, 'test_script.js'); |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function down(Database $db) |
28
|
|
|
{ |
29
|
|
|
// this down() migration is auto-generated, please modify it to your needs |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* This preUp is not required |
34
|
|
|
* I use it to demonstrate the analyzer. |
35
|
|
|
*/ |
36
|
|
|
public function preUp(Database $db) |
37
|
|
|
{ |
38
|
|
|
$testA = $db->selectCollection('test_a'); |
39
|
|
|
|
40
|
|
|
$testDocuments = []; |
41
|
|
|
|
42
|
|
|
for ($i = 0; $i < 100; ++$i) { |
43
|
|
|
$testDocument = []; |
44
|
|
|
$testDocument['iteration'] = $i; |
45
|
|
|
$testDocument['actor'] = $this->generateRandomString(); |
|
|
|
|
46
|
|
|
$testDocument['object'] = $this->generateRandomString(); |
47
|
|
|
$testDocument['target'] = $this->generateRandomString(); |
48
|
|
|
$testDocument['verb'] = $this->generateRandomString(); |
49
|
|
|
$testDocuments[] = $testDocument; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$testA->insertMany($testDocuments); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* This postUp is not required |
57
|
|
|
* I use it to demonstrate the analyzer. |
58
|
|
|
*/ |
59
|
|
|
public function postUp(Database $db) |
60
|
|
|
{ |
61
|
|
|
$testA = $db->selectCollection('test_a'); |
62
|
|
|
$testA->drop(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function generateRandomString() |
66
|
|
|
{ |
67
|
|
|
$length = rand(10, 50); |
68
|
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
69
|
|
|
$randomString = ''; |
70
|
|
|
for ($i = 0; $i < $length; ++$i) { |
71
|
|
|
$randomString .= $characters[rand(0, strlen($characters) - 1)]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $randomString; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|