From ba107184e1e7025d49ca8fdb1509c3d3c71d3fc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Kv=C3=A1=C4=8Dek?= Date: Mon, 8 Aug 2016 23:00:24 +0200 Subject: [PATCH] FIX: fix missing FK in contents->templates --- ..._08_204055_AddFKToContenctAndTemplates.php | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 database/migrations/2016_08_08_204055_AddFKToContenctAndTemplates.php diff --git a/database/migrations/2016_08_08_204055_AddFKToContenctAndTemplates.php b/database/migrations/2016_08_08_204055_AddFKToContenctAndTemplates.php new file mode 100644 index 00000000..c0cef763 --- /dev/null +++ b/database/migrations/2016_08_08_204055_AddFKToContenctAndTemplates.php @@ -0,0 +1,63 @@ +getDoctrineSchemaManager() + ->getDatabasePlatform(); + $platform->registerDoctrineTypeMapping('enum', 'string'); + + Schema::table('contents', function (Blueprint $table) { + $table->string('template', 128) + ->nullable(true) + ->comment('Template used for rendering this content page.') + ->change(); + }); + + DB::update('UPDATE contents SET template = NULL WHERE template NOT IN (SELECT DISTINCT template_name FROM templates)'); + + // add FK + Schema::table('contents', function (Blueprint $table) { + $table->foreign('template') + ->references('template_name') + ->on('templates') + ->onDelete('set null') + ->onUpdate('cascade'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + $platform = Schema::getConnection() + ->getDoctrineSchemaManager() + ->getDatabasePlatform(); + $platform->registerDoctrineTypeMapping('enum', 'string'); + + Schema::table('contents', function (Blueprint $table) { + $table->dropForeign('contents_template_foreign'); + }); + + Schema::table('contents', function (Blueprint $table) { + $table->string('template', 128) + ->nullable(false) + ->change(); + }); + } +}