Merge branch 'master' of feistymeow.org:feisty_meow
[feisty_meow.git] / production / example_apps / zippy_maps / config / Migrations / 20170712142151_TransmogrifyLatLongDataInLocations.php
1 <?php
2 use Migrations\AbstractMigration;
3 use Cake\Log\Log;
4 use Cake\ORM\TableRegistry;
5
6 /**
7  * this is an unusual migration since we want it to turn old data in latlong column into
8  * new data in two columns, lat and long.
9  */
10 class TransmogrifyLatLongDataInLocations extends AbstractMigration {
11         /**
12          * Change Method.
13          *
14          * More information on this method is available here:
15          * http://docs.phinx.org/en/latest/migrations.html#the-change-method
16          *
17          * @return void
18          */
19         // public function change()
20         // {
21         // }
22         
23         /**
24          * handle migrating the single column into two.
25          * new columns must already exist.
26          * old latlong column could be blank if we're doing a new db, so we have to handle that.
27          */
28         public function up() {
29                 $locations = TableRegistry::get ( 'Locations' );
30                 
31                 $query = $locations->find ();
32                 
33                 // iterate across table on latlong.
34                 foreach ( $query as $locat ) {
35                         Log::write ( 'debug', 'id=' . var_export ( $locat->id, true ) . ' L&L=' . var_export ( $locat->latlong, true ) );
36                         // do nothing if the field is empty.
37                         if ($locat->latlong) {
38                                 // break out the two pieces.
39                                 $latlongExploded = explode ( ',', $locat->latlong );
40                                 Log::write ( 'debug', 'broke into lat=' . $latlongExploded [0] . ' lng=' . $latlongExploded [1] );
41                                 
42                                 // now update the row where we got this info with same lat and long values in two columns.
43                                 $loc_update = $locations->get ( $locat->id );
44                                 $loc_update->lat = floatval ( $latlongExploded [0] );
45                                 $loc_update->lng = floatval ( $latlongExploded [1] );
46                                 $locations->save ( $loc_update );
47                         }
48                 }
49         }
50         
51         /**
52          * performs the reverse case of migrating the two columns back into one.
53          * if we don't do this, we can't slide up and down the migration and rollback scale properly while keeping our lat and long data.
54          */
55         public function down() {
56                 $locations = TableRegistry::get ( 'Locations' );
57                 
58                 $query = $locations->find ();
59                 
60                 // iterate across table on lat and long values.
61                 foreach ( $query as $locat ) {
62                         Log::write ( 'debug', 'id=' . var_export ( $locat->id, true ) . ' lat=' . var_export ( $locat->lat, true )
63                                         .' lng=' . var_export ( $locat->lng, true ));
64                         // do nothing if the fields are empty.
65                         if ($locat->lat && $locat->lng) {
66                                 // combine the two pieces.
67                                 $latlong = '' . $locat->lat . ',' . $locat->lng;                                
68                                 Log::write ( 'debug', 'combo is=' . $latlong);
69                                 
70                                 // now update the row where we got these values to reproduce the single column.
71                                 $loc_update = $locations->get ( $locat->id );
72                                 $loc_update->latlong = $latlong;
73                                 $locations->save ( $loc_update );
74                         }
75                 }
76         }
77 }