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'); } };