From 341bfb08a84f7f7682e84351f59396d14bad9f95 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Mon, 14 Nov 2016 18:36:42 +0000 Subject: [PATCH] Use aliases when migrating data. Fixes #2240 --- .../Providers/IntegrationServiceProvider.php | 4 ++- app/Http/Controllers/StatusPageController.php | 2 ++ app/Integrations/Contracts/System.php | 7 ++++ app/Integrations/Core/System.php | 32 +++++++++++++++++++ ...AlterTableIncidentsAddOccurredAtColumn.php | 6 +++- ...erTableIncidentsRemoveScheduledColumns.php | 5 ++- 6 files changed, 53 insertions(+), 3 deletions(-) diff --git a/app/Foundation/Providers/IntegrationServiceProvider.php b/app/Foundation/Providers/IntegrationServiceProvider.php index 4f4ba9e0..6b53dc15 100644 --- a/app/Foundation/Providers/IntegrationServiceProvider.php +++ b/app/Foundation/Providers/IntegrationServiceProvider.php @@ -96,7 +96,9 @@ class IntegrationServiceProvider extends ServiceProvider protected function registerSystem() { $this->app->singleton(SystemContract::class, function (Container $app) { - return new System(); + $config = $app['config']; + + return new System($config); }); } diff --git a/app/Http/Controllers/StatusPageController.php b/app/Http/Controllers/StatusPageController.php index 982ed6c9..7eb8e77f 100644 --- a/app/Http/Controllers/StatusPageController.php +++ b/app/Http/Controllers/StatusPageController.php @@ -92,6 +92,8 @@ class StatusPageController extends AbstractApiController return app(DateFactory::class)->make($incident->occurred_at)->toDateString(); }); + dd($allIncidents); + // Add in days that have no incidents if (Config::get('setting.only_disrupted_days') === false) { foreach ($incidentDays as $i) { diff --git a/app/Integrations/Contracts/System.php b/app/Integrations/Contracts/System.php index e64f4262..385ad501 100644 --- a/app/Integrations/Contracts/System.php +++ b/app/Integrations/Contracts/System.php @@ -31,4 +31,11 @@ interface System * @return string */ public function getVersion(); + + /** + * Get the table prefix. + * + * @return string + */ + public function getTablePrefix(); } diff --git a/app/Integrations/Core/System.php b/app/Integrations/Core/System.php index 29a117a4..5504c21c 100644 --- a/app/Integrations/Core/System.php +++ b/app/Integrations/Core/System.php @@ -14,6 +14,7 @@ namespace CachetHQ\Cachet\Integrations\Core; use CachetHQ\Cachet\Integrations\Contracts\System as SystemContract; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\Incident; +use Illuminate\Contracts\Config\Repository; /** * This is the core system class. @@ -22,6 +23,25 @@ use CachetHQ\Cachet\Models\Incident; */ class System implements SystemContract { + /** + * The illuminate config instance. + * + * @var \Illuminate\Contracts\Config\Repository + */ + protected $config; + + /** + * Create a new system instance. + * + * @param \Illuminate\Contracts\Config\Repository $config + * + * @return void + */ + public function __construct(Repository $config) + { + $this->config = $config; + } + /** * Get the entire system status. * @@ -80,4 +100,16 @@ class System implements SystemContract { return CACHET_VERSION; } + + /** + * Get the table prefix. + * + * @return string + */ + public function getTablePrefix() + { + $driver = $this->config->get('database.default'); + + return $this->config->get("database.connections.{$driver}.prefix"); + } } diff --git a/database/migrations/2016_10_24_183415_AlterTableIncidentsAddOccurredAtColumn.php b/database/migrations/2016_10_24_183415_AlterTableIncidentsAddOccurredAtColumn.php index 8653fe83..9fad9497 100644 --- a/database/migrations/2016_10_24_183415_AlterTableIncidentsAddOccurredAtColumn.php +++ b/database/migrations/2016_10_24_183415_AlterTableIncidentsAddOccurredAtColumn.php @@ -9,6 +9,7 @@ * file that was distributed with this source code. */ +use CachetHQ\Cachet\Integrations\Contracts\System; use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\DB; @@ -28,7 +29,10 @@ class AlterTableIncidentsAddOccurredAtColumn extends Migration }); // We need a better way of handling data migrations... - DB::update('UPDATE incidents SET occurred_at = created_at'); + $system = app(System::class); + $prefix = $system->getTablePrefix(); + + DB::update("UPDATE {$prefix}incidents SET occurred_at = created_at"); } /** diff --git a/database/migrations/2016_10_30_182324_AlterTableIncidentsRemoveScheduledColumns.php b/database/migrations/2016_10_30_182324_AlterTableIncidentsRemoveScheduledColumns.php index 625dc553..a807e337 100644 --- a/database/migrations/2016_10_30_182324_AlterTableIncidentsRemoveScheduledColumns.php +++ b/database/migrations/2016_10_30_182324_AlterTableIncidentsRemoveScheduledColumns.php @@ -9,6 +9,7 @@ * file that was distributed with this source code. */ +use CachetHQ\Cachet\Integrations\Contracts\System; use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\DB; @@ -24,7 +25,9 @@ class AlterTableIncidentsRemoveScheduledColumns extends Migration public function up() { // We need a better way of handling data migrations... - DB::update('INSERT INTO schedules (name, message, scheduled_at, created_at, updated_at) SELECT name, message, scheduled_at, created_at, updated_at FROM incidents WHERE scheduled_at IS NOT NULL'); + $system = app(System::class); + $prefix = $system->getTablePrefix(); + DB::update("INSERT INTO {$prefix}schedules (name, message, scheduled_at, created_at, updated_at) SELECT name, message, scheduled_at, created_at, updated_at FROM {$prefix}incidents WHERE scheduled_at IS NOT NULL"); DB::table('incidents')->whereNotNull('scheduled_at')->delete();