ViralTiger
10/8/2018 - 11:55 AM

Refactor UUID's to Binary

<?php 

Route::get('/migrate', function () {
    DB::transaction(function () {
        \DB::statement('ALTER TABLE waystation_managements MODIFY COLUMN uuid BINARY(36)');

        $waystationManagements = WaystationManagement::all();

        foreach ($waystationManagements as &$item) {
            if (Uuid::isValid($item->uuid)) {
                $item->uuid_text = $item->uuid;
                $item->save();
            }
        }

        \DB::statement('ALTER TABLE waystation_managements MODIFY COLUMN uuid BINARY(16)');
    });
});

Route::get('/migrate/media', function () {
    $media = Media::all();
    foreach ($media as $item) {
        $item->disk = 'public';
        $item->update();
    }
    return 'All patched';
});

Route::get('/migrate/phase-one', function () {
    /**
     * Update the User Uuid to Binary form.
     * @var [type]
     */
    if (DB::connection()->getDoctrineColumn('users', 'uuid')->getType()->getName() != 'binary') {
        DB::transaction(function () {
            $users = User::with('driver')->get();
            DB::statement('ALTER TABLE users MODIFY COLUMN uuid BINARY(36)');
            foreach ($users as &$user) {
                $newbie =  User::where('email', $user->email)->first();
                $newbie->uuid_text = $user->uuid;
                $newbie->save();
            }
            DB::statement('ALTER TABLE users MODIFY COLUMN uuid BINARY(16)');
        });
    }

    /**
     * Update the Driver Uuid to Binary form.
     * @var [type]
     */
    if (DB::connection()->getDoctrineColumn('drivers', 'userUuid')->getType()->getName() != 'binary') {
        DB::transaction(function () {
            $drivers = Driver::all();
            DB::statement('ALTER TABLE drivers MODIFY COLUMN userUuid BINARY(36)');
            foreach ($drivers as &$driver) {
                $newbie =  Driver::where([['updated_at',  $driver->updated_at],['dob', $driver->dob]])->first();
                $newbie->uuid_text = $driver->userUuid;
                try {
                    $newbie->save();
                } catch (Exception $e) {
                }
            }
            DB::statement('ALTER TABLE drivers MODIFY COLUMN userUuid BINARY(16)');
        });
    }

    /**
     * Update the UserSettins Uuid to Binary form.
     * @var [type]
     */
    if (DB::connection()->getDoctrineColumn('user_settings', 'userUuid')->getType()->getName() != 'binary') {
        DB::transaction(function () {
            $settings = UserSettings::all();
            DB::statement('ALTER TABLE user_settings MODIFY COLUMN userUuid BINARY(36)');
            foreach ($settings as &$setting) {
                $setting->uuid_text =  $setting->userUuid;
                $setting->save();
            }
            DB::statement('ALTER TABLE user_settings MODIFY COLUMN userUuid BINARY(16)');
        });
    }

    /**
     * Update the Addressable Uuid to Binary form.
     * @var [type]
     */
    if (DB::connection()->getDoctrineColumn('addressables', 'addressable_id')->getType()->getName() != 'binary') {
        DB::transaction(function () {
            $addresses = Addressable::all();
            \DB::statement('ALTER TABLE addressables MODIFY COLUMN addressable_id BINARY(36)');
            foreach ($addresses as $address) {
                $address->uuid_text = $address->addressable_id;
                $address->save();
                DB::table('addressables')
              ->update(['addressable_type' => DB::raw("REPLACE(addressable_type, 'Depot', 'Waystation')")]);
            }
            \DB::statement('ALTER TABLE addressables MODIFY COLUMN addressable_id BINARY(16)');
        });
    }

    /**
     * Update UUID's that are not primary keys.
     * @var [type]
     */
    if (DB::connection()->getDoctrineColumn('disposals', 'waystationUuid')->getType()->getName() != 'binary') {
        DB::transaction(function () {
            \DB::statement('ALTER TABLE disposals MODIFY COLUMN waystationUuid BINARY(36)');
            $disposals = Disposal::all();
            foreach ($disposals as $disposal) {
                $uuid = Uuid::fromString($disposal->waystationUuid);
                $disposal->waystationUuid = $uuid->getBytes();
                $disposal->save();
            }
            \DB::statement('ALTER TABLE disposals MODIFY COLUMN waystationUuid BINARY(16)');
        });
    }


    /**
     * Update BankAccounts UUID's that are not primary keys.
     * @var [type]
     */
    if (DB::connection()->getDoctrineColumn('driver_bank_accounts', 'uuid')->getType()->getName() != 'binary') {
        DB::transaction(function () {
            \DB::statement('ALTER TABLE driver_bank_accounts MODIFY COLUMN uuid BINARY(36)');
            \DB::statement('ALTER TABLE driver_bank_accounts MODIFY COLUMN userUuid BINARY(36)');

            $bankAccounts = BankAccount::all();
            foreach ($bankAccounts as $account) {
                $uuid = Uuid::fromString($account->uuid);
                $uuidUser = Uuid::fromString($account->userUuid);

                $account->uuid = $uuid->getBytes();
                $account->userUuid = $uuidUser->getBytes();
                $account->save();
            }
            \DB::statement('ALTER TABLE driver_bank_accounts MODIFY COLUMN uuid BINARY(16)');
            \DB::statement('ALTER TABLE driver_bank_accounts MODIFY COLUMN userUuid BINARY(16)');
        });
    }

    /**
     * Update JobContent UUID's that are not primary keys.
     * @var [type]
     */
    if (DB::connection()->getDoctrineColumn('drivers', 'currentLicenseUuid')->getType()->getName() != 'binary') {
        DB::transaction(function () {
            \DB::statement('ALTER TABLE drivers MODIFY COLUMN currentLicenseUuid BINARY(36)');

            $drivers = Driver::all();
            foreach ($drivers as $driver) {
                if (Uuid::isValid($driver->currentLicenseUuid)) {
                    $uuid = Uuid::fromString($driver->currentLicenseUuid);
                    $driver->currentLicenseUuid = $uuid->getBytes();
                    $driver->save();
                }
            }
            \DB::statement('ALTER TABLE drivers MODIFY COLUMN currentLicenseUuid BINARY(16)');
        });
    }

    /**
     * Update JobContent UUID's that are not primary keys.
     * @var [type]
     */
    if (DB::connection()->getDoctrineColumn('job_content_items', 'jobUuid')->getType()->getName() != 'binary') {
        DB::transaction(function () {
            \DB::statement('ALTER TABLE job_content_items MODIFY COLUMN jobUuid BINARY(36)');

            $job_content_items = JobContentItem::all();
            foreach ($job_content_items as $item) {
                if (!is_null($item->jobUuid) || $item->jobUuid != '') {
                    $uuid = Uuid::fromString($item->jobUuid);
                    $item->jobUuid = $uuid->getBytes();
                    $item->save();
                }
            }
            \DB::statement('ALTER TABLE job_content_items MODIFY COLUMN jobUuid BINARY(16)');
        });
    }

    /**
     * Update Jobevent UUID's that are not primary keys.
     * @var [type]
     */
    if (DB::connection()->getDoctrineColumn('job_events', 'jobUuid')->getType()->getName() != 'binary') {
        DB::transaction(function () {
            \DB::statement('ALTER TABLE job_events MODIFY COLUMN jobUuid BINARY(36)');
            \DB::statement('ALTER TABLE job_events MODIFY COLUMN messageUuid BINARY(36)');

            $jobEvents = JobEvent::all();
            foreach ($jobEvents as &$event) {
                if (Uuid::isValid($event->jobUuid)) {
                    $uuid = Uuid::fromString($event->jobUuid);
                    $event->jobUuid = $uuid->getBytes();
                    $event->save();

                    if (Uuid::isValid($event->messageUuid)) {
                        $uuid2 = Uuid::fromString($event->messageUuid);
                        $event->messageUuid = $uuid2->getBytes();
                        $event->save();
                    }
                }
            }
            \DB::statement('ALTER TABLE job_events MODIFY COLUMN jobUuid BINARY(16)');
            \DB::statement('ALTER TABLE job_events MODIFY COLUMN messageUuid BINARY(16)');
        });
    }
});

Route::get('/migrate/phase-two', function () {

    /**
     * Update Job UUID's includes Primary key change.
     * @var [type]
     */
    if (DB::connection()->getDoctrineColumn('jobs', 'uuid')->getType()->getName() != 'binary') {
        DB::transaction(function () {
            \DB::statement('ALTER TABLE jobs MODIFY COLUMN uuid BINARY(36)');
            \DB::statement('ALTER TABLE jobs MODIFY COLUMN consumerUuid BINARY(36)');
            \DB::statement('ALTER TABLE jobs MODIFY COLUMN driverUuid BINARY(36)');

            $jobs = Job::all();

            foreach ($jobs as &$job) {
                if (Uuid::isValid($job->uuid)) {
                    $job->uuid_text = $job->uuid;
                    $uuid = Uuid::fromString($job->consumerUuid);
                    $job->consumerUuid = $uuid->getBytes();

                    if (!is_null($job->driverUuid)) {
                        $uuid2 = Uuid::fromString($job->driverUuid);
                        $job->driverUuid = $uuid2->getBytes();
                    }
                    $job->save();
                }
            }

            \DB::statement('ALTER TABLE jobs MODIFY COLUMN uuid BINARY(16)');
            \DB::statement('ALTER TABLE jobs MODIFY COLUMN consumerUuid BINARY(16)');
            \DB::statement('ALTER TABLE jobs MODIFY COLUMN driverUuid BINARY(16)');
        });
    }

    /**
     * Update Reviews UUID's includes no primary key change.
     * @var [type]
     */
    if (DB::connection()->getDoctrineColumn('reviews', 'user_uuid')->getType()->getName() != 'binary') {
        DB::transaction(function () {
            \DB::statement('ALTER TABLE reviews MODIFY COLUMN user_uuid BINARY(36)');
            \DB::statement('ALTER TABLE reviews MODIFY COLUMN reviewable_id BINARY(36)');
            \DB::statement('ALTER TABLE reviews MODIFY COLUMN contextable_id BINARY(36)');
            $reviews = Review::all();
            Review::truncate();
            \DB::statement('ALTER TABLE reviews MODIFY COLUMN user_uuid BINARY(16)');
            \DB::statement('ALTER TABLE reviews MODIFY COLUMN reviewable_id BINARY(16)');
            \DB::statement('ALTER TABLE reviews MODIFY COLUMN contextable_id BINARY(16)');
        });
    }

    /**
     * Update License UUID's includes Primary key change.
     * @var [type]
     */
    if (DB::connection()->getDoctrineColumn('licenses', 'uuid')->getType()->getName() != 'binary') {
        DB::transaction(function () {
            \DB::statement('ALTER TABLE licenses MODIFY COLUMN uuid BINARY(36)');
            \DB::statement('ALTER TABLE licenses MODIFY COLUMN userUuid BINARY(36)');

            $licenses = License::all();

            foreach ($licenses as &$license) {
                if (Uuid::isValid($license->uuid)) {
                    $license->uuid = $license->uuid;
                    $uuid2 = Uuid::fromString($license->userUuid);
                    $license->userUuid = $uuid2->getBytes();
                    $license->save();
                }
            }

            \DB::statement('ALTER TABLE licenses MODIFY COLUMN uuid BINARY(16)');
            \DB::statement('ALTER TABLE licenses MODIFY COLUMN userUuid BINARY(16)');
        });
    }

    /**
     * Update Vehicles UUID's includes Primary key change.
     * @var [type]
     */
    if (DB::connection()->getDoctrineColumn('vehicles', 'uuid')->getType()->getName() != 'binary') {
        DB::transaction(function () {
            \DB::statement('ALTER TABLE vehicles MODIFY COLUMN uuid BINARY(36)');
            \DB::statement('ALTER TABLE vehicles MODIFY COLUMN userUuid BINARY(36)');

            $vehicles = Vehicle::all();

            foreach ($vehicles as &$vehicle) {
                if (Uuid::isValid($vehicle->uuid)) {
                    $vehicle->uuid_text = $vehicle->uuid;
                    $uuid2 = Uuid::fromString($vehicle->userUuid);
                    $vehicle->userUuid = $uuid2->getBytes();
                    $vehicle->save();
                }
            }

            \DB::statement('ALTER TABLE vehicles MODIFY COLUMN uuid BINARY(16)');
            \DB::statement('ALTER TABLE vehicles MODIFY COLUMN userUuid BINARY(16)');
        });
    }

    /**
     * Update ShopGuarantee UUID's includes Primary key change.
     * @var [type]
     */
    if (DB::connection()->getDoctrineColumn('shopguarantees', 'uuid')->getType()->getName() != 'binary') {
        DB::transaction(function () {
            \DB::statement('ALTER TABLE shopguarantees MODIFY COLUMN uuid BINARY(36)');
            \DB::statement('ALTER TABLE shopguarantees MODIFY COLUMN jobUuid BINARY(36)');

            $shopguarantees = ShopGuarantee::all();

            foreach ($shopguarantees as &$item) {
                if (Uuid::isValid($item->uuid)) {
                    $item->uuid_text = $item->uuid;
                    $uuid2 = Uuid::fromString($item->jobUuid);
                    $item->jobUuid = $uuid2->getBytes();
                    $item->save();
                }
            }

            \DB::statement('ALTER TABLE shopguarantees MODIFY COLUMN uuid BINARY(16)');
            \DB::statement('ALTER TABLE shopguarantees MODIFY COLUMN jobUuid BINARY(16)');
        });
    }

    /**
     * Update Message UUID's includes Primary key change.
     * @var [type]
     */
    if (DB::connection()->getDoctrineColumn('messages', 'uuid')->getType()->getName() != 'binary') {
        DB::transaction(function () {
            \DB::statement('ALTER TABLE messages MODIFY COLUMN uuid BINARY(36)');
            \DB::statement('ALTER TABLE messages MODIFY COLUMN senderUuid BINARY(36)');
            \DB::statement('ALTER TABLE messages MODIFY COLUMN receiverUuid BINARY(36)');

            $messages = Message::all();

            foreach ($messages as &$message) {
                if (Uuid::isValid($message->uuid)) {
                    $message->uuid_text = $message->uuid;
                    $uuid2 = Uuid::fromString($message->senderUuid);
                    $message->senderUuid = $uuid2->getBytes();
                    $uuid2 = Uuid::fromString($message->receiverUuid);
                    $message->receiverUuid = $uuid2->getBytes();
                    $message->save();
                }
            }

            \DB::statement('ALTER TABLE messages MODIFY COLUMN uuid BINARY(16)');
            \DB::statement('ALTER TABLE messages MODIFY COLUMN senderUuid BINARY(16)');
            \DB::statement('ALTER TABLE messages MODIFY COLUMN receiverUuid BINARY(16)');
        });
    }

    /**
     * Update UserDevice UUID's includes Primary key change.
     * @var [type]
     */
    if (DB::connection()->getDoctrineColumn('user_devices', 'userUuid')->getType()->getName() != 'binary') {
        DB::transaction(function () {
            \DB::statement('ALTER TABLE user_devices MODIFY COLUMN userUuid BINARY(36)');

            $userDevices = UserDevice::all();

            foreach ($userDevices as &$device) {
                if (Uuid::isValid($device->userUuid)) {
                    $device->userUuid = Uuid::fromString($device->userUuid)->getBytes();
                    $device->save();
                }
            }

            \DB::statement('ALTER TABLE user_devices MODIFY COLUMN userUuid BINARY(16)');
        });
    }

    /**
     * Update OpeningHours UUID's includes Primary key change.
     * @var [type]
     */
    // if (DB::connection()->getDoctrineColumn('opening_hours', 'openable_id')->getType()->getName() != 'binary') {
    DB::transaction(function () {
        \DB::statement('ALTER TABLE opening_hours MODIFY COLUMN openable_id BINARY(36)');

        $openingHours = OpeningHours::all();

        foreach ($openingHours as &$hour) {
            if (Uuid::isValid($hour->openable_id)) {
                $uuid = Uuid::fromString($hour->openable_id);
                $hour->openable_id = $uuid->getBytes();
                if ($hour->openable_type = 'App\Depot\Depot') {
                    $hour->openable_type = 'App\Waystation\Waystation';
                }
                $hour->save();
            }
        }

        \DB::statement('ALTER TABLE opening_hours MODIFY COLUMN openable_id BINARY(16)');
    });
    // }

    /**
     * Update Transaction UUID's includes Primary key change.
     * @var [type]
     */
    DB::getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');

    if (DB::connection()->getDoctrineColumn('transactions', 'payable_id')->getType()->getName() != 'binary') {
        DB::transaction(function () {
            $transactions = Transaction::all();

            \DB::statement('ALTER TABLE transactions MODIFY COLUMN uuid BINARY(36)');
            \DB::statement('ALTER TABLE transactions MODIFY COLUMN payable_id BINARY(36)');
            \DB::statement('ALTER TABLE transactions MODIFY COLUMN causable_id BINARY(36)');


            foreach ($transactions as &$transaction) {
                if (Uuid::isValid($transaction->uuid)) {
                    $transaction->uuid_text = $transaction->uuid;

                    $uuid2 = Uuid::fromString($transaction->payable_id);
                    $transaction->payable_id = $uuid2->getBytes();

                    $uuid3 = Uuid::fromString($transaction->causable_id);
                    $transaction->causable_id = $uuid3->getBytes();

                    $transaction->save();
                }
            }

            \DB::statement('ALTER TABLE transactions MODIFY COLUMN uuid BINARY(16)');
            \DB::statement('ALTER TABLE transactions MODIFY COLUMN payable_id BINARY(16)');
            \DB::statement('ALTER TABLE transactions MODIFY COLUMN causable_id BINARY(16)');
        });
    }
});

  Route::get('/migrate/phase-three', function () {

        # Waystations, Media, WaystationManagements, Vehicle Trailers, ModelRoles

      /**
       * Update Waystations UUID's includes Primary key change.
       * @var [type]
       */
      // if (DB::connection()->getDoctrineColumn('waystations', 'uuid')->getType()->getName() != 'binary') {
      // DB::transaction(function () {
      \DB::statement('ALTER TABLE waystations MODIFY COLUMN uuid BINARY(36)');
      \DB::statement('ALTER TABLE waystations MODIFY COLUMN managementUuid BINARY(36)');

      $waystations = Waystation::all();

      foreach ($waystations as &$waystation) {
          if (Uuid::isValid($waystation->uuid)) {
              $waystation->uuid_text = $waystation->uuid;
              if (Uuid::isValid($waystation->managementUuid)) {
                  $uuid2 = Uuid::fromString($waystation->managementUuid);
                  $waystation->managementUuid = $uuid2->getBytes();
              }

              $waystation->save();
          }
      }

      \DB::statement('ALTER TABLE waystations MODIFY COLUMN uuid BINARY(16)');
      \DB::statement('ALTER TABLE waystations MODIFY COLUMN managementUuid BINARY(16)');
      // });
      // }

      /**
       * Update Spatie Media Model UUID's that are not primary key changes.
       * @var [type]
       */
      if (DB::connection()->getDoctrineColumn('media', 'model_id')->getType()->getName() != 'binary') {
          DB::transaction(function () {
              \DB::statement('ALTER TABLE media MODIFY COLUMN model_id BINARY(36)');

              $mediaItems = Media::all();

              foreach ($mediaItems as &$item) {
                  if (Uuid::isValid($item->model_id)) {
                      $uuid = Uuid::fromString($item->model_id);
                      $item->model_id = $uuid->getBytes();
                      $item->save();
                  }
              }

              \DB::statement('ALTER TABLE media MODIFY COLUMN model_id BINARY(16)');
          });
      }

      /**
       * Update WaystationManagement Model UUID's primary key changes.
       * @var [type]
       */
      if (DB::connection()->getDoctrineColumn('waystation_managements', 'uuid')->getType()->getName() != 'binary') {
          DB::transaction(function () {
              \DB::statement('ALTER TABLE waystation_managements MODIFY COLUMN uuid BINARY(36)');

              $waystationManagements = WaystationManagement::all();

              foreach ($waystationManagements as &$item) {
                  if (Uuid::isValid($item->uuid)) {
                      $item->uuid_text = $item->uuid;
                      $item->save();
                  }
              }

              \DB::statement('ALTER TABLE waystation_managements MODIFY COLUMN uuid BINARY(16)');
          });
      }

      /**
       * Update Vehicle Trailer UUID's that are not primary key changes.
       * @var [type]
       */
      if (DB::connection()->getDoctrineColumn('vehicle_trailer', 'vehicleUuid')->getType()->getName() != 'binary') {
          DB::transaction(function () {
              \DB::statement('ALTER TABLE vehicle_trailer MODIFY COLUMN vehicleUuid BINARY(36)');

              $vehicleTrailers = DB::table('vehicle_trailer')->get();

              foreach ($vehicleTrailers as &$trailer) {
                  $modelId = $trailer->vehicleUuid;

                  $uuid = Uuid::fromString($trailer->vehicleUuid);
                  $trailer->vehicleUuid = $uuid->getBytes();

                  DB::table('vehicle_trailer')
              ->where('vehicleUuid', $modelId)
              ->update(['vehicleUuid' => $trailer->vehicleUuid]);
              }

              \DB::statement('ALTER TABLE vehicle_trailer MODIFY COLUMN vehicleUuid BINARY(16)');
          });
      }

      /**
       * Spatie Model uuid to Binary uuid
       * @var [type]
       */
      if (DB::connection()->getDoctrineColumn('model_has_roles', 'model_id')->getType()->getName() != 'binary') {
          DB::transaction(function () {
              \DB::statement('ALTER TABLE model_has_roles MODIFY COLUMN model_id BINARY(36)');

              $roles = DB::table('model_has_roles')->get();
              foreach ($roles as $role) {
                  $modelId = $role->model_id;

                  $uuid = Uuid::fromString($role->model_id);
                  $role->model_id = $uuid->getBytes();

                  DB::table('model_has_roles')
                  ->where('model_id', $modelId)
                  ->update(['model_id' => $role->model_id]);
              }

              \DB::statement('ALTER TABLE model_has_roles MODIFY COLUMN model_id BINARY(16)');
          });
      }
  });

  Route::get('/migrate/phase-four-part-one', function () {

      /**
       * Oauth Access Tokens Model uuid to Binary uuid
       * @var [type]
       */
      if (DB::connection()->getDoctrineColumn('oauth_access_tokens', 'user_id')->getType()->getName() != 'binary') {
          DB::transaction(function () {
              \DB::statement('ALTER TABLE oauth_access_tokens MODIFY COLUMN user_id BINARY(36)');

              $tokens = DB::table('oauth_access_tokens')->get();

              foreach ($tokens as $token) {
                  $modelId = $token->user_id;

                  $uuid = Uuid::fromString($token->user_id);
                  $token->user_id = $uuid->getBytes();

                  DB::table('oauth_access_tokens')
                  ->where('user_id', $modelId)
                  ->update(['user_id' => $token->user_id]);
              }

              \DB::statement('ALTER TABLE oauth_access_tokens MODIFY COLUMN user_id BINARY(16)');
          });
      }
  });

  Route::get('/migrate/phase-four-part-two', function () {

      /**
       * Waystation Material Model uuid to Binary uuid
       * @var [type]
       */
      // if (DB::connection()->getDoctrineColumn('waystation_material', 'waystation_uuid')->getType()->getName() != 'binary') {
      // DB::transaction(function () {
      // \DB::statement('ALTER TABLE waystation_material MODIFY COLUMN waystation_uuid BINARY(36)');

      $waystationManagements = DB::table('waystation_material')->get();

      foreach ($waystationManagements as $management) {
          if (Uuid::isValid($management->waystation_uuid)) {
              if (stripos($management->waystation_uuid, '-') == 8) {
                  $modelId = $management->waystation_uuid;

                  $uuid = Uuid::fromString($management->waystation_uuid);
                  $management->waystation_uuid = $uuid->getBytes();

                  DB::table('waystation_material')
                        ->where('waystation_uuid', $modelId)
                        ->update(['waystation_uuid' => $management->waystation_uuid]);
              }
          }
      }

      \DB::statement('ALTER TABLE waystation_material MODIFY COLUMN waystation_uuid BINARY(16)');
      // });
      // }
  });

    Route::get('/migrate/phase-five', function () {

      /**
       * To be re-run
       * Waystation Material Type Model uuid to Binary uuid
       * @var [type]
       */
        if (DB::connection()->getDoctrineColumn('waystation_material_type', 'waystation_uuid')->getType()->getName() != 'binary') {
            DB::transaction(function () {
                \DB::statement('ALTER TABLE waystation_material_type MODIFY COLUMN waystation_uuid BINARY(36)');

                $waystationManagements = DB::table('waystation_material_type')->get();

                foreach ($waystationManagements as $management) {
                    $modelId = $management->waystation_uuid;

                    $uuid = Uuid::fromString($management->waystation_uuid);
                    $management->waystation_uuid = $uuid->getBytes();

                    DB::table('waystation_material_type')
                  ->where('waystation_uuid', $modelId)
                  ->update(['waystation_uuid' => $management->waystation_uuid]);
                }

                \DB::statement('ALTER TABLE waystation_material_type MODIFY COLUMN waystation_uuid BINARY(16)');
            });
        }

        /**
         * To be re-run
         * Waystation Type Model uuid to Binary uuid
         * @var [type]
         */
        if (DB::connection()->getDoctrineColumn('waystation_type', 'waystation_uuid')->getType()->getName() != 'binary') {
            DB::transaction(function () {
                \DB::statement('ALTER TABLE waystation_type MODIFY COLUMN waystation_uuid BINARY(36)');

                $waystationTypes = DB::table('waystation_type')->get();

                foreach ($waystationTypes as $type) {
                    $modelId = $type->waystation_uuid;

                    $uuid = Uuid::fromString($type->waystation_uuid);
                    $type->waystation_uuid = $uuid->getBytes();

                    DB::table('waystation_type')
                  ->where('waystation_uuid', $modelId)
                  ->update(['waystation_uuid' => $type->waystation_uuid]);
                }

                \DB::statement('ALTER TABLE waystation_type MODIFY COLUMN waystation_uuid BINARY(16)');
            });
        };
    });

    Route::get('/migrate/phase-six', function () {
        DB::getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');

        /**
         * To be re-run
         * Waystation Zipcode Model uuid to Binary uuid
         * @var [type]
         */
        if (DB::connection()->getDoctrineColumn('waystation_zipcode', 'waystation_uuid')->getType()->getName() != 'binary') {
            DB::transaction(function () {
                \DB::statement('ALTER TABLE waystation_zipcode MODIFY COLUMN waystation_uuid BINARY(36)');

                $waystationZipcodes = DB::table('waystation_zipcode')->get();

                foreach ($waystationZipcodes as $zipcode) {
                    $modelId = $zipcode->waystation_uuid;
                    $uuid = Uuid::fromString($zipcode->waystation_uuid);
                    $zipcode->waystation_uuid = $uuid->getBytes();

                    DB::table('waystation_zipcode')
                  ->where('waystation_uuid', $modelId)
                  ->update(['waystation_uuid' => $zipcode->waystation_uuid]);
                }

                \DB::statement('ALTER TABLE waystation_zipcode MODIFY COLUMN waystation_uuid BINARY(16)');
            });
        }

        /**
         * To be re-run
         * Waystation Zipcode Model uuid to Binary uuid
         * @var [type]
         */
        if (DB::connection()->getDoctrineColumn('oauth_auth_codes', 'user_id')->getType()->getName() != 'binary') {
            DB::transaction(function () {
                \DB::statement('ALTER TABLE oauth_auth_codes MODIFY COLUMN user_id BINARY(36)');

                $authcodes = DB::table('oauth_auth_codes')->get();

                foreach ($authcodes as $code) {
                    $modelId = $code->user_id;
                    $uuid = Uuid::fromString($code->user_id);
                    $code->user_id = $uuid->getBytes();

                    DB::table('oauth_auth_codes')
                  ->where('user_id', $modelId)
                  ->update(['user_id' => $code->user_id]);
                }

                \DB::statement('ALTER TABLE oauth_auth_codes MODIFY COLUMN user_id BINARY(16)');
            });
        }
        /**
         * To be re-run
         * Waystation Zipcode Model uuid to Binary uuid
         * @var [type]
         */
        if (DB::connection()->getDoctrineColumn('waystation_users', 'waystation_uuid')->getType()->getName() != 'binary') {
            DB::transaction(function () {
                \DB::statement('ALTER TABLE waystation_users MODIFY COLUMN waystation_uuid BINARY(36)');
                \DB::statement('ALTER TABLE waystation_users MODIFY COLUMN user_uuid BINARY(36)');

                $records = DB::table('waystation_users')->get();

                foreach ($records as $record) {
                    $modelId = $record->waystation_uuid;
                    $uuid = Uuid::fromString($record->waystation_uuid);
                    $record->waystation_uuid = $uuid->getBytes();
                    $record->user_uuid = Uuid::fromString($record->user_uuid)->getBytes();

                    DB::table('waystation_users')
                  ->where('waystation_uuid', $modelId)
                  ->update([
                    'waystation_uuid' => $record->waystation_uuid,
                    'user_uuid' => $record->user_uuid
                  ]);
                }

                \DB::statement('ALTER TABLE waystation_users MODIFY COLUMN waystation_uuid BINARY(16)');
                \DB::statement('ALTER TABLE waystation_users MODIFY COLUMN user_uuid BINARY(16)');
            });
        }

        if (DB::connection()->getDoctrineColumn('job_waystation_adjustments', 'uuid')->getType()->getName() != 'binary') {
            DB::transaction(function () {
                \DB::statement('ALTER TABLE job_waystation_adjustments CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci');
                \DB::statement('ALTER TABLE job_waystation_adjustments MODIFY COLUMN uuid BINARY(36)');
                \DB::statement('ALTER TABLE job_waystation_adjustments MODIFY COLUMN jobUuid BINARY(36)');
                \DB::statement('ALTER TABLE job_waystation_adjustments MODIFY COLUMN waystationUuid BINARY(36)');
                \DB::statement('ALTER TABLE job_waystation_adjustments MODIFY COLUMN userUuid BINARY(36)');

                $records = DB::table('job_waystation_adjustments')->get();

                foreach ($records as $record) {
                    $modelId = $record->uuid;
                    $record->uuid = Uuid::fromString($record->uuid)->getBytes();
                    $record->jobUuid = Uuid::fromString($record->jobUuid)->getBytes();
                    $record->waystationUuid = Uuid::fromString($record->waystationUuid)->getBytes();
                    $record->userUuid = ($record->userUuid) ? Uuid::fromString($record->userUuid)->getBytes() : null;

                    DB::table('job_waystation_adjustments')
                  ->where('uuid', $modelId)
                  ->update([
                    'uuid' => $record->uuid,
                    'jobUuid' => $record->jobUuid,
                    'waystationUuid' => $record->waystationUuid,
                    'userUuid' => $record->userUuid,
                  ]);
                }

                \DB::statement('ALTER TABLE job_waystation_adjustments MODIFY COLUMN uuid BINARY(16)');
                \DB::statement('ALTER TABLE job_waystation_adjustments MODIFY COLUMN jobUuid BINARY(16)');
                \DB::statement('ALTER TABLE job_waystation_adjustments MODIFY COLUMN waystationUuid BINARY(16)');
                \DB::statement('ALTER TABLE job_waystation_adjustments MODIFY COLUMN userUuid BINARY(16)');
            });
        }

        if (DB::connection()->getDoctrineColumn('waystation_bank_accounts', 'waystationUuid')->getType()->getName() != 'binary') {
            DB::transaction(
            function () {
                \DB::statement('ALTER TABLE waystation_bank_accounts CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci');
                \DB::statement('ALTER TABLE waystation_bank_accounts MODIFY COLUMN waystationUuid BINARY(36)');

                $records = DB::table('waystation_bank_accounts')->get();

                foreach ($records as $record) {
                    $modelId = $record->waystationUuid;
                    $record->waystationUuid = Uuid::fromString($record->waystationUuid)->getBytes();

                    DB::table('waystation_bank_accounts')
                          ->where('waystationUuid', $modelId)
                          ->update([
                            'waystationUuid' => $record->waystationUuid,
                  ]);
                }

                \DB::statement('ALTER TABLE waystation_bank_accounts MODIFY COLUMN waystationUuid BINARY(16)');
            }
        );
        }

        if (DB::connection()->getDoctrineColumn('job_addons', 'jobUuid')->getType()->getName() != 'binary') {
            DB::transaction(function () {
                \DB::statement('ALTER TABLE job_addons CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci');
                \DB::statement('ALTER TABLE job_addons MODIFY COLUMN jobUuid BINARY(36)');
                \DB::statement('ALTER TABLE job_addons MODIFY COLUMN addonable_id BINARY(36)');

                $records = DB::table('job_addons')->get();

                foreach ($records as $record) {
                    $modelId = $record->jobUuid;
                    $record->jobUuid = ($record->jobUuid) ? Uuid::fromString($record->jobUuid)->getBytes() : null;
                    $record->addonable_id = ($record->addonable_id) ? Uuid::fromString($record->addonable_id)->getBytes() : null;

                    DB::table('job_addons')
                  ->where('jobUuid', $modelId)
                  ->update([
                    'jobUuid' => $record->jobUuid,
                    'addonable_id' => $record->addonable_id,
                  ]);
                }

                \DB::statement('ALTER TABLE job_addons MODIFY COLUMN jobUuid BINARY(16)');
                \DB::statement('ALTER TABLE job_addons MODIFY COLUMN addonable_id BINARY(16)');
            });
        }

        if (DB::connection()->getDoctrineColumn('tasks', 'uuid')->getType()->getName() != 'binary') {
            DB::transaction(function () {
                \DB::statement('ALTER TABLE tasks CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci');
                \DB::statement('ALTER TABLE tasks MODIFY COLUMN uuid BINARY(36)');
                \DB::statement('ALTER TABLE tasks MODIFY COLUMN causable_id BINARY(36)');

                $records = DB::table('tasks')->get();

                foreach ($records as $record) {
                    $modelId = $record->uuid;
                    $record->uuid = ($record->uuid) ? Uuid::fromString($record->uuid)->getBytes() : null;
                    $record->causable_id = ($record->causable_id) ? Uuid::fromString($record->causable_id)->getBytes() : null;

                    DB::table('tasks')
                  ->where('jobUuid', $modelId)
                  ->update([
                    'jobUuid' => $record->jobUuid,
                    'causable_id' => $record->causable_id,
                  ]);
                }

                \DB::statement('ALTER TABLE tasks MODIFY COLUMN uuid BINARY(16)');
                \DB::statement('ALTER TABLE tasks MODIFY COLUMN causable_id BINARY(16)');
            });
        }

        if (DB::connection()->getDoctrineColumn('refunds', 'uuid')->getType()->getName() != 'binary') {
            DB::transaction(function () {
                // \DB::statement('ALTER TABLE refunds CONVERT TO CHARACTER SET utf8_unicode_ci COLLATE utf');
                \DB::statement('ALTER TABLE refunds MODIFY COLUMN uuid BINARY(36)');

                $refunds = DB::table('refunds')->get();

                foreach ($refunds as $refund) {
                    $modelId = $refund->uuid;
                    $refund->uuid = ($refund->uuid) ? Uuid::fromString($refund->uuid)->getBytes() : null;

                    DB::table('refunds')
                        ->where('jobUuid', $modelId)
                        ->update([
                          'uuid' => $refund->uuid,
                        ]);
                }

                \DB::statement('ALTER TABLE refunds MODIFY COLUMN uuid BINARY(16)');
            });
        }

        return 'Migration to Binary completed.';
    });



# Password reset routes if user forgot his password:
Route::post('/auth/request-reset-password-email', '\App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail');
Route::post('/auth/reset-password', '\App\Http\Controllers\Auth\ResetPasswordController@reset');
Route::get('/app/auth/reset-password/{token}', '\App\Http\Controllers\Auth\ResetPasswordController@showResetForm')->name('auth.password.reset');



// Query String
Route::get('/query', function () {
    return request()->query();
    $data = Searchy::search(request()->query('model'))->fields(request()->query('attributes'))->query(request()->query('query'))->get();
    return $data;
});