Files
Toiminnanohjaus/database/migrations/2026_11_26_143637_create_machines_tables.php
2026-11-26 16:43:55 +02:00

91 lines
3.8 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Machines (spec 19.4–19.5): the machines in their service units, their
* counters and readings, and the day-by-day work list of each machine,
* whose rows are job tasks or steps of quick sale order lines (the
* order lines get their foreign key with their table).
*/
public function up(): void
{
Schema::create('machines', function (Blueprint $table) {
$table->id();
$table->string('name', 150);
$table->string('type', 20);
$table->text('description')->nullable();
$table->foreignId('responsible_user_id')->nullable()->constrained('users')->nullOnDelete();
$table->boolean('is_active')->default(true);
$table->timestamps();
});
Schema::create('machine_service_units', function (Blueprint $table) {
$table->foreignId('machine_id')->constrained()->cascadeOnDelete();
$table->foreignId('service_unit_id')->constrained()->restrictOnDelete();
$table->primary(['machine_id', 'service_unit_id']);
});
Schema::create('machine_counters', function (Blueprint $table) {
$table->id();
$table->foreignId('machine_id')->constrained()->cascadeOnDelete();
$table->string('name', 100);
$table->string('unit', 20);
$table->unsignedInteger('sort_order');
$table->timestamps();
});
Schema::create('machine_counter_readings', function (Blueprint $table) {
$table->id();
$table->foreignId('machine_counter_id')->constrained()->cascadeOnDelete();
$table->bigInteger('reading_milli');
$table->foreignId('user_id')->constrained()->restrictOnDelete();
$table->timestamp('read_at');
$table->timestamps();
$table->index(['machine_counter_id', 'read_at']);
});
Schema::create('machine_queue_entries', function (Blueprint $table) {
$table->id();
$table->foreignId('machine_id')->constrained()->restrictOnDelete();
$table->date('planned_on');
$table->unsignedInteger('position');
$table->foreignId('task_id')->nullable()->constrained()->cascadeOnDelete();
$table->unsignedBigInteger('quick_sale_order_line_id')->nullable();
$table->unsignedSmallInteger('production_step_position')->nullable();
$table->timestamp('completed_at')->nullable();
$table->foreignId('completed_by')->nullable()->constrained('users')->nullOnDelete();
$table->timestamps();
$table->index(['machine_id', 'completed_at', 'planned_on']);
$table->index(['task_id', 'completed_at']);
});
Schema::table('stock_movements', function (Blueprint $table) {
$table->foreign('machine_id', 'stock_movements_machine_foreign')->references('id')->on('machines')->restrictOnDelete();
});
}
public function down(): void
{
// SQLite (tests) cannot drop a foreign key; there the table is dropped by the earlier migration's rollback.
if (Schema::getConnection()->getDriverName() !== 'sqlite') {
Schema::table('stock_movements', function (Blueprint $table) {
$table->dropForeign('stock_movements_machine_foreign');
});
}
Schema::dropIfExists('machine_queue_entries');
Schema::dropIfExists('machine_counter_readings');
Schema::dropIfExists('machine_counters');
Schema::dropIfExists('machine_service_units');
Schema::dropIfExists('machines');
}
};