# frozen_string_literals: true require "jobshop/helpers/migration.rb" class CreateInspections < ActiveRecord::Migration[5.2] include Jobshop::Helpers::Migration def change # rubocop:disable Metrics/MethodLength create_table :jobshop_inspection_reports, id: false do |t| t.uuid :organization_id, null: false t.uuid :report_id, null: false, default: "gen_random_uuid()" t.index %i[ organization_id report_id ], unique: true, name: "idx_jobshop_inspection_reports_pkey" t.timestamps end idx_table_name_pkey :jobshop_inspection_reports fk_organization_id :jobshop_inspection_reports create_table :jobshop_inspection_criteria, id: false do |t| t.uuid :organization_id, null: false t.uuid :report_id, null: false t.uuid :criterion_id, null: false, default: "gen_random_uuid()" t.string :criterion_type, null: false t.index %i[ organization_id report_id criterion_id criterion_type ], name: "idx_jobshop_inspection_criteria_pkey", unique: true t.integer :position, null: false t.index %i[ report_id position ], unique: true t.citext :name, null: false t.index %i[ report_id name ], unique: true, name: "idx_jobshop_inspection_criteria_name" end # At what point do Jobshop::Inspection::Criteria::Types # become their own relation? 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 idx_table_name_pkey :jobshop_inspection_criteria fk_organization_id :jobshop_inspection_criteria create_table :jobshop_inspection_tuples, id: false do |t| t.uuid :organization_id, null: false t.uuid :report_id, null: false t.integer :position, null: false t.index %i[ organization_id report_id position ], unique: true, name: "idx_jobshop_inspection_tuples_pkey" end idx_table_name_pkey :jobshop_inspection_tuples fk_organization_id :jobshop_inspection_tuples create_table :jobshop_inspection_results, id: false do |t| t.uuid :organization_id, null: false t.uuid :report_id, null: false t.uuid :criterion_id, null: false t.string :criterion_type, null: false t.integer :position, null: false t.index %i[ organization_id report_id criterion_id criterion_type position ], name: "idx_jobshop_inspection_results_pkey", unique: true t.decimal :value t.string :unit, null: false, default: "mm" end idx_table_name_pkey :jobshop_inspection_results fk_organization_id :jobshop_inspection_results foreign_key :jobshop_inspection_results, :jobshop_inspection_criteria, %i[ organization_id report_id criterion_id criterion_type ], "DEFERRABLE INITIALLY DEFERRED" foreign_key :jobshop_inspection_results, :jobshop_inspection_tuples, %i[ organization_id report_id position ], "DEFERRABLE INITIALLY DEFERRED" create_table :jobshop_inspection_boolean_criteria, id: false do |t| t.uuid :organization_id, null: false t.uuid :report_id, null: false t.uuid :criterion_id, null: false t.string :criterion_type, null: false, default: "Jobshop::Inspection::BooleanCriterion" t.index %i[ organization_id report_id criterion_id criterion_type ], name: "idx_jobshop_inspection_boolean_criteria_pkey", unique: true t.citext :condition end execute <<~SQL ALTER TABLE jobshop_inspection_boolean_criteria ADD CONSTRAINT boolean_criterion_type CHECK (criterion_type = 'Jobshop::Inspection::BooleanCriterion') SQL idx_table_name_pkey :jobshop_inspection_boolean_criteria fk_organization_id :jobshop_inspection_boolean_criteria foreign_key :jobshop_inspection_boolean_criteria, :jobshop_inspection_criteria, %i[ organization_id report_id criterion_id criterion_type ], "DEFERRABLE INITIALLY DEFERRED" create_table :jobshop_inspection_limit_criteria, id: false do |t| t.uuid :organization_id, null: false t.uuid :report_id, null: false t.uuid :criterion_id, null: false t.string :criterion_type, null: false, default: "Jobshop::Inspection::LimitCriterion" t.index %i[ organization_id report_id criterion_id criterion_type ], name: "idx_jobshop_inspection_limit_criteria_pkey", unique: true t.decimal :minimum t.decimal :maximum t.string :unit, null: false, default: "mm" end execute <<~SQL ALTER TABLE jobshop_inspection_limit_criteria ADD CONSTRAINT limit_criterion_type CHECK (criterion_type = 'Jobshop::Inspection::LimitCriterion') SQL idx_table_name_pkey :jobshop_inspection_limit_criteria fk_organization_id :jobshop_inspection_limit_criteria foreign_key :jobshop_inspection_limit_criteria, :jobshop_inspection_criteria, %i[ organization_id report_id criterion_id criterion_type ], "DEFERRABLE INITIALLY DEFERRED" create_table :jobshop_inspection_deviation_criteria, id: false do |t| t.uuid :organization_id, null: false t.uuid :report_id, null: false t.uuid :criterion_id, null: false t.string :criterion_type, null: false, default: "Jobshop::Inspection::DeviationCriterion" t.index %i[ organization_id report_id criterion_id criterion_type ], name: "idx_jobshop_inspection_deviation_criteria_pkey", unique: true t.decimal :nominal, null: false t.decimal :lower, null: false t.decimal :upper, null: false t.string :unit, null: false, default: "mm" end execute <<~SQL ALTER TABLE jobshop_inspection_deviation_criteria ADD CONSTRAINT deviation_criterion_type CHECK (criterion_type = 'Jobshop::Inspection::DeviationCriterion') SQL idx_table_name_pkey :jobshop_inspection_deviation_criteria fk_organization_id :jobshop_inspection_deviation_criteria foreign_key :jobshop_inspection_deviation_criteria, :jobshop_inspection_criteria, %i[ organization_id report_id criterion_id criterion_type ], "DEFERRABLE INITIALLY DEFERRED" end end