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(); + }); + } +}