clrghouz/database/migrations/2023_07_31_104758_bbs.php
2023-09-13 20:59:40 +10:00

118 lines
3.1 KiB
PHP

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$this->down();
Schema::create('modes', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('name',16);
$table->string('note',255)->nullable();
$table->unique(['name']);
});
Schema::create('cugs', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('name', 16);
$table->string('description', 255)->nullable();
$table->integer('parent_id')->nullable();
$table->foreign('parent_id')->references('id')->on('cugs');
$table->unique(['name']);
$table->softDeletes();
});
Schema::create('cug_users', function (Blueprint $table) {
$table->integer('cug_id')->unsigned();
$table->foreign('cug_id')->references('id')->on('cugs');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->boolean('owner');
$table->unique(['user_id','cug_id']);
});
Schema::create('frames', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->integer('frame')->unsigned();
$table->char('index', 1);
$table->string('r0')->nullable();
$table->string('r1')->nullable();
$table->string('r2')->nullable();
$table->string('r3')->nullable();
$table->string('r4')->nullable();
$table->string('r5')->nullable();
$table->string('r6')->nullable();
$table->string('r7')->nullable();
$table->string('r8')->nullable();
$table->string('r9')->nullable();
$table->string('type', 2);
$table->smallInteger('cost')->default(0);
$table->boolean('access')->default(FALSE);
$table->boolean('public')->default(FALSE);
$table->binary('content');
$table->string('title',16)->nullable();
$table->string('note', 255)->nullable();
$table->boolean('cls')->default(TRUE);
//$table->unique(['frame','index','mode_id']); // Not needed since we have timewarp
$table->integer('mode_id')->unsigned();
$table->foreign('mode_id')->references('id')->on('modes');
$table->integer('cug_id')->unsigned()->default(0);
$table->foreign('cug_id')->references('id')->on('cugs');
$table->softDeletes();
});
DB::statement("
CREATE VIEW frame_view AS
(
SELECT F.id, F.frame || F.index as page,F.type,F.access,F.public,F.cls,C.name as cug,M.name as mode,F.cost,F.title,
F.r0,F.r1,F.r2,F.r3,F.r4,F.r5,F.r6,F.r7,F.r8,F.r9
FROM frames F
LEFT JOIN cugs C ON C.id=F.cug_id
LEFT JOIN modes M ON M.id=F.mode_id
ORDER BY
F.mode_id,F.frame,F.index
)
");
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::statement('DROP VIEW IF EXISTS frame_view');
Schema::dropIfExists('frames');
Schema::dropIfExists('cug_users');
Schema::dropIfExists('cugs');
Schema::dropIfExists('modes');
}
};