# frozen_string_literal: true require "jobshop/helpers/migration.rb" class CreateInspections < ActiveRecord::Migration[5.2] include Jobshop::Helpers::Migration def change create_inspection_reports_table create_inspection_criteria_table create_inspection_boolean_criteria_table create_inspection_limit_criteria_table create_inspection_deviation_criteria_table end private def create_inspection_reports_table create_table_with_auditing :jobshop_inspection_reports, primary_key: %i[ organization_id id ] do |t| t.text :comment t.timestamps end fk_organization :jobshop_inspection_reports end private def create_inspection_criteria_table create_table_with_auditing :jobshop_inspection_criteria, primary_key: { organization_id: :uuid, report_id: :uuid, criterion_id: :uuid, criterion_type: :string } do |t| t.integer :position t.citext :name t.index %i[ report_id name ], unique: true end fk_organization :jobshop_inspection_criteria foreign_key :jobshop_inspection_criteria, :jobshop_inspection_reports, { organization_id: :organization_id, report_id: :id } # Q: At what point do Jobshop::Inspection::Criteria::Types # become their own relation? # # A: Hopefully soon. execute <<~SQL ALTER TABLE jobshop_inspection_criteria ADD CONSTRAINT valid_criterion_type CHECK (criterion_type IN ( 'Jobshop::Inspection::BooleanCriterion', 'Jobshop::Inspection::LimitCriterion', 'Jobshop::Inspection::DeviationCriterion' )) SQL end private def create_inspection_boolean_criteria_table create_table_with_auditing :jobshop_inspection_boolean_criteria, primary_key: %i[ organization_id report_id id ] do |t| t.citext :condition end fk_organization :jobshop_inspection_boolean_criteria end private def create_inspection_limit_criteria_table create_table_with_auditing :jobshop_inspection_limit_criteria, primary_key: %i[ organization_id report_id id ] do |t| t.decimal :minimum t.decimal :maximum t.string :unit, null: false, default: "mm" end fk_organization :jobshop_inspection_limit_criteria end private def create_inspection_deviation_criteria_table create_table_with_auditing :jobshop_inspection_deviation_criteria, primary_key: %i[ organization_id report_id id ] do |t| t.decimal :nominal, null: false t.decimal :lower, null: false t.decimal :upper, null: false t.string :unit, null: false, default: "mm" end fk_organization :jobshop_inspection_deviation_criteria end end