FEATURE #1452: add database migration for adding templates into database

This commit is contained in:
Michal Kváček
2016-02-17 16:14:26 +01:00
parent 6a7402248a
commit ad457db740
@@ -0,0 +1,124 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddTemplateSupport extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// drop FK in contents table to modules
try {
Schema::table('contents', function (Blueprint $table) {
$table->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');
});
}
}