@@ -61,36 +61,36 @@ class MigrateInstrumentaionCommand extends Command
61
61
// 'account_number' => 'account_number',
62
62
// ],
63
63
// ],
64
- 'bells ' => [
65
- 'newTableName ' => 'bells ' ,
66
- 'columns ' => [
67
- 'bell_id ' => 'id ' ,
68
- 'bell_description ' => 'description ' ,
69
- 'bell_topic ' => 'topic ' ,
70
- 'bell_message ' => 'message ' ,
71
- 'bell_enabled ' => 'enabled ' ,
72
- ],
73
- ],
74
- 'door_bells ' => [
75
- 'newTableName ' => 'door_bell ' ,
76
- 'columns ' => [
77
- 'door_id ' => 'door_id ' ,
78
- 'bell_id ' => 'bell_id ' ,
79
- ],
80
- ],
81
- 'doors ' => [
82
- 'newTableName ' => 'doors ' ,
83
- 'columns ' => [
84
- 'door_id ' => 'id ' ,
85
- 'door_description ' => 'description ' ,
86
- 'door_short_name ' => 'short_name ' ,
87
- 'door_state ' => 'state ' ,
88
- 'door_state_change ' => 'state_change ' ,
89
- 'permission_code ' => 'permission_code ' ,
90
- 'side_a_zone_id ' => 'side_a_zone_id ' ,
91
- 'side_b_zone_id ' => 'side_b_zone_id ' ,
92
- ],
93
- ],
64
+ // 'bells' => [
65
+ // 'newTableName' => 'bells',
66
+ // 'columns' => [
67
+ // 'bell_id' => 'id',
68
+ // 'bell_description' => 'description',
69
+ // 'bell_topic' => 'topic',
70
+ // 'bell_message' => 'message',
71
+ // 'bell_enabled' => 'enabled',
72
+ // ],
73
+ // ],
74
+ // 'door_bells' => [
75
+ // 'newTableName' => 'door_bell',
76
+ // 'columns' => [
77
+ // 'door_id' => 'door_id',
78
+ // 'bell_id' => 'bell_id',
79
+ // ],
80
+ // ],
81
+ // 'doors' => [
82
+ // 'newTableName' => 'doors',
83
+ // 'columns' => [
84
+ // 'door_id' => 'id',
85
+ // 'door_description' => 'description',
86
+ // 'door_short_name' => 'short_name',
87
+ // 'door_state' => 'state',
88
+ // 'door_state_change' => 'state_change',
89
+ // // 'permission_code' => 'permission_code',
90
+ // 'side_a_zone_id' => 'side_a_zone_id',
91
+ // 'side_b_zone_id' => 'side_b_zone_id',
92
+ // ],
93
+ // ],
94
94
'emails ' => [
95
95
'newTableName ' => 'snackspace_emails ' ,
96
96
'columns ' => [
@@ -255,22 +255,22 @@ class MigrateInstrumentaionCommand extends Command
255
255
'vmc_address ' => 'address ' ,
256
256
],
257
257
],
258
- 'vmc_ref ' => [
259
- 'newTableName ' => 'vending_locations ' ,
260
- 'columns ' => [
261
- 'vmc_ref_id ' => 'id ' ,
262
- 'vmc_id ' => 'vending_machine_id ' ,
263
- 'loc_encoded ' => 'encoding ' ,
264
- 'loc_name ' => 'name ' ,
265
- ],
266
- ],
267
- 'vmc_state ' => [
268
- 'newTableName ' => 'product_vending_location ' ,
269
- 'columns ' => [
270
- 'vmc_ref_id ' => 'vending_location_id ' ,
271
- 'product_id ' => 'product_id ' ,
272
- ],
273
- ],
258
+ // 'vmc_ref' => [
259
+ // 'newTableName' => 'vending_locations',
260
+ // 'columns' => [
261
+ // 'vmc_ref_id' => 'id',
262
+ // 'vmc_id' => 'vending_machine_id',
263
+ // 'loc_encoded' => 'encoding',
264
+ // 'loc_name' => 'name',
265
+ // ],
266
+ // ],
267
+ // 'vmc_state' => [
268
+ // 'newTableName' => 'product_vending_location',
269
+ // 'columns' => [
270
+ // 'vmc_ref_id' => 'vending_location_id',
271
+ // 'product_id' => 'product_id',
272
+ // ],
273
+ // ],
274
274
'transactions ' => [
275
275
'newTableName ' => 'transactions ' ,
276
276
'columns ' => [
@@ -347,15 +347,15 @@ class MigrateInstrumentaionCommand extends Command
347
347
'time_entered ' => 'time_entered ' ,
348
348
],
349
349
],
350
- 'zones ' => [
351
- 'newTableName ' => 'zones ' ,
352
- 'columns ' => [
353
- 'zone_id ' => 'id ' ,
354
- 'zone_description ' => 'description ' ,
355
- 'zone_short_name ' => 'short_name ' ,
356
- 'permission_code ' => 'permission_code ' ,
357
- ],
358
- ],
350
+ // 'zones' => [
351
+ // 'newTableName' => 'zones',
352
+ // 'columns' => [
353
+ // 'zone_id' => 'id',
354
+ // 'zone_description' => 'description',
355
+ // 'zone_short_name' => 'short_name',
356
+ // 'permission_code' => 'permission_code',
357
+ // ],
358
+ // ],
359
359
];
360
360
361
361
/**
@@ -377,6 +377,16 @@ class MigrateInstrumentaionCommand extends Command
377
377
*/
378
378
protected $ description = 'Migrate data form old instrumentation database ' ;
379
379
380
+ /**
381
+ * @var null|\DateTimeZone
382
+ */
383
+ private static $ utc = null ;
384
+
385
+ /**
386
+ * @var null|\DateTimeZone
387
+ */
388
+ private static $ gmt = null ;
389
+
380
390
/**
381
391
* Create a new command instance.
382
392
*
@@ -397,11 +407,18 @@ public function __construct(ToolManager $toolManager)
397
407
*/
398
408
public function handle ()
399
409
{
400
- // TODO get from user
401
- $ instrumentationHost = '127.0.0.1 ' ;
402
- $ instrumentationDatabase = 'instrumentation ' ;
403
- $ instrumentationUser = 'hms ' ;
404
- $ instrumentationPassword = 'secret ' ;
410
+ if (is_null (self ::$ utc )) {
411
+ self ::$ utc = new \DateTimeZone ('UTC ' );
412
+ }
413
+ if (is_null (self ::$ gmt )) {
414
+ self ::$ gmt = new \DateTimeZone ('Europe/London ' );
415
+ }
416
+
417
+ $ this ->info ('Please provide details for connecting to the instrumentation database. ' );
418
+ $ instrumentationHost = $ this ->anticipate ('Host? [127.0.0.1] ' , ['127.0.0.1 ' ]) ?: '127.0.0.1 ' ;
419
+ $ instrumentationDatabase = $ this ->anticipate ('Database name? [instrumentation] ' , ['instrumentation ' ]) ?: 'instrumentation ' ;
420
+ $ instrumentationUser = $ this ->anticipate ('User? [hms] ' , ['hms ' ]) ?: 'hms ' ;
421
+ $ instrumentationPassword = $ this ->secret ('Password? [secret] ' ) ?: 'secret ' ;
405
422
406
423
$ start = Carbon::now ();
407
424
config (['database.connections.instrumentation ' => [
@@ -432,6 +449,7 @@ public function handle()
432
449
$ this ->migrateTools ();
433
450
$ this ->migrateMemberTools ();
434
451
$ this ->migrateHmsEmails ();
452
+ $ this ->migrateVeningLocations ();
435
453
436
454
DB ::statement ('SET FOREIGN_KEY_CHECKS=1; ' );
437
455
$ this ->info ('Total run took ' . $ start ->diff (Carbon::now ())->format ('%H:%i:%s ' ));
@@ -474,6 +492,16 @@ protected function migrateStatusUpdates()
474
492
$ roleUpdates = [];
475
493
476
494
foreach ($ oldData as $ row ) {
495
+ // convert timestamp column from Europe/London to UTC
496
+ $ converted = Carbon::createFromFormat (
497
+ 'Y-m-d H:i:s ' ,
498
+ $ row ->timestamp ,
499
+ self ::$ gmt
500
+ );
501
+
502
+ $ converted ->setTimezone (self ::$ utc );
503
+ $ row ->timestamp = $ converted ->toDateTimeString ();
504
+
477
505
// deal with old status first
478
506
if ($ row ->old_status > 2 ) {
479
507
$ roleUpdates [] = [
@@ -559,9 +587,10 @@ protected function migrateMembers()
559
587
'lastname ' => $ row ->surname ,
560
588
'username ' => $ row ->username ,
561
589
'email ' => $ row ->email ,
562
- 'created_at ' => $ row ->join_date != '0000-00-00 ' ? $ row ->join_date : Carbon::now (),
563
- 'updated_at ' => Carbon::now (),
590
+ 'created_at ' => $ row ->join_date != '0000-00-00 ' ? $ row ->join_date : Carbon::now (self :: $ utc ),
591
+ 'updated_at ' => Carbon::now (self :: $ utc ),
564
592
'account_id ' => $ row ->account_id ,
593
+ 'email_verified_at ' => Carbon::now (self ::$ utc ),
565
594
];
566
595
$ profile = [
567
596
'user_id ' => $ row ->member_id ,
@@ -573,8 +602,8 @@ protected function migrateMembers()
573
602
'address_city ' => $ row ->address_city ,
574
603
'address_postcode ' => $ row ->address_postcode ,
575
604
'contact_number ' => $ row ->contact_number ,
576
- 'created_at ' => $ row ->join_date != '0000-00-00 ' ? $ row ->join_date : Carbon::now (),
577
- 'updated_at ' => Carbon::now (),
605
+ 'created_at ' => $ row ->join_date != '0000-00-00 ' ? $ row ->join_date : Carbon::now (self :: $ utc ),
606
+ 'updated_at ' => Carbon::now (self :: $ utc ),
578
607
'balance ' => $ row ->balance ,
579
608
];
580
609
@@ -736,6 +765,16 @@ protected function migrateMemberTools()
736
765
$ roleUsers = [];
737
766
$ roleUpdates = [];
738
767
foreach ($ oldData as $ row ) {
768
+ // convert mt_date_inducted column from Europe/London to UTC
769
+ $ converted = Carbon::createFromFormat (
770
+ 'Y-m-d H:i:s ' ,
771
+ $ row ->mt_date_inducted ,
772
+ self ::$ gmt
773
+ );
774
+
775
+ $ converted ->setTimezone (self ::$ utc );
776
+ $ row ->mt_date_inducted = $ converted ->toDateTimeString ();
777
+
739
778
switch ($ row ->mt_access_level ) {
740
779
case 'MAINTAINER ' :
741
780
$ roleUsers [] = [
@@ -816,6 +855,16 @@ protected function migrateHmsEmails()
816
855
});
817
856
818
857
$ newData = $ filteredData ->map (function ($ row , $ key ) use ($ columns , $ newTableName , &$ emailUser ) {
858
+ // convert timestamp column from Europe/London to UTC
859
+ $ converted = Carbon::createFromFormat (
860
+ 'Y-m-d H:i:s ' ,
861
+ $ row ->timestamp ,
862
+ self ::$ gmt
863
+ );
864
+
865
+ $ converted ->setTimezone (self ::$ utc );
866
+ $ row ->timestamp = $ converted ->toDateTimeString ();
867
+
819
868
$ newRow = [];
820
869
foreach ($ columns as $ oldName => $ newName ) {
821
870
$ newRow [$ newName ] = $ row ->$ oldName ;
@@ -855,6 +904,45 @@ protected function migrateHmsEmails()
855
904
$ this ->info ($ startTime ->diff (Carbon::now ())->format ('took: %H:%i:%s ' ));
856
905
}
857
906
907
+ protected function migrateVeningLocations ()
908
+ {
909
+ $ oldTableName = 'vmc_ref ' ;
910
+ $ newTableName = 'vending_locations ' ;
911
+ $ columns = [
912
+ 'vmc_ref_id ' => 'id ' ,
913
+ 'vmc_id ' => 'vending_machine_id ' ,
914
+ 'loc_encoded ' => 'encoding ' ,
915
+ 'loc_name ' => 'name ' ,
916
+ ];
917
+
918
+ $ this ->info ('Migrating ' . $ oldTableName );
919
+ $ startTime = Carbon::now ();
920
+ $ oldData = DB ::connection ('instrumentation ' )->table ($ oldTableName )->get ();
921
+
922
+ $ newData = $ oldData ->map (function ($ row , $ key ) use ($ columns , $ newTableName ) {
923
+ $ newRow = [];
924
+ foreach ($ columns as $ oldName => $ newName ) {
925
+ $ newRow [$ newName ] = $ row ->$ oldName ;
926
+ }
927
+
928
+ // From old state for find product_id for $newRow['id']
929
+ $ state = DB ::connection ('instrumentation ' )->table ('vmc_state ' )->where ('vmc_ref_id ' , $ row ->vmc_ref_id )->first ();
930
+
931
+ $ newRow ['product_id ' ] = $ state ->product_id ;
932
+
933
+ return $ newRow ;
934
+ });
935
+
936
+ DB ::statement ("TRUNCATE TABLE $ newTableName; " );
937
+
938
+ $ dataChunks = array_chunk ($ newData ->toArray (), 1000 , true );
939
+ foreach ($ dataChunks as $ dataChunk ) {
940
+ DB ::table ($ newTableName )->insert ($ dataChunk );
941
+ }
942
+
943
+ $ this ->info ($ startTime ->diff (Carbon::now ())->format ('took: %H:%i:%s ' ));
944
+ }
945
+
858
946
protected function migrateSimpleMappings ()
859
947
{
860
948
$ simpleStart = Carbon::now ();
@@ -872,7 +960,12 @@ protected function simpleMigration($oldTableName, $mapping)
872
960
$ startTime = Carbon::now ();
873
961
$ oldData = DB ::connection ('instrumentation ' )->table ($ oldTableName )->get ();
874
962
875
- $ newData = $ oldData ->map (function ($ row , $ key ) use ($ mapping , $ newTableName ) {
963
+ $ column_types = [];
964
+ foreach ($ mapping ['columns ' ] as $ oldName => $ newName ) {
965
+ $ column_types [$ oldName ] = DB ::connection ('instrumentation ' )->getSchemaBuilder ()->getColumnType ($ oldTableName , $ oldName );
966
+ }
967
+
968
+ $ newData = $ oldData ->map (function ($ row , $ key ) use ($ mapping , $ newTableName , $ column_types ) {
876
969
$ newRow = [];
877
970
foreach ($ mapping ['columns ' ] as $ oldName => $ newName ) {
878
971
if (array_key_exists ($ oldName , $ row )) {
@@ -883,11 +976,25 @@ protected function simpleMigration($oldTableName, $mapping)
883
976
$ newRow [$ newName ] *= 100 ;
884
977
}
885
978
886
- // zone id's need to be increase by one
887
- if (strpos ($ newName , 'zone_id ' ) !== false ) {
888
- $ newRow [$ newName ] += 1 ;
889
- } elseif ($ newTableName == 'zones ' && $ newName == 'id ' ) {
890
- $ newRow [$ newName ] += 1 ;
979
+ if ($ column_types [$ oldName ] == 'datetime ' ) {
980
+ // convert datatime columns from Europe/London to UTC
981
+ if (is_null ($ newRow [$ newName ])) {
982
+ // skipp null
983
+ } elseif (strlen ($ newRow [$ newName ]) == 10 ) {
984
+ // skip date only fields
985
+ } elseif (strlen ($ newRow [$ newName ]) == 19 ) {
986
+ // ah date and time
987
+ $ converted = Carbon::createFromFormat (
988
+ 'Y-m-d H:i:s ' ,
989
+ $ newRow [$ newName ],
990
+ self ::$ gmt
991
+ );
992
+ $ converted ->setTimezone (self ::$ utc );
993
+ $ newRow [$ newName ] = $ converted ->toDateTimeString ();
994
+ } else {
995
+ // hmm time only field?
996
+ dd ($ newRow [$ newName ]);
997
+ }
891
998
}
892
999
}
893
1000
}
@@ -897,10 +1004,20 @@ protected function simpleMigration($oldTableName, $mapping)
897
1004
898
1005
DB ::statement ("TRUNCATE TABLE $ newTableName; " );
899
1006
1007
+ if ($ newTableName == 'zones ' ) {
1008
+ $ mode = DB ::select ('select @@sql_mode as sql_mode ' )[0 ];
1009
+ DB ::statement ("SET sql_mode=' $ mode ->sql_mode ,NO_AUTO_VALUE_ON_ZERO' " );
1010
+ }
1011
+
900
1012
$ dataChunks = array_chunk ($ newData ->toArray (), 1000 , true );
901
1013
foreach ($ dataChunks as $ dataChunk ) {
902
1014
DB ::table ($ newTableName )->insert ($ dataChunk );
903
1015
}
1016
+
1017
+ if ($ newTableName == 'zones ' ) {
1018
+ DB ::statement ("SET sql_mode=' $ mode ->sql_mode ' " );
1019
+ }
1020
+
904
1021
$ this ->info ($ startTime ->diff (Carbon::now ())->format ('took: %H:%i:%s ' ));
905
1022
}
906
1023
}
0 commit comments