From ad457db740b83ee6958fbdd359766bb539ec2d55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Kv=C3=A1=C4=8Dek?= Date: Wed, 17 Feb 2016 16:14:26 +0100 Subject: [PATCH] FEATURE #1452: add database migration for adding templates into database --- .../2016_02_17_131804_AddTemplateSupport.php | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 database/migrations/2016_02_17_131804_AddTemplateSupport.php diff --git a/database/migrations/2016_02_17_131804_AddTemplateSupport.php b/database/migrations/2016_02_17_131804_AddTemplateSupport.php new file mode 100644 index 00000000..ca50f4c2 --- /dev/null +++ b/database/migrations/2016_02_17_131804_AddTemplateSupport.php @@ -0,0 +1,124 @@ +dropForeign('fk_content_module1'); + }); + } catch (\Illuminate\Database\QueryException $e) { + // probably foreign does not exist, try to remove one from migration + try { + Schema::table('contents', function (Blueprint $table) { + $table->dropForeign('contents_module_handler_foreign'); + }); + } catch (\Illuminate\Database\QueryException $ex) { + // ignore this error + } + } + + // drop primary index in modules (previously was on handler column) + Schema::table('modules', function (Blueprint $table) { + $table->dropIndex('PRIMARY'); + }); + + // create new column ID, set it autoincrement and set unique index to handler + Schema::table('modules', function (Blueprint $table) { + $table->integer('id', true) + ->first(); + $table->unique('handler'); + }); + + // add again FK constraint to contents -> modules relationship + Schema::table('contents', function (Blueprint $table) { + $table->foreign('module_handler') + ->references('handler') + ->on('modules') + ->onDelete('cascade') + ->onUpdate('cascade'); + }); + + // create new table with templates + Schema::create('templates', function (Blueprint $table) { + $table->integer('id', true); + $table->integer('module_id') + ->comment('Template belongs to module specified with this ID.'); + $table->timestamps(); + $table->string('template_name', 128) + ->comment('Template name used for rendering page. Use namespace::dir.template format.'); + $table->string('path') + ->comment('Path to this template. Given as relative path for theme directory.'); + $table->string('name') + ->comment('Human readable name of given template'); + $table->mediumText('description') + ->comment('Description of this template.'); + $table->boolean('editable') + ->comment('Can this template be edited?'); + + $table->index('module_id'); + $table->index('template_name'); + $table->unique(['module_id', 'template_name']); + }); + + // add FK to templates->modules + Schema::table('templates', function (Blueprint $table) { + $table->foreign('module_id') + ->references('id') + ->on('modules') + ->onDelete('cascade') + ->onUpdate('cascade'); + }); + + // add column to contents - template name + Schema::table('contents', function (Blueprint $table) { + $table->string('template', 128) + ->after('module_handler') + ->comment('Template used for rendering this content page. [This should be FK to templates (template_name), however I was not able to create the constraint...]'); + + $table->index('template'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + + Schema::drop('templates'); + + Schema::table('contents', function (Blueprint $table) { + $table->dropForeign('contents_module_handler_foreign'); + }); + + Schema::table('modules', function (Blueprint $table) { + $table->dropColumn('id'); + $table->dropIndex('modules_handler_unique'); + $table->primary('handler'); + }); + + Schema::table('contents', function (Blueprint $table) { + $table->dropColumn('template'); + + $table->foreign('module_handler') + ->references('handler') + ->on('modules') + ->onDelete('cascade') + ->onUpdate('cascade'); + }); + } +}