1+ <?php
2+
3+ use Illuminate \Database \Schema \Blueprint ;
4+ use Illuminate \Database \Migrations \Migration ;
5+
6+ class AddStoredToStatementRoot extends Migration {
7+
8+ /**
9+ * Run the migrations.
10+ *
11+ * @return void
12+ */
13+ public function up ()
14+ {
15+ set_time_limit (0 );
16+
17+ $ db = \DB ::getMongoDB ();
18+ $ statementsCollection = new MongoCollection ($ db , 'statements ' );
19+
20+ $ statementsCollection ->createIndex (['stored ' => 1 ], ['background ' =>1 , 'socketTimeoutMS ' =>-1 ]);
21+ $ statementsCollection ->createIndex (['stored ' => -1 ], ['background ' =>1 , 'socketTimeoutMS ' =>-1 ]);
22+ $ statementsCollection ->createIndex (['lrs_id ' => 1 , 'stored ' => 1 ], ['background ' =>1 , 'socketTimeoutMS ' =>-1 ]);
23+ $ statementsCollection ->createIndex (['lrs_id ' => 1 , 'stored ' => -1 ], ['background ' =>1 , 'socketTimeoutMS ' =>-1 ]);
24+
25+ $ statementsCursor = $ statementsCollection ->find ();
26+
27+ $ remaining = $ statementsCursor ->count ();
28+ print ($ remaining . ' statements total ' . PHP_EOL );
29+
30+ $ maxBatchSize = 10000 ;
31+
32+ while ($ statementsCursor ->hasNext ()) {
33+ $ batch = new MongoUpdateBatch ($ statementsCollection );
34+ $ batchSize = 0 ;
35+
36+ while ($ batchSize < $ maxBatchSize && $ statementsCursor ->hasNext ()) {
37+ $ batchSize ++;
38+ $ statement = $ statementsCursor ->next ();
39+ $ statementStored = new Carbon \Carbon ($ statement ['statement ' ]['stored ' ]);
40+
41+ $ query = [
42+ 'q ' => ['_id ' => $ statement ['_id ' ]],
43+ 'u ' => ['$set ' => ["stored " => new \MongoDate ($ statementStored ->timestamp , $ statementStored ->micro )]],
44+ 'multi ' => false ,
45+ 'upsert ' => false ,
46+ ];
47+
48+ if (isset ($ statement ['refs ' ])) {
49+ foreach ($ statement ['refs ' ] as $ key => $ refStatement ) {
50+ if (isset ($ refStatement ['timestamp ' ]) && !$ refStatement ['timestamp ' ] instanceof MongoDate) {
51+ $ timestamp = new Carbon \Carbon ($ refStatement ['timestamp ' ]);
52+ $ query ['u ' ]['$set ' ]['refs. ' .$ key .'.timestamp ' ] = new \MongoDate ($ timestamp ->timestamp , $ timestamp ->micro );
53+ }
54+ if (isset ($ refStatement ['stored ' ]) && !$ stored ['stored ' ] instanceof MongoDate) {
55+ $ stored = new Carbon \Carbon ($ refStatement ['stored ' ]);
56+ $ query ['u ' ]['$set ' ]['refs. ' .$ key .'.stored ' ] = new \MongoDate ($ stored ->timestamp , $ stored ->micro );
57+ }
58+ }
59+ }
60+
61+ $ batch ->add ((object ) $ query );
62+ }
63+ $ batch ->execute ();
64+ $ remaining -= $ batchSize ;
65+
66+ print ($ remaining . ' remaining ' . PHP_EOL );
67+ }
68+ }
69+
70+ /**
71+ * Reverse the migrations.
72+ *
73+ * @return void
74+ */
75+ public function down ()
76+ {
77+ $ db = \DB ::getMongoDB ();
78+ $ statementsCollection = new MongoCollection ($ db , 'statements ' );
79+
80+ $ statementsCollection ->deleteIndex ('stored ' );
81+ $ statementsCollection ->deleteIndex (['lrs_id ' => 1 , 'stored ' => -1 ]);
82+
83+ $ statementsCollection ->update ([], ['$unset ' => ["stored " => "" ]], ['multiple ' => true ]);
84+
85+ $ statementsCursor = $ statementsCollection ->find ();
86+ $ remaining = $ statementsCursor ->count ();
87+ print ($ remaining . ' statements total ' . PHP_EOL );
88+
89+ $ maxBatchSize = 10000 ;
90+
91+ while ($ statementsCursor ->hasNext ()) {
92+ $ batch = new MongoUpdateBatch ($ statementsCollection );
93+ $ batchSize = 0 ;
94+ $ shouldExecute = false ;
95+
96+ while ($ batchSize < $ maxBatchSize && $ statementsCursor ->hasNext ()) {
97+ $ batchSize ++;
98+ $ statement = $ statementsCursor ->next ();
99+
100+ if (isset ($ statement ['refs ' ])) {
101+ $ query = [
102+ 'q ' => ['_id ' => $ statement ['_id ' ]],
103+ 'u ' => ['$set ' => []],
104+ 'multi ' => false ,
105+ 'upsert ' => false ,
106+ ];
107+ foreach ($ statement ['refs ' ] as $ key => $ refStatement ) {
108+ if (isset ($ refStatement ['timestamp ' ]) && $ refStatement ['timestamp ' ] instanceof MongoDate ) {
109+ $ query ['u ' ]['$set ' ]['refs. ' .$ key .'.timestamp ' ] = date ('Y-m-d\TH:i:s.uP ' , $ refStatement ['timestamp ' ]->sec );
110+ }
111+ if (isset ($ refStatement ['stored ' ]) && $ refStatement ['stored ' ] instanceof MongoDate ) {
112+ $ query ['u ' ]['$set ' ]['refs. ' .$ key .'.stored ' ] = date ('Y-m-d\TH:i:s.uP ' , $ refStatement ['stored ' ]->sec );
113+ }
114+ }
115+
116+ if (!empty ($ query ['u ' ]['$set ' ])) {
117+ $ batch ->add ((object ) $ query );
118+ $ shouldExecute = true ;
119+ }
120+ }
121+ }
122+
123+ if ($ shouldExecute ) $ batch ->execute ();
124+ $ remaining -= $ batchSize ;
125+
126+ print ($ remaining . ' remaining ' . PHP_EOL );
127+ }
128+ }
129+
130+ }
0 commit comments